无法创建深层菜单
Cannot create deep level menu
我正在尝试创建一个深层菜单,所以我在我的数据库中定义了这个 table 结构:
id | parent | menu_order
15 0 0
22 15 0
26 15 0
30 15 0
47 22 0
49 22 0
51 22 0
68 26 0
69 26 0
我想实现的是生成允许我正确设置 menu_order
列的代码,在上面的示例中,最终结果应该是:
id | parent | menu_order
15 0 0
22 15 1
26 15 5
30 15 8
47 22 2
49 22 3
51 22 4
68 26 6
69 26 7
结果很容易理解,本质上22
有parent
15
,所以menu_order
是1
。同样的概念也适用于 22
的嵌套级别,即 (47,49,51
).
实际上我的代码是这样的:
<?php
$posts = [
['id' => 15, 'parent' => 0, 'menu_order' => 0],
['id' => 22, 'parent' => 15, 'menu_order' => 0],
['id' => 26, 'parent' => 15, 'menu_order' => 0],
['id' => 30, 'parent' => 15, 'menu_order' => 0],
['id' => 47, 'parent' => 22, 'menu_order' => 0],
['id' => 49, 'parent' => 22, 'menu_order' => 0],
['id' => 51, 'parent' => 22, 'menu_order' => 0],
['id' => 68, 'parent' => 26, 'menu_order' => 0],
['id' => 69, 'parent' => 26, 'menu_order' => 0],
];
$currOrder = -1;
foreach ($posts as $key => $p) {
$currOrder++;
$test[] = [
'id' => $p['id'],
'parent' => $p['parent'],
'menu_order' => $currOrder
];
hasChild($p['id'], $currOrder, $test);
}
function hasChild($postId, &$currOrder, $test)
{
// get childs post
$childs = $this->where('parent', $postId)
->get()
->getResult();
foreach ($childs as $c) {
$currOrder++;
$test[] = [
'id' => $c['id'],
'parent' => $c['parent'],
'menu_order' => $currOrder
];
// check nested levels
if ($c->parent != 0) {
hasChild($c['id'], $currOrder, $test);
}
}
}
问题是我得到了嵌套级别的重复帖子,menu_order
完全搞砸了。我想我的逻辑过于复杂了,有人可以帮我实现这个吗?
谢谢
有几种方法可以处理递归函数。我就是这样做的。
<?php
// example code
$posts = [
['id' => 15, 'parent' => 0, 'menu_order' => 0],
['id' => 22, 'parent' => 15, 'menu_order' => 0],
['id' => 26, 'parent' => 15, 'menu_order' => 0],
['id' => 30, 'parent' => 15, 'menu_order' => 0],
['id' => 47, 'parent' => 22, 'menu_order' => 0],
['id' => 49, 'parent' => 22, 'menu_order' => 0],
['id' => 51, 'parent' => 22, 'menu_order' => 0],
['id' => 68, 'parent' => 26, 'menu_order' => 0],
['id' => 69, 'parent' => 26, 'menu_order' => 0],
];
function recursePosts($pid, $order, $tally, $posts) {
foreach($posts as $p) {
if ($p['parent']===$pid) {
$p['menu_order'] = $tally ;
$order['id'.$p['id']] = $p;
$tally++;
$o = recursePosts($p['id'], $order, $tally, $posts);
$tally = $o[1];
$order = $o[0];
}
}
return [$order, $tally];
}
$o =recursePosts(0, [], 0, $posts);
$order = $o[0];
ksort($order);
print_r(array_values($order));
https://www.tehplayground.com/uNY7Bn0VyDv6JIUS
还有PHP的原生RecursiveIterator
我正在尝试创建一个深层菜单,所以我在我的数据库中定义了这个 table 结构:
id | parent | menu_order
15 0 0
22 15 0
26 15 0
30 15 0
47 22 0
49 22 0
51 22 0
68 26 0
69 26 0
我想实现的是生成允许我正确设置 menu_order
列的代码,在上面的示例中,最终结果应该是:
id | parent | menu_order
15 0 0
22 15 1
26 15 5
30 15 8
47 22 2
49 22 3
51 22 4
68 26 6
69 26 7
结果很容易理解,本质上22
有parent
15
,所以menu_order
是1
。同样的概念也适用于 22
的嵌套级别,即 (47,49,51
).
实际上我的代码是这样的:
<?php
$posts = [
['id' => 15, 'parent' => 0, 'menu_order' => 0],
['id' => 22, 'parent' => 15, 'menu_order' => 0],
['id' => 26, 'parent' => 15, 'menu_order' => 0],
['id' => 30, 'parent' => 15, 'menu_order' => 0],
['id' => 47, 'parent' => 22, 'menu_order' => 0],
['id' => 49, 'parent' => 22, 'menu_order' => 0],
['id' => 51, 'parent' => 22, 'menu_order' => 0],
['id' => 68, 'parent' => 26, 'menu_order' => 0],
['id' => 69, 'parent' => 26, 'menu_order' => 0],
];
$currOrder = -1;
foreach ($posts as $key => $p) {
$currOrder++;
$test[] = [
'id' => $p['id'],
'parent' => $p['parent'],
'menu_order' => $currOrder
];
hasChild($p['id'], $currOrder, $test);
}
function hasChild($postId, &$currOrder, $test)
{
// get childs post
$childs = $this->where('parent', $postId)
->get()
->getResult();
foreach ($childs as $c) {
$currOrder++;
$test[] = [
'id' => $c['id'],
'parent' => $c['parent'],
'menu_order' => $currOrder
];
// check nested levels
if ($c->parent != 0) {
hasChild($c['id'], $currOrder, $test);
}
}
}
问题是我得到了嵌套级别的重复帖子,menu_order
完全搞砸了。我想我的逻辑过于复杂了,有人可以帮我实现这个吗?
谢谢
有几种方法可以处理递归函数。我就是这样做的。
<?php
// example code
$posts = [
['id' => 15, 'parent' => 0, 'menu_order' => 0],
['id' => 22, 'parent' => 15, 'menu_order' => 0],
['id' => 26, 'parent' => 15, 'menu_order' => 0],
['id' => 30, 'parent' => 15, 'menu_order' => 0],
['id' => 47, 'parent' => 22, 'menu_order' => 0],
['id' => 49, 'parent' => 22, 'menu_order' => 0],
['id' => 51, 'parent' => 22, 'menu_order' => 0],
['id' => 68, 'parent' => 26, 'menu_order' => 0],
['id' => 69, 'parent' => 26, 'menu_order' => 0],
];
function recursePosts($pid, $order, $tally, $posts) {
foreach($posts as $p) {
if ($p['parent']===$pid) {
$p['menu_order'] = $tally ;
$order['id'.$p['id']] = $p;
$tally++;
$o = recursePosts($p['id'], $order, $tally, $posts);
$tally = $o[1];
$order = $o[0];
}
}
return [$order, $tally];
}
$o =recursePosts(0, [], 0, $posts);
$order = $o[0];
ksort($order);
print_r(array_values($order));
https://www.tehplayground.com/uNY7Bn0VyDv6JIUS
还有PHP的原生RecursiveIterator