Recursive directory tree, how to set up UL LI

Source: Internet
Author: User
Recursive directory tree, how to set up UL LI
This post was last edited by xingguangyingying on 2013-04-15 08:18:54

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




    ------Solution--------------------
    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.
  • 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.