php - 如何使用 Codeigniter 从数据库中创建动态导航菜单选择?

标签 php codeigniter

我一直在尝试寻找使用 Codeigniter 在数据库中创建动态导航菜单存储的解决方案。但直到现在我都无法解决我的问题而且我真的不明白如何创建此功能我尝试创建以下代码但我只能显示没有子菜单的菜单。 正如在 View 中一样,我不仅可以选择菜单,还可以选择子菜单。我真的不明白如何为创建子菜单设置条件 因为我必须将 parent 与菜单 ID 进行比较,如果我发现 parent 等于菜单 ID 我将显示下拉当前菜单 但这对我来说是失败的

这是我的观点

<div class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
       <li><a>some menu retrieve from DB </a></li>
           <li class="dropdown megamenu-fullwidth"> 
              <a data-toggle="dropdown" class="dropdown-toggle">title</a>
              <ul class="dropdown-menu"> 
                <li class="megamenu-content">
                 <ul class="col-lg-2 col-sm-2 col-md-2">
                   <li class="no-border"><p><strong>Head</strong></p></li>
                   <li><a href="#">Descript<br></a></li>
                 </ul> 
               </li> 
            </ul>
        </li>
       <?PHP endif; ?>
      <?PHP endforeach; ?>
 </ul>    
</div>

这是 Controller

$this->data['menus'] =  $this->nav_m->menus(); 

这是模型

 public function menus() {
        $this->db->select("*");
        $this->db->from("nav"); 
        $this->q = $this->db->get();
        if ($this->q->num_rows() > 0) {
           return $this->q->result_array();
        }
    }

这是我使用来自 DB 的 Var_dump() 的导航

array (size=7)
  0 => 
    array (size=9)
      'id' => string '1' (length=1)
      'uid' => string '0' (length=1)
      'text' => string 'title1' (length=6)
      'link' => string 'menus1' (length=6)
      'show_condition' => string '1' (length=1)
      'parent' => string '0' (length=1)
      'class' => string '' (length=0)
      'data' => string '' (length=0)
      'des' => string '' (length=0)
  1 => 
    array (size=9)
      'id' => string '2' (length=1)
      'uid' => string '0' (length=1)
      'text' => string 'title2' (length=6)
      'link' => string 'menus2' (length=6)
      'show_condition' => string '1' (length=1)
      'parent' => string '2' (length=1)
      'class' => string '1' (length=1)
      'data' => string '' (length=0)
      'des' => string '' (length=0)
  2 => 
    array (size=9)
      'id' => string '3' (length=1)
      'uid' => string '0' (length=1)
      'text' => string 'title3' (length=6)
      'link' => string 'menu3' (length=5)
      'show_condition' => string '1' (length=1)
      'parent' => string '2' (length=1)
      'class' => string '' (length=0)
      'data' => string '' (length=0)
      'des' => string '' (length=0)
  3 => 
    array (size=9)
      'id' => string '4' (length=1)
      'uid' => string '0' (length=1)
      'text' => string 'Sub1' (length=4)
      'link' => string 'menu4' (length=5)
      'show_condition' => string '1' (length=1)
      'parent' => string '2' (length=1)
      'class' => string '' (length=0)
      'data' => string '' (length=0)
      'des' => string '' (length=0)
  4 => 
    array (size=9)
      'id' => string '5' (length=1)
      'uid' => string '0' (length=1)
      'text' => string 'sub2' (length=4)
      'link' => string 'hhhhhhhhhhhh' (length=12)
      'show_condition' => string '1' (length=1)
      'parent' => string '2' (length=1)
      'class' => string '' (length=0)
      'data' => string '' (length=0)
      'des' => string '' (length=0)
  5 => 

请帮忙

最佳答案

您获得结果的方式太复杂而无法处理。我会将数据库中的子菜单项和父菜单项分开,然后构建一个数组,以便 View 可以更轻松地解析它。试试这个:

数据库:

enter image description here

然后你的模型:

function menus() {
    $this->db->select("*");
    $this->db->from("menu_parents");
    $q = $this->db->get();

    $final = array();
    if ($q->num_rows() > 0) {
        foreach ($q->result() as $row) {

            $this->db->select("*");
            $this->db->from("menu_children");
            $this->db->where("fk_parent_id", $row->parent_id);
            $q = $this->db->get();
            if ($q->num_rows() > 0) {
                $row->children = $q->result();
            }
            array_push($final, $row);
        }
    }
    return $final;
}

Controller :

public function get_menus() {
    $this->load->model('your_model');
    $menus = $this->your_model->menus();
    $data = array('menus' => $menus);
    $this->load->view('page1', $data);
}

查看:

<div class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
        <?php foreach ($menus as $menu) { ?>
            <li><a><?= $menu->text ?></a></li>
            <?php
            if (isset($menu->children)) {
                foreach ($menu->children as $child) {
                    ?>
                    <li class="dropdown megamenu-fullwidth"> 
                        <a data-toggle="dropdown" class="dropdown-toggle" href="#"><</a>
                        <ul class="dropdown-menu"> 
                            <li class="megamenu-content">
                                <ul class="col-lg-2 col-sm-2 col-md-2">
                                    <li class="no-border"><p><strong>Head</strong></p></li>
                                    <li><a href="#">Descript<br></a></li>
                                </ul> 
                            </li> 
                        </ul>
                    </li>
                    <?php
                }
            }
            ?>
        <?php } ?>
    </ul>    
</div>

关于php - 如何使用 Codeigniter 从数据库中创建动态导航菜单选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28946105/

相关文章:

php - 使用 codeigniter PHP fatal error : Class 'MY_Model' not found

php - 将字符串数组写入由换行符分隔的文本文件

php - 在不使用 shuffle() 函数的情况下在 PHP 中随机化数组顺序的最佳方法是什么?

php - 加载 php_xdebug 消息失败

php - 发送 HTML 电子邮件会导调用子邮件显示 HTML 源代码(Codeigniter 电子邮件类)

php - 如何在 CodeIgniter 中使用准备好的语句

Laravel Http Facade 中的 PHP Curl 代理选项

php jquery mysql-更好的方式加载更多

javascript - 按钮需要点击两次才能工作,我希望只点击一次

php - 如何在php codeigniter中将动态生成的表数据保存到mysql数据库