I show my dropdown menu with 2 level as the following:
<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>
A result is only the first row['fullName']
in $data['mainMenu']
Which is shown. It shows only one dl
element. The first foreach doesn't continue.
[UPDATE]
If I convert $data['mainMenu']
to $tblMainMenu
array:
$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++;
}
And try to loop with $tblMainMenu
, It works perfectly.
What is problem with 2 foreach? How php execute the code? I cannot see what is problem with this code above.