CodeIgniter создать n-уровень глубокой навигации

Мне нужна помощь, пожалуйста. Я создал динамически панель навигации по меню, которая отображает пункты меню в соответствии с установленным ими порядком. Я использую этоnestedsortable плагин, чтобы заказать пункты меню, но в настоящее время мое меню имеет только 2 уровня, поэтому в основном это выглядит так:

Item1
Item2
 > Subitem2.1
 > Subitem2.2
Item3
etc etc.

То, что я хотел бы сделать, это сделать это с n-уровнями, так что примерно так:

Item1
Item2
  > Subitem2.1
    >> Subitem2.1.1
  > Subitem2.2
Item3
etc etc.

и каждый предмет может пройти 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-й случай: с 2 уровнями (как в настоящее время): я устанавливаю элементы с этим порядком

Элемент1Элемент2Item4Item3Item5

и результат выглядит так, как и ожидалось:

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
        )

)

2-й случай: с n-уровнями: я попытался установить пункты меню в следующем порядке:

Элемент1Элемент2Item5Item4Item3

и результат выглядит так:

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
                        )

                )

        )

)

Это тот случай, когда я получаю ошибку и не работаю. Я получаю следующие ошибки:

Сообщение: неопределенный индекс: page_id Сообщение: неопределенный индекс: menu_item

в моем представлении файла:

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

Ответы на вопрос(1)

Ваш ответ на вопрос