[英]How to create dynamic navigation menu select from database using Codeigniter?
I've been trying to find the solution for creating a dynamic navigation menu store in database using Codeigniter. 我一直在尝试使用Codeigniter找到在数据库中创建动态导航菜单存储的解决方案。 But till now I can't solve my problem and I really don't understand how to create this feature I try to create below code but I can show only menu without sub menu.
但到目前为止我无法解决我的问题,我真的不明白如何创建此功能我尝试创建下面的代码,但我只能显示没有子菜单的菜单。 As in the view I can't only select menu but can't with sub menus.
在视图中,我不仅可以选择菜单,还可以选择子菜单。 I'm really don't understand how to make the conditional for create sub menus Because I have to compared parent with menus ID if I found the parent equal to menu ID I will show the drop down for current that menu But it is fail for me
我真的不明白如何为创建子菜单创建条件因为我必须将父菜单与菜单ID进行比较如果我发现父菜单等于菜单ID我将显示当前该菜单的下拉菜单但是它失败了我
Here is my View 这是我的观点
<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>
Here is Controller 这是控制器
$this->data['menus'] = $this->nav_m->menus();
Here is model 这是模特
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();
}
}
Here is my navigation which I used Var_dump() from DB 这是我在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 =>
Please help 请帮忙
the way you have your result is too complicated to deal with. 你的结果的方式太复杂,无法处理。 I would separate children and parent menu items in your database, then build an array so that the view can then parse it easier.
我会在数据库中分离子项和父项菜单项,然后构建一个数组,以便视图可以更容易地解析它。 Try this:
尝试这个:
DB: D B:
Then your model: 然后你的模型:
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);
}
View: 视图:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.