I'm writing a nav menu and got stuck.
Basically I have a foreach loop that runs some logic if a page id is 0. This logic runs another foreach loop that checks whether there are any subpages to the current page.If so, it define a third foreach loop to check whether there are any subsubpages to the current subpage.
The problem I'm having is that, I need the second and third foreach loops to loop back around and around to check for sub-sub-sub-pages and so on. So I have written the variables in the third foreach loop to be the same as the first foreach loop with the intention of putting the second and third foreach loops into a function that is then inserted into the function buildMenu(); where needed and that then calls itself later inside the function so that it loops over and over.
However it seems that by running through the third foreach loop the value for $b = $childpage['id']; changes so that when the second foreach loop loops back around and tries to see whether $c == the $b from the first foreach loop it no longer does because the value has been changed by the third loop.
So my question is can I maintain the value of $b in the second foreach loop so that when the second loop loops round on itself $b is still what was set by the first foreach loop but then also change the value of $b in the third loop so that the new value is used lower down the looping cycle.
Hopefully that makes sense but the code is below so hopefully you can see what I mean.
If it's not possible and there's a better way I'd be very grateful for it! Thanks
<?php
function buildMenu2($allpages){
echo "<ul>\n";
foreach($allpages as $pageitem) {
$a = $pageitem['parentid'];
$b = $pageitem['id'];
if($a == 0){
echo "<li><a href=" . $pageitem['url'] . ">" . $pageitem['linklabel'] . "</a>";
echo "<ul>\n";
foreach($allpages as $childpage) {
$c = $childpage['parentid'];
$d = $childpage['id'];
if ($c == $b){
echo "<li><a href=" . $childpage['url'] . ">" . $childpage['linklabel'] . "</a>";
echo "<ul>\n";
foreach ($allpages as $childpage) {
$a = $childpage['parentid'];
$b = $childpage['id'];
if ($a == $d) {
echo "<li><a href=" . $childpage['url'] . ">" . $childpage['linklabel'] . "</a>";
echo "</li>";
};
};
echo "</ul>\n";
echo "</li>";
};
};
echo "</ul>\n";
echo "</li></br><br/><br/>";
};
};
echo "</ul>\n";
};
?>