Home > Backend Development > PHP Tutorial > PHP uses recursive method to implement infinite classification code

PHP uses recursive method to implement infinite classification code

WBOY
Release: 2016-07-25 09:03:27
Original
826 people have browsed it
  1. $dbhost = "localhost"; // Database host name
  2. $dbuser = "root"; // Database user name
  3. $dbpd = "123456"; // Database password
  4. $dbname = "test"; //Database name
  5. mysql_connect($dbhost,$dbuser,$dbpd); //Connect to host
  6. mysql_select_db($dbname); //Select database
  7. mysql_query("SET NAMES 'utf8'");
  8. display_tree("├",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. //Indent the node name
  18. echo $tag.$row['classname'] . "
    ";
  19. //Call this again The function displays the child nodes of the child node
  20. display_tree($tag."─┴",$row['id']);
  21. }
  22. }
  23. ?>
Copy code

Remarks:Use recursion This method will be inefficient for a large number of sub-columns. Some mature CMS systems use the production array method to improve efficiency.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template