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

时间:2018-08-04 04:30:48

标签: php codeigniter bootstrap-4

你好,我需要设计方面的帮助,我对设计没有任何想法,我已经在codeigniter中创建了动态菜单,我想在treeview中设置该菜单。 打开子菜单后先单击父菜单,那么如何实现它对我有帮助

这是我的查看代码:

<?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
        }
    }
?>

2 个答案:

答案 0 :(得分:0)

您需要使用parent_id=0获取所有类别,然后获取subCategory。尝试以下类似的方法。

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);
    }

答案 1 :(得分:0)

在这里,我在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
        }
    }
    ?>