GenXCoders GenXCoders - 1 month ago 24
JSON Question

How to Traversal this?

I am working on an application that has a web based backend and a Android app based front end.
The backend is used to input data into MySQl (using PHP) and then generate a JSON which is then supplied to the Android App (front end) and displayed.

So here is what I have:
This is my Database

CREATE TABLE IF NOT EXISTS `tddept` (
`deptId` int(11) NOT NULL AUTO_INCREMENT,
`deptName` varchar(50) NOT NULL,
`deptIsRoot` tinyint(1) NOT NULL,
`deptHasChild` tinyint(1) NOT NULL,
`deptParentId` int(11) NOT NULL,
PRIMARY KEY (`deptId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;

--
-- Dumping data for table `tddept`
--

INSERT INTO `tddept` (`deptId`, `deptName`, `deptIsRoot`, `deptHasChild`, `deptParentId`) VALUES
(2, 'One', 1, 1, 0),
(3, 'Two', 1, 1, 0),
(4, 'Three', 1, 1, 0),
(5, 'One1', 0, 0, 2),
(6, 'One2', 0, 0, 2),
(7, 'One3', 0, 0, 2),
(12, 'Two1', 0, 0, 3),
(13, 'Three1', 0, 0, 4),
(14, 'Dept1', 1, 1, 0),
(15, 'dept1.1', 0, 0, 14);


My intention is simple to create a tree structure. only that there are multiple root nodes. and hence multiple leafs.

If put in simple words, i am trying to create a directory tree structure. I would want to display the data in the above table in to a dropdown menu (i.e. using and ) as:


  • One


    • One1



    • One2



    • One3


  • Two


    • Two1


  • Three


    • Three1


  • Dept1


    • dept1.1




Where One, Two, Three, Dept1 are root departments while the rest are sub-departments.
There can definitely be situations in the future where in i might have to have something like:
- One
- - One1
- - - One1a
- - - One1b
so on and so forth.

How can I traversal through them using Core PHP and how can I display them in the very structure as above in to a / (dropdown) menu.

PS: any heads up on JSON would also be a great help.

Regards,
Aashish

Answer

Here's the solution for image2

echo '<select>';

structureTree(0);

echo '</select>';

function structureTree($deptParentId){

$result = getDataFromDb($deptParentId); // change this into SQL
foreach($result as $result){
    echo '<option value="'.$deptParentId.'">'.str_repeat('-',$deptParentId).$result['deptName'].'</option>';
    structureTree($result['deptId']);
}

}

// mocking database record function getDataFromDb($deptParentId) {

$data = array(
    array(
        'deptId' => 1,
        'deptName' => 'IT',
        'deptParentId' => 0,
    ),
    array(
        'deptId' => 2,
        'deptName' => 'Human Resources',
        'deptParentId' => 1,    
    ),
    array(
        'deptId' => 3,
        'deptName' => 'Opreational',
        'deptParentId' => 1,    
    ),
    array(
        'deptId' => 4,
        'deptName' => 'Networking Department',
        'deptParentId' => 1,
    ),
    array(
        'deptId' => 5,
        'deptName' => 'Software Development',
        'deptParentId' => 1,    
    ),
    array(
        'deptId' => 6,
        'deptName' => 'Mobile Software Development',
        'deptParentId' => 5,    
    ),
    array(
        'deptId' => 7,
        'deptName' => 'ERP',
        'deptParentId' => 5,    
    ),
    array(
        'deptId' => 8,
        'deptName' => 'Product Development',
        'deptParentId' => 5,    
    ),
);
$result = array();
foreach ($data as $record) {
    if ($record['deptParentId'] === $deptParentId) {
        $result[] = $record;
    }
}

return $result;

}

Comments