我需要在分层树结构中开发Web服务。我已经尝试了很多,但我没有得到预期的结果。我已经给了我的DB,下面的代码以及我的预期结果..请帮帮我
这是我的数据库
category_id parent_id
1
2 1
3 1
4 2
5 2
6 4
7 5
8 3
9 3
10 6
这是我的代码
<?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);
?>
我的预期结果是
{
response: {
Success: "1",
Details: [
{
Categoty: "2"[
{
Categoty: "4"[
{
Categoty: "6"[
{
Categoty: "10"
}
]
}
]
Categoty: "5"[
{
Categoty: "7"
}
]
}
]
},
{
Categoty: "3"[
{
Categoty: "8"
}
{
Categoty: "9"
}
]
}
]
}
}