显示所选父级的树形菜单
我在这里构建了一个树形菜单.类别通过其 id 及其显示的子项访问.我的功能适用于两级菜单,但无法生成第三级.仅当单击第二级项目的 id 时才应显示第三级菜单.我也需要这个函数来跟踪节点的路径.如果给出了第三级 id,那么应该将树扩展到第三级.即孩子的父母,如果该父母也有父母.mySQL 表
Here im building a tree menu. A category is accessed by its id and its child displayed. my function works fine for two level menu but fails to generate level third. Third level menu should be displayed only if id of a second level item clicked. I need this function to track the path to node too. That if a third level id is given than tree should be expanded to third level. i.e. parent of the child and if that parent has a parent too. mySQL Table
+-------+-----------+------------+
| id | title | parent_id |
+-------+-----------+------------+
| 1 | Computers | NULL |
+-------+-----------+------------+
| 2 | Dell | 1 |
+-------+-----------+------------+
| 3 | Laptops | 2 |
+-------+-----------+------------+
| 4 | Desktops | 2 |
+-------+-----------+------------+
| 5 | HP | 1 |
+-------+-----------+------------+
PHP 代码
<?php
$sql = "SELECT * FROM category";
$statement= $db->prepare($sql);
$statement->execute();
$categories = array();
$rootCategories = array();
while ( $row = $statement->fetchObject() ) {
$row->childs = array();
$categories[$row->id] = $row;
if(empty($row->parent_id)) {
$rootCategories[$row->id] = $categories[$row->id];
} else {
$categories[$row->parent_id]->childs[] = $categories[$row->id];
}
}
function rederTreeById($records, $id=false) {
echo '<ul>';
foreach($records as $record) {
if($id == $record->id) {
echo '<li>'.$record->title;
if(!empty($record->childs)) {
rederTreeById($record->childs);
}
echo '</li>';
} else {
echo '<li>'.$record->title.'</li>';
}
}
echo '</ul>';
}
rederTreeById($rootCategories, 1);
?>
例如如果点击计算机,则
For example if clicked Computers then
- 电脑
- 戴尔
- 惠普
如果戴尔点击了然后
- 电脑
- 戴尔
- 笔记本电脑
- 桌面
推荐答案
我发现您的解决方案存在一个问题.当您检查 ID
if($id == $record->id)
时,您将只匹配树中的当前级别.即选择 id=2 的戴尔将与第一次迭代不匹配,因此您的函数不会遍历到下一个级别.I see one problem with your solution. When you check for ID
if($id == $record->id)
you will only match the current level in the tree. i.e. selecting Dell with id=2 will not match the first iteration so your function wil not traverse to next level.您需要跟踪所选菜单的路径.
You need to keep track of the path to your selected menu.
就你而言.当您选择戴尔时,您只会看到计算机",对吗?
In your case. When you select Dell you will only se "Computer", am I right?
这样的事情怎么样:
... function rederTreeById($records, $path) { echo '<ul>'; foreach($records as $record) { if(in_array($record->id, $path)) { echo '<li>'.$record->title; if(!empty($record->childs)) { rederTreeById($record->childs, $path); } echo '</li>'; } else { echo '<li>'.$record->title.'</li>'; } } echo '</ul>'; } function getPath($id) { $path = array(); $current=$id; $path[] = 1 while(!is_null($categories[$current]->parent_id)) { $current=$categories[$current]->parent_id $path[] = $current; } return $path; } $selectedId = 1; rederTreeById($rootCategories, getPath($selectedId)); ...
- 戴尔
相关文章