Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CodeIgniter创建n级深度导航_Php_Codeigniter_Navigation_Nested_Jquery Ui Sortable - Fatal编程技术网

Php CodeIgniter创建n级深度导航

Php CodeIgniter创建n级深度导航,php,codeigniter,navigation,nested,jquery-ui-sortable,Php,Codeigniter,Navigation,Nested,Jquery Ui Sortable,我想要一些帮助。我创建了一个动态菜单导航栏,根据我设置的顺序显示菜单项。我正在使用这个插件来订购我的菜单项,但目前我的菜单只有两个级别,所以基本上是这样的: Item1 Item2 > Subitem2.1 > Subitem2.2 Item3 etc etc. Item1 Item2 > Subitem2.1 >> Subitem2.1.1 > Subitem2.2 Item3 etc etc. Array ( [1] =&g

我想要一些帮助。我创建了一个动态菜单导航栏,根据我设置的顺序显示菜单项。我正在使用这个插件来订购我的菜单项,但目前我的菜单只有两个级别,所以基本上是这样的:

Item1
Item2
 > Subitem2.1
 > Subitem2.2
Item3
etc etc.
Item1
Item2
  > Subitem2.1
    >> Subitem2.1.1
  > Subitem2.2
Item3
etc etc.
Array
(
    [1] => Array
        (
            [id] => 1
            [menu_item] => Item1 
            [parent_id] => 0
        )

    [2] => Array
        (
            [id] => 2
            [menu_item] => Item2
            [parent_id] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [menu_item] => Item5
                            [parent_id] => 2
                        )

                )

        )

    [3] => Array
        (
            [id] => 3
            [menu_item] => Item3
            [parent_id] => 0
        )

    [4] => Array
        (
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [menu_item] => Item4
                            [parent_id] => 4
                        )

                )

        )

)
我想做的是使用n个级别,基本上是这样的:

Item1
Item2
 > Subitem2.1
 > Subitem2.2
Item3
etc etc.
Item1
Item2
  > Subitem2.1
    >> Subitem2.1.1
  > Subitem2.2
Item3
etc etc.
Array
(
    [1] => Array
        (
            [id] => 1
            [menu_item] => Item1 
            [parent_id] => 0
        )

    [2] => Array
        (
            [id] => 2
            [menu_item] => Item2
            [parent_id] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [menu_item] => Item5
                            [parent_id] => 2
                        )

                )

        )

    [3] => Array
        (
            [id] => 3
            [menu_item] => Item3
            [parent_id] => 0
        )

    [4] => Array
        (
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [menu_item] => Item4
                            [parent_id] => 4
                        )

                )

        )

)
每件物品都可以达到n级深度。问题是,如果我为菜单项设置了超过2级的新顺序,则会出现错误,并且该顺序不会存储在数据库中。我怎样才能解决这个问题

数据库结构如下所示:

table: Menu
id (pk)
menu_item
parent_id // it is the id of the parent menu item
order
以下是我的主要(模型)功能:

// save the order of the menu items
public function save_order($items){
    if (count($items)>0) {
        foreach ($items as $order => $item) {
            if ($item['item_id'] != '') {

                $data = array(
                    'parent_id' => (int)$item['parent_id'], 
                    'order'     => $order
                );

                $this->db->set($data)
                ->where($this->_primary_key, $item['item_id'])
                ->update($this->_table_name);
            }
        }
    }
}

// fetch the menu items (parents & children) from the last order set
public function get_menu(){

    $this->db->select('id, menu_item, parent_id');
    $this->db->order_by('parent_id, order');
    $menu_items = $this->db->get('menu')->result_array();

    $arr = array();
    foreach ($menu_items as $item) {

        // the item has no parent
        if (!$item['parent_id']) {
            $arr[$item['id']] = $item; // e.g. $arr(4 => array())
        } // the item is a child
        else {
            // e.g. $arr(4 => array('children' => array()))
            $arr[$item['parent_id']]['children'][] = $item;
        }
    }
    return $arr;
 } 
更新 额外帮助:在这两种情况下,我都进行了测试并在屏幕上转储了项目数组:

第1种情况:有两个级别(目前是这样):我按此顺序设置项目

  • 项目1
  • 项目2
    • 项目4
  • 项目3
  • 项目5
结果如下所示,正如预期的那样:

Array
(
    [1] => Array
        (
            [id] => 1
            [menu_item] => Item1
            [parent_id] => 0
        )

    [2] => Array
        (
            [id] => 2
            [menu_item] => Item2
            [parent_id] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [menu_item] => Item4
                            [parent_id] => 2
                        )

                )

        )

    [3] => Array
        (
            [id] => 3
            [menu_item] => Item3
            [parent_id] => 0
        )

    [5] => Array
        (
            [id] => 5
            [menu_item] => Item5
            [parent_id] => 0
        )

)
第二种情况:带n级:我尝试按以下顺序设置菜单项:

  • 项目1
  • 项目2
    • 项目5
      • 项目4
  • 项目3
结果如下所示:

Item1
Item2
 > Subitem2.1
 > Subitem2.2
Item3
etc etc.
Item1
Item2
  > Subitem2.1
    >> Subitem2.1.1
  > Subitem2.2
Item3
etc etc.
Array
(
    [1] => Array
        (
            [id] => 1
            [menu_item] => Item1 
            [parent_id] => 0
        )

    [2] => Array
        (
            [id] => 2
            [menu_item] => Item2
            [parent_id] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [menu_item] => Item5
                            [parent_id] => 2
                        )

                )

        )

    [3] => Array
        (
            [id] => 3
            [menu_item] => Item3
            [parent_id] => 0
        )

    [4] => Array
        (
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [menu_item] => Item4
                            [parent_id] => 4
                        )

                )

        )

)
在这种情况下,我得到了错误并且无法工作。我得到的错误是:

消息:未定义索引:第页\u id 消息:未定义索引:菜单项

在我的视图文件中:

function nav($menu_items, $child = false){
    $output = '';

    if (count($array)) {
        $output .= ($child === false) ? '<ol class="sortable">' : '<ol>' ;

        foreach ($menu_items as $item) {
            $output .= '<li id="list_' . $item['id'] . '">'; // here is the line of the 1st error
            $output .= '<div>' . $item['menu_item'] . '</div>'; // 2nd error

            //check if there are any children
            if (isset($item['children']) && count($item['children'])) {
                $output .= nav($item['children'], true);
            }
            $output .= '</li>';
        }
        $output .= '</ol>';
    }
    return $output;
}


echo nav($menu_items); 
函数导航($menu\u项,$child=false){
$output='';
if(计数($array)){
$output.=($child===false)?“”;
foreach($菜单项作为$项){
$output.='
  • ;//这是第一个错误的行 $output.=''.$item['menu_item'].';//第二个错误 //检查是否有孩子 if(设置($item['children'])和计数($item['children'])){ $output.=nav($item['children',true); } $output.='
  • '; } $output.=''; } 返回$output; } 回声导航(菜单项);
    考虑到数据库输出,项目似乎正确地存储在数据库中。这个问题属于
    get\u menu()
    方法及其创建输出的算法

    为了创建一个n-level深度菜单,您应该递归地遍历这些项

    我们开始:

    函数准备列表(数组$items,$pid=0)
    {
    $output=array();
    #循环浏览项目
    foreach($items作为$item){
    #项目的父项id是否与当前的$pid匹配
    如果((int)$item['parent_id']==$pid){
    #递归调用函数,使用项的id作为父项的id
    #函数返回子元素列表或空数组()
    如果($children=prepareList($items,$item['id'])){
    #存储当前项的所有子项
    $item['children']=$children;
    }
    #填充输出
    $output[]=$item;
    }
    }
    返回$output;
    }
    
    您可以将上述逻辑用作控制器类中的助手函数(在CodeIgniter中)或私有方法

    然后在
    get_menu()
    方法中调用该函数/方法,如下所示:

    公共功能获取菜单()
    {
    $this->db->select('id,菜单项,父项id');
    $this->db->order_by('parent_id,order');
    $menu_items=$this->db->get('menu')->result_array();
    返回准备列表(菜单项);
    }
    
    注意:我使用了
    prepareList()
    作为助手(全局)函数。如果您决定将其作为私有方法使用,则应将函数名替换为
    $this->prepareList()
    (甚至在函数本身内部)


    这里是

    我得到一个错误
    什么样的错误?
    $items
    看起来像什么(在
    保存订单()
    函数中)?我更新了帖子。请review@Lykos我不明白你想说什么。另外,我在代码中找不到
    page\u id
    变量,第二种情况下的数组不统一(第四个索引)。我找到了这个库(),它可以处理嵌套集,但我没有找到任何关于如何使用它的文档。您是否使用过这个或其他类似的库,是否有任何帮助/教程或用户指南???@Lykos没有,对不起。我不再使用CodeIgniter了。