I have been messing around with recursion a few hours and just cant get it right!
I want my menu structure to be:
<ul>
<li> menu 1 <ul>
<li> menu 1.1 <ul>
<li> menu 1.1.1 </li>
</ul>
</ul>
<li> menu 2 </li>
<li> menu 3 <ul>
<li> menu 3.1 </li>
</ul>
<li> menu 4 </li>
</ul>
</li>
but i just cant get it right.
print '<ul>';
display_children(0, 0);
function display_children($parent, $level) {
// retrieve all children of $parent
$result = mysql_query('SELECT * FROM kategori WHERE parent="'.$parent.'";');
while ($row = mysql_fetch_array($result)) {
if($row['parent']!=0)
print '<ul>';
print '<li>';
// indent and display the title of this child
echo $row['navn']."
";
// call this function again to display this
// child's children
display_children($row['id'], $level+1);
}
print '</li>';
print '</ul>';
as it is now, it prints the right structure but poops out a crapton of missplaced <ul>
and <il>
my top level menu items have 0 as parent (to indicate that they are the absolut top) and its children have their respective parents id as parent attribut.
i tried to check if the current run through of the function (lets say we were at menu 1.1.1) had parent 0 which, to my surprise, was true as i would have thought the current run through would have have parent "menu 1.1" instead of "menu 1"
Any one able to help me out here? I have been staring at that damn code for waaay to long and if you have a quick fix to how i can get this code snippet to insert UL and IL the right places i would be most grateful!
Thanks in advance, Rasmus