Recursive directory tree, how to set up UL LI

Source: Internet
Author: User
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


      • Music
        • Popular

        • Classic

          • 80 's

          • 90 's





      • Movie

      • Books




    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. = "
        $t
      "; $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.

    • Contact Us

      The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

      If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

      A Free Trial That Lets You Build Big!

      Start building with 50+ products and up to 12 months usage for Elastic Compute Service

      • Sales Support

        1 on 1 presale consultation

      • After-Sales Support

        24/7 Technical Support 6 Free Tickets per Quarter Faster Response

      • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.