PHP recursive infinite class classification and returns an example of an array

Source: Internet
Author: User
Tags php and mysql php write
    1. !--? php /**

    2. * Recursive infinite class class return array
    3. * link:bbs.it-home.org
    4. * DATE:2013/2/21
    5. */
    6. $conn = mysql_connect (' localhost ', ' root ', ' 123456 ');
    7. mysql_select_db (' test ');
    8. mysql_query ("Set names ' UTF8 '");

    9. Function getcate ($pid = 0)

    10. {
    11. $sql = "SELECT * from Cate where pid=". $pid;
    12. $res = mysql_query ($sql);
    13. if ($res)
    14. {
    15. while ($row = Mysql_fetch_assoc ($res)) {
    16. $categories [] = $row;
    17. }
    18. }

    19. if (0 < count ($categories))

    20. {
    21. for ($i = 0; $i < count ($c Ategories); $i + +)
    22. {
    23. $categories [$i] [' child '] = Getcate ($categories [$i] [' id ']);
    24. }

    25. }

    26. Return $categories;

    27. }

    28. //Call

    29. $cate = getcate (0);
    30. ?

Copy CodeYou may be interested in the article: PHP infinite class recursive function PHP implementation infinite level cache class extension code PHP with JS made infinitely scalable menu PHP and MySQL implementation of the Infinite class class PHP to the Infinite class classification generation array of classes to the infinite class of ecshop in the analysis PHP write infinite level Select Tree class PHP uses recursive method to implement infinite class classification code
  • 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.