Codeigniter与mysql多维动态菜单创建?

时间:2017-02-16 08:25:43

标签: php codeigniter

我正在使用带有多维动态菜单的codeigniter开发一个网站。我正在使用codeigniter和mysql我有创建菜单,子菜单1,子菜单2。现在菜单和子菜单1正在加载网站,但我尝试加载子菜单2我遇到问题我在php中使用array_push方法。这是我在此代码中的代码我已经加载菜单和submenu1可以任何正文帮助我如何加载子菜单2 ...

这是我的查看代码..

<div class="collapse navbar-collapse">
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="<?php echo base_url()?>Pixel/index" >Home</a></li>
                         <?php foreach($menus as $menu) { ?>
                            <li id="mainmenu">
                                <a href="#" class="dropdown-toggle" id="<?php echo $menu->m_id; ?>"  data-toggle="dropdown"> <?php echo $menu->menuname; ?> <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    <?php
                                    if (isset($menu->children)) {
                                     foreach ($menu->children as $child) {
                                       ?>
                                     <li>
                                        <a href="<?php echo base_url()?>Pixel/corporate" id="submenuone" class="dropdown-toggle"><?php echo $child->submenu_name; ?>
                                        </a>
                                     </li>
                                     <?php
                                      }

                                     } ?>
                               </ul>
                            </li>
                       <?php } ?>

这是我的控制器代码

 public function index()
    {
                $menus = $this->Px_Model->populateMenus();
                $data = array('menus' => $menus);
                $this->load->view('pxHead');
                $this->load->view('pxHeader');
                $this->load->view('pxNav',$data);
                $this->load->view('pxSlider');
                $this->load->view('pxHome');
                $this->load->view('pxFooter');
    }

这是我的型号代码

public function populateMenus(){
    $this->db->select("*");
    $this->db->from("menu");
    $q = $this->db->get();
    $final = array();
    if ($q->num_rows() > 0) {
    foreach ($q->result() as $row){
            $this->db->select("*");
            $this->db->from("submenu1");
            $this->db->where("menu_id", $row->m_id);
            $q = $this->db->get();
            if ($q->num_rows() > 0) {
                $row->children = $q->result();
            }
            array_push($final, $row);
        }
    }
     return $final;
   }

1 个答案:

答案 0 :(得分:0)

尝试这样。需要制作多维数组。

<强> MODEL

// $final = array();
if ($q->num_rows() > 0) {
foreach ($q->result() as $row){
        $final[]['menuname'] = $row->menuname;
        $final[]['m_id'] = $row->m_id;
        $this->db->select("*");
        $this->db->from("submenu1");
        $this->db->where("menu_id", $row->m_id);
        $q = $this->db->get();
        if ($q->num_rows() > 0) {
            $final[]['children'] = $q->result();
        }

    }
}
 return $final;
}

查看

<div class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="<?php echo base_url()?>Pixel/index" >Home</a></li>
                     <?php foreach($menus as $key=>$menu) { ?>
                        <li id="mainmenu">
                            <a href="#" class="dropdown-toggle" id="<?php echo $menu['m_id']; ?>"  data-toggle="dropdown"> <?php echo $menu['menuname']; ?> <b class="caret"></b></a>
                            <ul class="dropdown-menu">
                                <?php
                                if (isset($menu['children'])) {
                                 foreach ($menu['children'] as $child) {
                                   ?>
                                 <li>
                                    <a href="<?php echo base_url()?>Pixel/corporate" id="submenuone" class="dropdown-toggle"><?php echo $child->submenu_name; ?>
                                    </a>
                                 </li>
                                 <?php
                                  }

                                 } ?>
                           </ul>
                        </li>
                   <?php } ?>