function menu($parent_id = 0) {
$db = array( // Это якобы таблица категорий
"0" => array(
'id' => 1,
'name' => 'Кошки',
'parent_id' => '0',
),
"1" => array(
'id' => 2,
'name' => 'Собаки',
'parent_id' => '0',
),
"2" => array(
'id' => 3,
'name' => 'Грызуны',
'parent_id' => '0',
),
"3" => array(
'id' => 4,
'name' => 'Домики для кошек',
'parent_id' => '1',
),
"4" => array(
'id' => 5,
'name' => 'Кошачье штырилово',
'parent_id' => '1',
),
"5" => array(
'id' => 6,
'name' => 'Ништяки собакам',
'parent_id' => '2',
),
);
// Это эквивалент запроса к БД SELECT * FROM Categories WHERE Parent_ID = '". $parent_id ."'
$result = array();
foreach($db as $item) {
if($item['parent_id'] == $parent_id) {
$result[] = $item;
}
}
// Построение дерева
if( ! empty($result)) {
foreach($result as & $item) {
$temp = menu($item['id']);
$item['childrens'] = $temp['result'];
}
}
return Array('result' => $result);
}
echo '<pre>';print_r(menu()['result']); echo '</pre>';
Array
(
[0] => Array
(
[id] => 1
[name] => Кошки
[parent_id] => 0
[childrens] => Array
(
[0] => Array
(
[id] => 4
[name] => Домики для кошек
[parent_id] => 1
[childrens] => Array
(
)
)
[1] => Array
(
[id] => 5
[name] => Кошачье штырилово
[parent_id] => 1
[childrens] => Array
(
)
)
)
)
[1] => Array
(
[id] => 2
[name] => Собаки
[parent_id] => 0
[childrens] => Array
(
[0] => Array
(
[id] => 6
[name] => Ништяки собакам
[parent_id] => 2
[childrens] => Array
(
)
)
)
)
[2] => Array
(
[id] => 3
[name] => Грызуны
[parent_id] => 0
[childrens] => Array
(
)
)
)
$(".lazy").Lazy({
beforeLoad: function(element) {
$(element).addClass('loader');
},
afterLoad: function(element) {
$(element).removeClass('loader');
},
threshold: 1000,
effect: "fadeIn",
effectTime: 2000,
});