duanbeng6709 2017-04-08 11:42
浏览 37
已采纳

Parent - Child - Codeigniter视图中的Grand Child类别标记

I have a test Database Table by the name of autos like following

enter image description here

I am using a recursive function in my model to get the data in array and dumped it. It looks perfect like following

Array
(
[menu] => Array
    (
        [0] => Array
            (
                [id] => 1
                [name] => Automobiles
                [parent] => 0
                [child] => Array
                    (
                        [0] => Array
                            (
                                [id] => 2
                                [name] => Honda
                                [parent] => 1
                                [child] => Array
                                    (
                                        [0] => Array
                                            (
                                                [id] => 3
                                                [name] => Cars
                                                [parent] => 2
                                                [child] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [id] => 4
                                                                [name] => Civic
                                                                [parent] => 3
                                                                [child] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [id] => 5
                                                                                [name] => Prosmetic
                                                                                [parent] => 4
                                                                            )

                                                                    )

                                                            )

                                                    )

                                            )

                                    )

                            )

In my view I am creating a standard bootstrap multi-level dropdown but I am not getting all the child menus

enter image description here

Problem : I am not getting all the child

I believe I have found the reason which is in my view code . Following is the code snippet which renders the drop-down

<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Menu<span class="caret"></span></button>
    <ul class="dropdown-menu">
        <?php for($i=0;$i<count($menu);$i++){?>
            <?php if(!empty($menu[$i]['child'])){?>
                <li class="dropdown-submenu">
                    <a class="test" href="#"><?php echo $menu[$i]['name']?> <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <?php for($j=0;$j<count($menu[$i]['child']);$j++){?>
                            <li><a href="#"><?php echo $menu[$i]['child'][$j]['name']?></a></li>
                        <?php }?>
                    </ul>
                </li>
            <?php }else{?>
                <li><a tabindex="-1" href="#"><?php echo $menu[$i]['name']?></a></li>
        <?php }}?>
    </ul>
</div>

I am only able to get First level child because I am only checking for the first level child. How can I do it repeatedly (recursively) in the view. I can't just keep checking for sub-child of a child and so on.. There has to be a way. Can anyone point me to the right direction please?

EDIT: My Model

function getCategoriesByParentId($category_id) {
    $data = $this->db->select('*')->from('autos')->WHERE('parent',$category_id)->get()->result_array();
    for($i=0;$i<count($data);$i++)
    {
        if($this->getCategoriesByParentId($data[$i]['id']))
        {
            $data[$i]['child']=$this->getCategoriesByParentId($data[$i]['id']);
        }
    }
    return $data;
}

My Controller

public function index()
{
    $this->load->model('Test_model');
    $data['menu']=$this->Test_model->getCategoriesByParentId(0);
    //echo '<pre>';print_r(($data));echo '</pre>';exit;

    $data['title']='testing';
    $this->load->view('head',$data);
    $this->load->view('dropdown');
}

I inserted some more categories and subcategories. Now the Menu looks like this

enter image description here

  • 写回答

1条回答 默认 最新

  • duanjia6959 2017-04-08 14:40
    关注

    A recursive function might be of help. It could be placed in a helper file, and then you could call it from the view like printMenu($menu);

    UPDATE WITH EXAMPLE: If you need to exclude the first element in your array, as it seems that you want to identify it as a parent, you could do something like this:

    HELPER: (for example menu_helper.php put in folder helper)

    function printMenu($a) {
    
      if (!is_array($a)) {
        return;
      } 
    
      foreach($a as $m) {         
          if($m['parent'] > 0){
              echo '<li><a tabindex="-1" href="#">'. $m['name'] .'</a></li>';             
          }
    
          if(is_array($m['child'])){
            printMenu($m['child']);
          }
      }
    }
    

    VIEW:

    <div class="dropdown">
    <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Menu<span class="caret"></span></button>
        <ul class="dropdown-menu">
            <?php if($has_children){ ?>
                <li class="dropdown-submenu">
                    <a class="test" href="#"><?php echo $menu[0]['name']?> <span class="caret"></span></a>
                    <ul class="dropdown-menu">                      
                        <?php printMenu($menu); ?>
                    </ul>
                </li>
            <?php } else {?>
                <li><a tabindex="-1" href="#"><?php echo $menu[0]['name']?></a></li>
            <?php } ?>
        </ul>
    </div>
    

    CONTROLLER:

    public function index()
    {
        $this->load->helper('menu'); // OUR NEW HELPER FILE
        $this->load->model('Test_model');
        $data['menu'] = $this->Test_model->getCategoriesByParentId(0);
        $data['has_children'] = 1; //REPLACE WITH A QUERY OR FUNCTION TO CHECK IF CHILDREN EXISTS IN YOU MENU
        $this->load->view('dropdown', $data);
    }
    

    The example above only applies to arrays with one single parent. If you need multiple parents you have to call the function for each parent. THis is not optimal, an if you need more advanced handling of menus with multiple depths in Codeigniter you could check the library in this project: https://github.com/edomaru/codeigniter_multilevel_menu

    The library is not updated for a while and I don't know if its compatible with CI 3, but maybe you could get some ideas of how to go along by checking the source code. They even have a Bootstrap 3 example there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程