I need to develope a webservice in hierarchical tree structure. I have tried a lot but Iam not getting the expected result. i have given my DB, code below and also my expected result.. Please help me
this is my db
category_id parent_id
1
2 1
3 1
4 2
5 2
6 4
7 5
8 3
9 3
10 6
Here is my code
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
error_reporting(0);
mysql_connect("localhost","root","");
mysql_select_db("hierarchical");
function display_children($parent, $level) {
$result = mysql_query('SELECT `category_id` FROM `category` WHERE `parent_id`="'.$parent.'";');
while ($row = mysql_fetch_array($result)) {
//echo str_repeat(' ',$level).$row['category_id']."n";
$res[]=array("Categoty"=>$row['category_id']);
display_children($row['category_id'], $level+1);
}
$response = array("Success" => "1", "Details" => $res);
$final = array("response" => $response);
echo json_encode($final);
}
echo display_children(1, 1);
?>
My expected result is
{
response: {
Success: "1",
Details: [
{
Categoty: "2"[
{
Categoty: "4"[
{
Categoty: "6"[
{
Categoty: "10"
}
]
}
]
Categoty: "5"[
{
Categoty: "7"
}
]
}
]
},
{
Categoty: "3"[
{
Categoty: "8"
}
{
Categoty: "9"
}
]
}
]
}
}