我已经通过 oop php 创建了一个菜单和子菜单,但 foreach 循环有问题。我想要实现的目标是让子菜单数组仅在菜单数组的第三个元素中循环!我希望我描述得很好!
这是 foreach 代码:
<ul id="navigation">
<?php
$HeaderMenuOBJ = new Headermenu();
$HeaderMenu = $HeaderMenuOBJ->getHeaderMenu();
$SubHeaderMenu = $HeaderMenuOBJ->getHeaderSubMenu();
//var_dump($SubHeaderMenu);
//var_dump($HeaderMenu);
foreach ($HeaderMenu as $row){
?>
<li>
<a class="active"
href="
<?php
echo $row['href'];
?>
">
<?php echo($row['content']);?>
</a>
<ul class="submenu">
<?php
foreach ($SubHeaderMenu as $row){
?>
<li>
<a
href="
<?php
echo $row['href'];
?>
">
<?php
echo $row['content'];
?>
</a>
</li>
<?php
}
?>
</ul>
</li>
<?php
}
?>
</ul>
这是我的 DOM(一种从数据库读取数据的控制器):
class Headermenu extends dbh {
public $main_query = "SELECT * FROM `header_menu` WHERE parent is null";
public $sub_query = "SELECT * FROM `header_menu` WHERE parent = 3";
public function getHeaderMenu(){
$menu = parent::connect()->query($this->main_query);
while($row = $menu->fetch_array()){
$rows[] = $row;
/*$submenu = parent::connect()->query(str_replace("@param",$header->PK_ID,$this->sub_query));
while($sub_header = $submenu-> fetch_array()){
$header->subheaders[]=$sub_header;
}*/
//$headers[] = $header;
}
return $rows;
}
倚天杖