douqing0713 2016-04-13 14:09
浏览 5

php mysql选择组织图/多级结构

I have build my user table like this:

enter image description here

Now I would like to select the 3 blue rows. I have build the following function:

function display_children($parent, $array = array()) {
  global $db;

  $stmt = $db->prepare("SELECT id AS man, parent_id FROM user WHERE parent_id= ?");
  $stmt->execute(array($parent));
  while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    display_children($row['man'], $array);
    $array[] = array("man" => $row['man'], "parent_id" => $row['parent_id']);
    echo $row['man']."
"; //for debug
  }
  return $array;
}
print_r(display_children(50001));

My Output is:

50002
50004
50003
Array
(
    [0] => Array
        (
            [man] => 50002
            [parent_id] => 50001
        )

    [1] => Array
        (
            [man] => 50003
            [parent_id] => 50001
        )

)

The first three lines are correct, but the array is missing one. The problem is, that the first parent_id has two rows. But the array is empty at the beginning. Is there a solution for my query?

  • 写回答

1条回答 默认 最新

  • duangekui7451 2016-04-13 21:31
    关注

    the solution was to change the following line from

    display_children($row['man'], $array);
    

    to

    $array = display_children($row['man'], $array);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)