显示选定父级的树菜单 [英] Display tree menu of selected parent

查看:91
本文介绍了显示选定父级的树菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我正在建立一个树菜单.类别通过其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

  • 计算机
    • 戴尔
    • HP
    • Computer
      • Dell
      • HP

      如果单击了Dell,则

      if Dell s clicked then

      • 计算机
        • 戴尔
          • 笔记本电脑
          • 桌面
          • Computer
            • Dell
              • Laptops
              • Desktops

              推荐答案

              我看到您的解决方案有一个问题.当您检查ID if($id == $record->id)时,您将仅匹配树中的当前级别.即选择id = 2的Dell将不匹配第一次迭代,因此您的功能将不会遍历下一个级别.

              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.

              以您为例.当您选择Dell时,您只会选择计算机",对吗?

              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));
              ...
              

              这篇关于显示选定父级的树菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆