Code for a simple multi-level tree menu

Source: Internet
Author: User
Code for a simple multi-level tree menu

  1. // Tree directory structure template program

  2. // Menu directory field description:
  3. // Menu_id menu Project id
  4. // Menu name
  5. // Menu_grade Level 1: Main Menu 2: Level 2 ........
  6. // Menu_superior top-level menu id

  7. Function my_menu ($ menu_content, $ I, $ menu_grade_temp, $ menu_superior_temp)

  8. {
  9. Global $ PHP_SELF;
  10. $ Temp1 = $ menu_grade_temp + 1;
  11. $ Menu_superior_temp_array = split ("/", $ menu_superior_temp );
  12. For ($ t = 0; $ t <$ I; $ t ++)
  13. {
  14. $ Menu_array = split ("/", $ menu_content [$ t]);
  15. If ($ menu_array [2] ==$ menu_grade_temp) & ($ menu_array [3] ==$ menu_superior_temp_array [$ menu_grade_temp-1])
  16. {
  17. For ($ p = 1; $ p <= $ menu_grade_temp; $ p ++) {echo "";}
  18. $ Temp3 = $ menu_superior_temp_array;
  19. $ Temp3 [$ menu_grade_temp] = $ menu_array [0];
  20. $ Temp2 = implode ("/", $ temp3 );
  21. If ($ menu_array [0] ==$ menu_superior_temp_array [$ temp1-1])
  22. {
  23. $ Temp5 = $ temp1-1;
  24. $ Temp3 [$ menu_grade_temp] = "";
  25. $ Temp6 = implode ("/", $ temp3 );
  26. Echo "$ menu_array [1]
    ";
  27. My_menu ($ menu_content, $ I, $ temp1, $ temp2 );
  28. }
  29. Else
  30. {
  31. $ Temp3 [$ menu_grade_temp + 1] = "";
  32. $ Temp6 = implode ("/", $ temp3 );
  33. Echo "$ menu_array [1]
    ";
  34. }
  35. }
  36. }
  37. }
  38. // Connect to the MySql database
  39. $ Db_host = "localhost ";
  40. $ Db_user = "dkj ";
  41. $ Db_password = "123 ";
  42. $ Db_name = "test ";
  43. Mysql_connect ($ db_host, $ db_user, $ db_password );
  44. Mysql_select_db ($ db_name );

  45. // Obtain data from the database

  46. $ Query_string = "select * from menu order by menu_grade ";
  47. $ Db_data = mysql_query ($ query_string );

  48. // Perform initialization for the first time

  49. If ($ menu_grade_temp = "")
  50. {
  51. $ Menu_superior_temp = 0;
  52. }

  53. // Read all information into the array and count the number of arrays

  54. $ I = 0;
  55. While (list ($ menu_id, $ menu, $ menu_grade, $ menu_superior) = mysql_fetch_row ($ db_data ))
  56. {
  57. $ Menu_content [$ I] = $ menu_id. "/". $ menu. "/". $ menu_grade. "/". $ menu_superior;
  58. $ I ++;
  59. }
  60. My_menu ($ menu_content, $ I, 1, $ menu_superior_temp );

  61. /* Attach the database structure and simulation data

  62. # HOST: localhost database: test
  63. # Data table structure 'menu'
  64. Create table menu (
  65. Menu_id int (11) not null auto_increment,
  66. Menu varchar (20) not null,
  67. Menu_grade int (11) not null,
  68. Menu_superior int (11) not null,
  69. UNIQUE menu_id (menu_id)
  70. );

  71. # Export the following database content 'menu'

  72. Insert into menu VALUES ('1', 'computer ', '1', '0 ');
  73. Insert into menu VALUES ('2', 'programmatically ', '2', '1 ');
  74. Insert into menu VALUES ('3', 'network', '2', '1 ');
  75. Insert into menu VALUES ('4', 'php and mysql', '3', '2 ');
  76. Insert into menu VALUES ('5', 'c language ', '3', '2 ');
  77. Insert into menu VALUES ('6', 'webpage maker ', '3', '3 ');
  78. Insert into menu VALUES ('7', 'tcp, ipprotocol ', '3', '3 ');
  79. Insert into menu VALUES ('8', 'mat', '1', '0 ');
  80. Insert into menu VALUES ('9', 'advanced Mathematics ', '2', '8 ');
  81. Insert into menu VALUES ('10', 'linear algebra ', '3', '9 ');
  82. Insert into menu VALUES ('11', 'Discrete mat', '3', '9 ');
  83. Insert into menu VALUES ('12', 'Elementary mat', '2', '8 ');
  84. Insert into menu VALUES ('13', 'literary ', '1', '0 ');
  85. Insert into menu VALUES ('14', 'programmer home', '2', '13 ');
  86. Insert into menu VALUES ('15', 'php', '4', '4 ');
  87. Insert into menu VALUES ('16', 'mysql', '4', '4 ');
  88. */
  89. ?>

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.