Home  >  Article  >  Backend Development  >  Code for a simple multi-level tree menu

Code for a simple multi-level tree menu

WBOY
WBOYOriginal
2016-07-25 09:00:43752browse
  1. //Tree directory structure template program

  2. //Menu directory library field description:
  3. //menu_id menu item id
  4. //menu menu name
  5. //menu_grade menu level 1 is the main menu 2 is the secondary menu...
  6. //menu_superior The id number of the previous menu

  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. // Connect to MySql database
  38. $db_host="localhost";
  39. $db_user="dkj";
  40. $db_password="123";
  41. $db_name="test";
  42. mysql_connect($db_host,$db_user,$db_password);
  43. mysql_select_db($db_name);

  44. //Get data from the database

  45. $query_string="select * from menu order by menu_grade";
  46. $db_data=mysql_query($query_string);

  47. //First execution of initialization

  48. if ($menu_grade_temp=="")
  49. {
  50. $menu_superior_temp=0;
  51. }
  52. //Read all the information into the array and count the number of arrays

  53. $i=0;
  54. while (list($menu_id,$menu,$menu_grade,$menu_superior)=mysql_fetch_row($db_data ))
  55. {
  56. $menu_content[$i]=$menu_id."/".$menu."/".$menu_grade."/".$menu_superior;
  57. $i++;
  58. }
  59. my_menu($menu_content,$i ,1,$menu_superior_temp);

  60. /* Attached database structure and simulation data

  61. # Host: localhost Database: test
  62. # Data table structure 'menu'
  63. CREATE TABLE menu (
  64. menu_id int (11) NOT NULL auto_increment,
  65. menu varchar(20) NOT NULL,
  66. menu_grade int(11) NOT NULL,
  67. menu_superior int(11) NOT NULL,
  68. UNIQUE menu_id (menu_id)
  69. );

  70. < ;p># Export the following database content 'menu'
  71. INSERT INTO menu VALUES( '1', 'Computer', '1', '0');
  72. INSERT INTO menu VALUES( '2', 'Programming', ' 2', '1');
  73. INSERT INTO menu VALUES( '3', 'Network', '2', '1');
  74. INSERT INTO menu VALUES( '4', 'PHP and MySql', '3' , '2');
  75. INSERT INTO menu VALUES( '5', 'C language', '3', '2');
  76. INSERT INTO menu VALUES( '6', 'Web page production', '3', ' 3');
  77. INSERT INTO menu VALUES( '7', 'TCP, IP protocol', '3', '3');
  78. INSERT INTO menu VALUES( '8', 'Math', '1', '0 ');
  79. INSERT INTO menu VALUES( '9', 'Advanced Mathematics', '2', '8');
  80. INSERT INTO menu VALUES( '10', 'Linear Algebra', '3', '9') ;
  81. INSERT INTO menu VALUES( '11', 'Discrete Mathematics', '3', '9');
  82. INSERT INTO menu VALUES( '12', 'Elementary Mathematics', '2', '8');
  83. INSERT INTO menu VALUES( '13', 'Literature', '1', '0');
  84. INSERT INTO menu VALUES( '14', 'Programmer's Home', '2', '13');
  85. INSERT INTO menu VALUES( '15', 'php', '4', '4');
  86. INSERT INTO menu VALUES( '16', 'mysql', '4', '4');
  87. */
  88. ?> ;

Copy code


Statement:
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