Php code that uses recursive methods to implement unlimited classification

Source: Internet
Author: User
Php code that uses recursive methods to implement unlimited classification

  1. $ Dbhost = "localhost"; // database host name
  2. $ Dbuser = "root"; // database username
  3. $ Dbpd = "123456"; // database password
  4. $ Dbname = "test"; // database name
  5. Mysql_connect ($ dbhost, $ dbuser, $ dbpd); // connect to the host
  6. Mysql_select_db ($ dbname); // select a database
  7. Mysql_query ("set names 'utf8 '");
  8. Display_tree ("primary", 0 );
  9. Function display_tree ($ tag, $ classid ){
  10. $ Result = mysql_query ("
  11. SELECT *
  12. FROM ylmf_class
  13. WHERE parentid = '". $ classid ."'
  14. ;"
  15. );
  16. While ($ row = mysql_fetch_array ($ result )){
  17. // Display the node name in indentation
  18. Echo $ tag. $ row ['classname']."
    ";
  19. // Call this function again to display the subnode of the subnode
  20. Display_tree ($ tag. "-rows", $ row ['id']);
  21. }
  22. }
  23. ?>

Remarks:The recursive method is used to reduce the efficiency of a large number of sub-columns. some mature cms systems use the method of producing arrays for calling to improve the efficiency.

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.