且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Codeigniter视图中的“父-子-孙”类别标记

更新时间:2023-08-25 09:16:40

递归函数可能会有所帮助。可以将其放置在帮助文件中,然后可以从 printMenu($ menu);



这样的视图中调用它。

示例更新:
如果您需要排除数组中的第一个元素,因为您似乎希望将其标识为父元素,则可以执行以下操作:



HELPER:(例如 menu_helper.php 放在文件夹帮助器中)> p>

 函数printMenu($ a){

if(!is_array($ a)){
返回
}

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

查看:

 < div class = dropdown> 
< button class = btn btn-default dropdown-toggle type = button data-toggle = dropdown> Menu< span class = caret>< / span>< /按钮>
< 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}其他{?>
< li>< tabindex =-1 href =#><?php echo $ menu [0] ['name']?>< / a>< / li>
<?php}?>
< / ul>
< / div>

CONTROLLER

 公共功能index()
{
$ this-> load-> helper('menu'); //我们的新帮助程序文件
$ this-> load-> model(’Test_model’);
$ data ['menu'] = $ this-> Test_model-> getCategoriesByParentId(0);
$ data [‘has_children’] = 1; //使用查询或功能来进行替换,以检查菜单中是否存在儿童
$ this-> load-> view('dropdown',$ data);
}

以上示例仅适用于具有一个单亲的数组。如果需要多个父母,则必须为每个父母调用该函数。这不是***选择,如果您需要对Codeigniter中具有多个深度的菜单进行更高级的处理,则可以检查此项目中的库: https://github.com/edomaru/codeigniter_multilevel_menu



该库一段时间未更新,我不知道它是否与CI 3,但是也许您可以通过检查源代码来了解一些方法。他们甚至在那里有一个Bootstrap 3示例。


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

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

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

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.