用php中的pdo动态构建多级菜单

vcirk6k6  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(340)

我的数据库中有以下数据:

id  nameOfPerson    parent
3   John             NULL
4   Michel            3
5   Husam             4
6   Khalaf            5
7   Mark              5
----------------------------

我想在列表中这样显示它
厕所
米歇尔
胡萨姆
哈拉夫
作记号
但在我的代码里他只是
厕所
米歇尔
我想如何显示所有数据根据家长喜欢上面的列表?
这是我的职能有什么错?

public function familyTree(){
    $query = "SELECT id, nameOfPerson, parent FROM person WHERE parent is null";
    $statment = $this->db->prepare($query);
    $statment->execute();
    echo '<ul id ="family">';
    while($family = $statment->fetch(PDO::FETCH_OBJ)){
        echo '<li>'. $family->nameOfPerson;
        $query1 = "SELECT id, nameOfPerson, parent FROM person WHERE parent = :id";
        $statment1 = $this->db->prepare($query1);
       $statment1->bindValue('id', $family->id);
        $statment1->execute();
        if($statment1->rowCount() > 0){
        echo '<ul>';
        while($family2 = $statment1->fetch(PDO::FETCH_OBJ)){
            echo '<li>' . $family2->nameOfPerson . '</li>';
        }
            echo '</ul>';
        }
            echo '</li>';
    }
            echo '</ul>';      

}
5q4ezhmt

5q4ezhmt1#

假设
每个孩子只有 1 起源。
代码
首先,我从db表中检索所有数据,因为为每个父级单独检索数据会增加db调用并影响响应时间,因此效率不高。
构建父级和子级的关联数组,其中每个父级都有两个 each child - nameOfPerson 以及 id .
以递归的方式迭代这个数组,并继续为每个子级构建列表,最终返回它们。
有关表行,请参阅SQLFiddle。

<?php

$pdo = new PDO("mysql:dbname=newdummy;host=localhost","root","");

$query = $pdo->query("Select * from hierarchy");

$family_tree = [];
$root_parent = -1;
$root_parent_name = "";

function makeTree($query,&$family_tree,&$root_parent,&$root_parent_name){
   while($row = $query->fetch(PDO::FETCH_ASSOC)){
      if(is_null($row['parent'])){
          $root_parent = $row['id'];
          $root_parent_name = $row['nameOfPerson'];
      }else{
          if(!isset($family_tree[$row['parent']])){
            $family_tree[$row['parent']] = [];
          }          
          $family_tree[$row['parent']][] = array($row['nameOfPerson'],$row['id']);
      }    
   }
}

function buildList($family_tree,$parent){
  $list = "<ul>";

  foreach($family_tree[$parent] as $each_child){
     $list .= "<li>" . $each_child[0];
     if(isset($family_tree[$each_child[1]])){
        $list .= buildList($family_tree,$each_child[1]);
     }
     $list .= "</li>";
  }

  $list .= "</ul>";

  return $list;
}

makeTree($query,$family_tree,$root_parent,$root_parent_name);

echo "<ul>";
echo "<li>$root_parent_name";
echo buildList($family_tree,$root_parent);
echo "</li>";
echo "</ul>";

输出

<ul>
    <li>John
        <ul>
            <li>Michel
                <ul>
                    <li>
                        Husam
                            <ul>
                                <li>khalaf</li>
                                <li>Mark</li>
                            </ul>
                    </li>
                </ul>
            </li>
            <li>Tross</li>
            <li>David</li>
        </ul>
    </li>
</ul>

相关问题