Function nav ($parent) {
$sql = mysql_query ("Select * from menu where parent = ' $parent '");
while ($row = Mysql_fetch_array ($sql)) {
Echo '
'. $row [' name ']. ';
Nav ($row [' id ']);
Echo '';
}
}
How to set the sub-class UL output it?
Like this
Reply to discussion (solution)
Function nav ($parent) { $sql = mysql_query ("Select * from menu where parent = ' $parent '"); while ($row = Mysql_fetch_array ($sql)) { echo '
'. $row [' name ']. '; Echo '
; Nav ($row [' id ']); Echo '
; Echo ''; }}
Since you are outputting directly in the NAV function, there is no child node, which produces an empty
But it doesn't affect the display effect.
It's going to affect me. The front end is the folding menu when UL display is block, it's a hassle.
On the internet to find a night Chinese English test countless there is no perfect or even database encapsulation into class can not be used or UL nested error
Either traverse n times or hundreds of lines of code look clumsy
Do not know how to solve the Daniel, there is no perfect way?
Please share your usual code with the moderator
You query the side output, so you can not know whether the current node has child nodes (because it has not been read)
So you need to read the query results to the array http://bbs.csdn.net/topics/390364669
And then recursively output
You can also cache what you want to output with a variable, and then output it when the recursion ends
Function nav ($parent) { $res = '; $sql = mysql_query ("Select * from menu where parent = ' $parent '"); while ($row = Mysql_fetch_array ($sql)) { $res. = '
'. $row [' name ']. '; $t = Nav ($row [' id ']); if (! empty ($t)) $res. = "
"; $res. = ''; } return $res;}
When called
Echo nav ($id);
I have been dynamically loaded with Ajax, so there is no recursion.
At least not for you at the moment.
Thank you very much, I'll study it slowly.