PHP recursively generates tree-like instance code

Source: Internet
Author: User

This article introduces the example code for Recursive Generation of tree lines in PHP.

  1. <? Php
  2. Header ("content-type: text/html; charset = UTF-8 ");
  3. $ Connect = mysql_connect (localhost, root, 123456 );
  4. Mysql_select_db ("wz ");
  5. Mysql_query ("set names utf8 ");
  6. // Obtain the top node
  7. $ SQL = "select id, name, ischild from tree where parent = 0 order by id asc ";
  8. $ Result = mysql_query ($ SQL );
  9. While ($ row = mysql_fetch_array ($ result ))
  10. {
  11. Extract ($ row );
  12. // If a subnode exists, add an event to it to expand or close the subnode.
  13. $ Icon = $ ischild? "<A href = javascript.: expand (" div $ id ");> </a> ":"-";
  14. $ Name = $ icon. $ name;
  15. Echo "<div id = div". $ id. ">". $ name;
  16. If ($ ischild)
  17. {
  18. // Recursively retrieve nodes
  19. GetNode ($ id, 0 );
  20. }
  21. Echo "</div> ";
  22. }
  23. Function getNode ($ id, $ level)
  24. {
  25. $ SQL = "select id, name, ischild from tree where parent = $ id order by id asc ";
  26. $ Result = mysql_query ($ SQL );
  27. $ Level;
  28. While ($ row = mysql_fetch_array ($ result ))
  29. {
  30. Extract ($ row );
  31. $ Icon = $ ischild? "<A href = javascript.: expand (" div $ id ");> </a> ":"-";
  32. $ Name = $ icon. $ name;
  33. Echo "<div id = div". $ id. ">". echoChar ("", $ level). $ name;
  34. If ($ ischild)
  35. {
  36. GetNode ($ id, $ level );
  37. }
  38. Echo "</div> ";
  39. }
  40. }
  41. Function echoChar ($ char, $ num)
  42. {
  43. For ($ I = 0; $ I <$ num; $ I)
  44. {
  45. $ StrChar. = $ char;
  46. }
  47. Return $ strChar;
  48. }
  49. ?>
  50. <Script language = "javascript">
  51. Function expand (id)
  52. {
  53. Var obj = document. getElementById (id). childNodes;
  54. For (var I = 0; I <obj. length; I)
  55. {
  56. If (obj [I]. nodeName = "DIV ")
  57. {
  58. Switch (obj [I]. style. display)
  59. {
  60. Case "":
  61. Case "block ":
  62. Obj [I]. style. display = "none ";
  63. Break;
  64. Case "none ":
  65. Obj [I]. style. display = "block ";
  66. Break;
  67. }
  68. }
  69. }
  70. }
  71. </Script>

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.