mysql - 使用 PHP 和 mysql 创建菜单层次结构 Bootstrap

标签 mysql oop hierarchy codeigniter-3

抱歉我的英语不好 我有一个餐 table 菜单: here is my table menus

所以我想问一下,根据表中的数据制作代码的过程如何.. 或者也许有人在这方面有任何引用?

这是我尝试过但仍然失败的代码

public function all_tree()
    {
        $nodes = $this->db->get('alus_menu_group')->result();
        return $this->getChildren($nodes, 0, 0);
    }

public function getChildren($nodes ,$pid = 0, $depth = 0)
   {
      $tree = array();

      foreach($nodes as $node) {

         if ($node->menu_parent == $pid) {

            if($depth == 0)
            {
               if(!$this->getChildren1($nodes, $node->menu_id, ($depth + 1)))
               {
                  $hasil = "<li class='dropdown'>
                      <a href='#' class='dropdown-toggle' data-toggle='dropdown'>".$node->menu_nama."</a>
                      <ul class='dropdown-menu'>";
                  $hasil .= $this->getChildren($nodes, $node->menu_id, ($depth + 1));

               }else
               {
                  $hasil = "<li><a href=".base_url().$node->menu_uri." target=".$node->menu_target.">".$node->menu_nama."</a></li>";      
               }
            }
            if($depth == 1)
            {
               if(!$this->getChildren1($nodes, $node->menu_id, ($depth + 1)))
               {
                  $hasil = "<li class='dropdown-submenu'>
                      <a href='#'>".$node->menu_nama."</a>
                      <ul class='dropdown-menu'>";
                  $hasil .= $this->getChildren($nodes, $node->menu_id, ($depth + 1));

               }else
               {
                  $hasil = "<li><a href=".base_url().$node->menu_uri." target=".$node->menu_target.">".$node->menu_nama."</a></li></ul></li>";      

               }

            }if ($depth == 2) 
            {
               $hasil = "<li><a href=".base_url().$node->menu_uri." target=".$node->menu_target.">".$node->menu_nama."2</a></li></ul></li></ul></li>";   

            }
            $tree[]   = $hasil;
         }

      }
      return $tree;
   }
    public function view()
{
$data['tree'] = $this->all_tree();
$this->load->view('index.php',$data);
}

然后在 view.php 中我正在执行 foreach 。 有人可以帮忙吗?谢谢

最佳答案

此链接可能对您有帮助。

Link from code.tutsplus.com

您只需要根据上面链接的html制作动态数据即可。

希望这可以帮助你!!

关于mysql - 使用 PHP 和 mysql 创建菜单层次结构 Bootstrap ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37806698/

相关文章:

java - Spring数据@公式计数查询

php - 获取特定产品的所有父类别

MySQL 选择大写跟随小写的位置

c++ - 在不向下转换的情况下处理派生类的细节

Oracle 中具有来自另一个表的父节点的 XML 层次结构

c# - List<Object> 到层次结构

html - Thymeleaf - 使用多个片段进行布局的 div 类的正确顺序是什么?

mysql - 将此语句转换为sql查询

oop - 什么是“整体”和“整体”对象关系?

用于搜索具有不同增量数的java算法