CodeIgniter создает n-уровневую глубокую навигацию
Пожалуйста, мне нужна ваша помощь. Я создал динамическую навигационную панель меню, которая отображает пункты меню в соответствии с порядком, в котором я их установил. Я использую этот встроенный плагин для заказа пунктов моего меню, но в настоящее время в моем меню всего 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
- Пункт 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
)
)
2-й случай: с n-уровнями: Я попытался установить пункты меню в таком порядке:
- Пункт 1
- Пункт 2
- Пункт 5
- Пункт 4
- Пункт 5
- Пункт 3
И результат выглядит так:
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
)
)
)
)
Это так где я получаю ошибку и не работаю. Ошибки, которые я получаю, следующие:
Сообщение: Неопределенный индекс: идентификатор страницы Сообщение: Неопределенный индекс: 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 answers
Учитывая выходные данные базы данных, кажется, что элементы хранятся в базе данных правильно. И проблема относится к методу get_menu()
и его алгоритму для создания выходных данных.
Чтобы создать n-уровневое глубокое меню, вы должны рекурсивно перебирать пункты.
Вот так:
function prepareList(array $items, $pid = 0)
{
$output = array();
# loop through the items
foreach ($items as $item) {
# Whether the parent_id of the item matches the current $pid
if ((int) $item['parent_id'] == $pid) {
# Call the function recursively, use the item's id as the parent's id
# The function returns the list of children or an empty array()
if ($children = prepareList($items, $item['id'])) {
# Store all children of the current item
$item['children'] = $children;
}
# Fill the output
$output[] = $item;
}
}
return $output;
}
Вы можете использовать приведенную выше логику в качестве вспомогательной функции (в CodeIgniter) или частного метода в вашем классе контроллера.
Затем вызовите эту функцию/метод внутри метода get_menu()
следующим образом:
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();
return prepareList($menu_items);
}
Примечание: Я использовал prepareList()
в качестве вспомогательной (глобальной) функции. Если вы решите использовать это в качестве частного метода, вам следует заменить имя функции на $this->prepareList()
везде (даже внутри самой функции).
Вот Онлайн-демонстрация.