Im working on a prototype, and would like to build a multi level navigation – however not by looping through an array. I have a $depth and a $children, which should determine the depth of the navigation and the number of children on each level. So $depth = 4, $children = 8 would yield 4096 menu items.
This is a snippet af the output I would like:
<ul>
<li class="level-1">
<a href="#">Subject 1</a>
<ul>
<li class="level-2">
<a href="#">Subject 1.1</a>
<ul>
<li class="level-3">
<a href="#">Subject 1.1.1</a>
</li>
...
</ul>
</li>
...
</ul>
</li>
...
</ul>
So far I have tried this, but I cant get my head around it 🙁
function draw_list ($depth, $children) {
echo '<ul>';
for ($i = 0; $i < $children; $i++) {
echo '<li>' . ($i++);
$depth--;
if ($depth > 0) {
echo draw_list($depth, $children);
}
echo '</li>';
}
echo '</ul>';
}
Several things required as I see it…
$depth--;needs to be outside of theforloop$itwice, once in theforstatement, and then again on theecho '<li>' . ($i++);The $depth check was stopping one early, so make(Edit, thinking about it, this is an incorrect statement)>=instead of just>That should give you…
Update
For the display of the level numbering, try passing a string value through as a parameter…