doudaochu1699 2015-11-19 07:51
浏览 37

JSON视图中的分层树

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"
                }
                ]
            }
        ]
    }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?