我将下拉菜单显示为2级,如下所示:
<nav id= "main-menu">
<?php foreach($data['mainMenu'] as $row) // $data['mainMenu'] is a table MainMenu in database.
{
if($row['parent']<=0){
?>
<dl class ="menudropdown">
<!-- show parent menu-->
<dt id="<?php echo $row['shortName'].'-ddheader'; ?>" onmouseover="ddMenu('<?php echo $row['shortName'];?>',1)" onmouseout="ddMenu('<?php echo $row['shortName'];?>',-1)"><?php echo $row['fullName'];?> </dt>
<!--/parent menu-->
<!--show childmenu-->
<dd id="<?php echo $row['shortName'].'-ddcontent';?>" onmouseover="cancelHide('<?php echo $row['shortName'];?>')" onmouseout="ddMenu('<?php echo $row['shortName'];?>',-1)">
<ul>
<?php foreach($data['mainMenu'] as $row1)
{
if($row1['parent']==$row['id']){
?>
<li><a href ="#" class ="underline"> <?php echo $row1['fullName'];?></a></li>
<?php
}
}
?>
</ul>
</dd>
<!--show childmenu-->
</dl>
<?php
}
}?>
</nav>
结果只显示row['fullName']
中的第一个$data['mainMenu']
。它只显示一个dl
元素。第一个foreach不会继续。
[UPDATE]
如果我将$data['mainMenu']
转换为$tblMainMenu
数组:
$tblMainMenu=[];
$index = 0;
foreach( $data['mainMenu'] as $tbl)
{
$tblMainMenu[$index] = array(
'id' => $tbl['id'],
'fullName' => $tbl['fullName'],
'shortName' => $tbl['shortName'],
'parent' => $tbl['parent'],
'active' => $tbl['active'],
);
$index++;
}
尝试与$tblMainMenu
循环,它完美无缺。
2 foreach有什么问题? php如何执行代码? 我看不出上面这段代码有什么问题。