Codeigniter视图中的“父-子-孙”类别标记 [英] Parent - Child - Grand Child category Markup in Codeigniter view

查看:58
本文介绍了Codeigniter视图中的“父-子-孙”类别标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试的数据库表,名称是像下面这样的汽车





我在模型中使用递归函数来获取数组中的数据并将其转储。看起来很像

  Array 

[menu] => Array

[0] =>数组

[id] => 1
[name] =>汽车
[parent] => 0
[child] =>数组

[0] =>数组

[id] => 2
[name] =>本田
[父母] => 1
[孩子] =>阵列

[0] =>阵列

[id] => 3
[name] =>汽车
[parent] => 2
[child] =>数组

[0] =>数组

[id] => 4
[name] =>市政
[parent ] => 3
[child] =>数组

[0] =>数组

[id] => 5
[name] => Prosmetic
[parent] => 4













在我看来,我正在创建标准的引导程序



问题:我没有得到所有孩子



我相信我已经在我的查看代码中找到了原因。以下是呈现下拉菜单的代码段

 < 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 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>< tabindex =-1 href =#><?php echo $ menu [$ i] ['name']?>< / a>< / li>
<?php}}?>
< / ul>
< / div>

我只能得到一级孩子,因为我只在检查一级孩子。如何在视图中重复(递归)进行操作。我不能只继续检查孩子的子孩子,依此类推。有人可以给我指出正确的方向吗?



编辑:我的模型

 函数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-&get; getCategoriesByParentId($ data [$ i] ['id ']))
{
$ data [$ i] ['child'] = $ this-> getCategoriesByParentId($ data [$ i] ['id']);
}
}
返回$ data;
}

我的控制器

 公共功能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');
}

我插入了更多类别和子类别。现在,菜单看起来像这样



< img src = https://i.stack.imgur.com/YYzvB.jpg alt =在此处输入图片描述>

解决方案

递归函数可能会有所帮助。可以将其放置在帮助文件中,然后可以从 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.

这篇关于Codeigniter视图中的“父-子-孙”类别标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆