1 回答
TA贡献1828条经验 获得超3个赞
直接回答:
首先,您需要在外部和内部 foreach 中使用 2 个不同的变量:foreach ($HeaderMenu as $menuRow){现在foreach ($SubHeaderMenu as $subMenuRow){两者都有$row. 你可以这样做:if ($menuRow['id'] == 3) { foreach ($SubHeaderMenu as $subMenuRow){- 所以将内部 foreach 包含在其中,if并且仅当父 id 为 3 时才执行此操作,但最好获取所有 header_menu 行,将该数据转换为嵌套数组并按动态结构显示。
动态结构答案:
获取所有菜单项,创建菜单数组,每个菜单都会有其子菜单项。然后显示这个就很容易了。我假设只有两个级别 - 菜单和子菜单。
样本数据:
+----+-------+---------------+-----------+
| id | href | content | parent_id |
+----+-------+---------------+-----------+
| 1 | href1 | header1 | NULL |
| 2 | href2 | header2 | NULL |
| 3 | href3 | header3 | NULL |
| 4 | href4 | subheader 1 1 | 1 |
| 5 | href5 | subheader 1 2 | 1 |
| 6 | href6 | subheader 2 1 | 2 |
+----+-------+---------------+-----------+
代码:
<?php
$mysqli = new mysqli("localhost", "zz", "zz", "zz");
// fetch all menu items
$query = 'select * from header_menu order by parent_id';
$result = $mysqli->query($query);
$data = $result->fetch_all(MYSQLI_ASSOC);
var_dump($data);
// build menu with menus and their submenus
$menu = [];
foreach ($data as $row) {
if ($row['parent_id'] === null) {
$menu[$row['id']] = $row;
$menu[$row['id']]['submenus'] = [];
} else {
$menu[$row['parent_id']]['submenus'][] = $row;
}
}
var_dump($menu);
// now display it in html or however you want
foreach ($menu as $item) {
echo $item['content'].PHP_EOL;
foreach ($item['submenus'] as $subitem) {
echo ' '.$subitem['content'].PHP_EOL;
}
}
$数据:
array(6) {
[0]=>
array(4) {
["id"]=>
string(1) "1"
["href"]=>
string(5) "href1"
["content"]=>
string(7) "header1"
["parent_id"]=>
NULL
}
[1]=>
array(4) {
["id"]=>
string(1) "2"
["href"]=>
string(5) "href2"
["content"]=>
string(7) "header2"
["parent_id"]=>
NULL
}
[2]=>
array(4) {
["id"]=>
string(1) "3"
["href"]=>
string(5) "href3"
["content"]=>
string(7) "header3"
["parent_id"]=>
NULL
}
[3]=>
array(4) {
["id"]=>
string(1) "4"
["href"]=>
string(5) "href4"
["content"]=>
string(13) "subheader 1 1"
["parent_id"]=>
string(1) "1"
}
[4]=>
array(4) {
["id"]=>
string(1) "5"
["href"]=>
string(5) "href5"
["content"]=>
string(13) "subheader 1 2"
["parent_id"]=>
string(1) "1"
}
[5]=>
array(4) {
["id"]=>
string(1) "6"
["href"]=>
string(5) "href6"
["content"]=>
string(13) "subheader 2 1"
["parent_id"]=>
string(1) "2"
}
}
$菜单:
array(3) {
[1]=>
array(5) {
["id"]=>
string(1) "1"
["href"]=>
string(5) "href1"
["content"]=>
string(7) "header1"
["parent_id"]=>
NULL
["submenus"]=>
array(2) {
[0]=>
array(4) {
["id"]=>
string(1) "4"
["href"]=>
string(5) "href4"
["content"]=>
string(13) "subheader 1 1"
["parent_id"]=>
string(1) "1"
}
[1]=>
array(4) {
["id"]=>
string(1) "5"
["href"]=>
string(5) "href5"
["content"]=>
string(13) "subheader 1 2"
["parent_id"]=>
string(1) "1"
}
}
}
[2]=>
array(5) {
["id"]=>
string(1) "2"
["href"]=>
string(5) "href2"
["content"]=>
string(7) "header2"
["parent_id"]=>
NULL
["submenus"]=>
array(1) {
[0]=>
array(4) {
["id"]=>
string(1) "6"
["href"]=>
string(5) "href6"
["content"]=>
string(13) "subheader 2 1"
["parent_id"]=>
string(1) "2"
}
}
}
[3]=>
array(5) {
["id"]=>
string(1) "3"
["href"]=>
string(5) "href3"
["content"]=>
string(7) "header3"
["parent_id"]=>
NULL
["submenus"]=>
array(0) {
}
}
}
菜单结构的输出:
header1
subheader 1 1
subheader 1 2
header2
subheader 2 1
header3
现在您可以输出所有菜单项及其子菜单项,而不仅仅是菜单 ID 3。
- 1 回答
- 0 关注
- 110 浏览
添加回答
举报