如何在codeigniter的动态菜单中设置树视图



你好,guyz,我需要设计方面的帮助,我对设计没有任何想法,我在codeigniter中创建了动态菜单,我想在树视图中设置该菜单。打开子菜单后首先点击父菜单,所以如何实现它帮助我

这是我的视图代码:

<?php
foreach ($test as $val) {
$array = explode(",", $val->category_id);
}
foreach ($get_cat as $key => $value) {
if (in_array($value->category_id, $array)) {
echo $value->category_name . '&nbsp;&nbsp;';  //Here My Menu Print
}
}
?>

您需要使用parent_id=0获取所有类别,然后获取子类别。试试下面给出的东西。

public function get_categories(){
$this->db->select('*');
$this->db->from('categories');
$this->db->where('parent_id', 0);
//Add here role condition
$parent = $this->db->get();
$categories = $parent->result();
$i=0;
foreach($categories as $p_cat){
$categories[$i]->sub = $this->sub_categories($p_cat->cat_id);
$i++;
}
return $categories;
}

您的子类别函数。

public function sub_categories($id){
$this->db->select('*');
$this->db->from('categories');
$this->db->where('parent_id', $id);
//add here role condition
$child = $this->db->get();
$categories = $child->result();
$i=0;
foreach($categories as $p_cat){
$categories[$i]->sub = $this->sub_categories($p_cat->cat_id);
$i++;
}
return $categories;       
}

还有你的控制器。

public function categories(){
$this->load->model('model_categories');
$data = $this->model_categories->get_categories();
print_r($data);
}

这里我得到了CodeIgniter中动态菜单设计的解决方案:

<?php
//GET CATEGORY ID FROM USER REGISTARTION
foreach ($test as $val) {
$array = explode(",", $val->category_id);
//CATEGORY ID MATCH WITH CATEGORY NAME FROM CATEGORY TABLE
foreach ($listMenuLevel1 as $key => $value) {
if (in_array($value->category_id, $array)) {
?>  
<ul class="sidebar-menu">
<li class="treeview">
<a href="#">
<i class="fa fa-share"></i> <span><?php echo $value->category_name; ?></span> 
<i class="fa fa-angle-left pull-right"></i>
</a>
<ul class="treeview-menu">
<?php foreach ($this->main_model->listchildMenus($value->category_id) as $menu2) : ?>
<li class="treeview">
<a href="<?php echo base_url(); ?><?php echo $menu2->category_link; ?>"><i class="fa fa-circle-o"></i><?php echo $menu2->category_name; ?><i class="fa fa-angle-left pull-right"></i></a>
<?php foreach ($this->main_model->listchildMenus($menu2->category_id) as $menu3): ?>
<ul class="treeview-menu"> 
<li class="treeview">
<a href="<?php echo base_url(); ?><?php echo $menu3->category_link; ?>"><i class="fa fa-circle-o"></i><?php echo $menu3->category_name; ?></a>
</li>
</ul>
<?php endforeach; ?>
</li>
<?php endforeach; ?>
</ul>
</li>
</ul>
<?php } ?>
<?php
}
}
?>

相关内容

  • 没有找到相关文章

最新更新