Code for a simple multi-level tree menu

Source: Internet
Author: User
  1. Tree-type directory structure template program

  2. Menu Catalog Library Field Description:
  3. MENU_ID menu item ID
  4. Menu Name
  5. Menu_grade Menu Level 1 Main Menu 2 for level two menu ...
  6. Menu_superior first-level menu ID number

  7. function My_menu ($menu _content, $i, $menu _grade_temp, $menu _superior_temp)

  8. {
  9. Global $PHP _self;
  10. $temp 1= $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. $temp 3= $menu _superior_temp_array;
  19. $temp 3[$menu _grade_temp]= $menu _array[0];
  20. $temp 2=implode ("/", $temp 3);
  21. if ($menu _array[0]== $menu _superior_temp_array[$temp 1-1])
  22. {
  23. $temp 5= $temp 1-1;
  24. $temp 3[$menu _grade_temp]= "";
  25. $temp 6=implode ("/", $temp 3);
  26. echo "$menu _array[1]
    ";
  27. My_menu ($menu _content, $i, $temp 1, $temp 2);
  28. }
  29. Else
  30. {
  31. $temp 3[$menu _grade_temp+1]= "";
  32. $temp 6=implode ("/", $temp 3);
  33. echo "$menu _array[1]
    ";
  34. }
  35. }
  36. }
  37. }
  38. Connect to 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. Getting data from the database

  46. $query _string= "SELECT * from Menu order by Menu_grade";
  47. $db _data=mysql_query ($query _string);

  48. First execution initialization

  49. if ($menu _grade_temp== "")
  50. {
  51. $menu _superior_temp=0;
  52. }

  53. Read all the 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. /* Attached database structure and simulation data

  62. # Host: localhost database: Test
  63. # The structure of the data table ' menu '
  64. CREATE TABLE Menu (
  65. menu_id Int (one) not NULL auto_increment,
  66. Menu varchar (not NULL),
  67. Menu_grade Int (one) is not NULL,
  68. Menu_superior Int (one) is not NULL,
  69. UNIQUE menu_id (menu_id)
  70. );

  71. # Export the following database contents ' menu '

  72. INSERT into Menu VALUES (' 1 ', ' computer ', ' 1 ', ' 0 ');
  73. INSERT into Menu VALUES (' 2 ', ' Programming ', ' 2 ', ' 1 ');
  74. INSERT into Menu VALUES (' 3 ', ' Network ', ' 2 ', ' 1 ');
  75. INSERT into Menu VALUES (' 4 ', ' PHP with MySQL ', ' 3 ', ' 2 ');
  76. INSERT into Menu VALUES (' 5 ', ' C language ', ' 3 ', ' 2 ');
  77. INSERT into Menu VALUES (' 6 ', ' Page maker ', ' 3 ', ' 3 ');
  78. INSERT into Menu VALUES (' 7 ', ' TCP, IP protocol ', ' 3 ', ' 3 ');
  79. INSERT into Menu VALUES (' 8 ', ' Math ', ' 1 ', ' 0 ');
  80. INSERT into Menu VALUES (' 9 ', ' Advanced math ', ' 2 ', ' 8 ');
  81. INSERT into menu VALUES (' 10 ', ' linear Algebra ', ' 3 ', ' 9 ');
  82. INSERT into menu VALUES (' 11 ', ' Discrete Math ', ' 3 ', ' 9 ');
  83. INSERT into Menu VALUES (' 12 ', ' Elementary math ', ' 2 ', ' 8 ');
  84. INSERT into menu VALUES (' 13 ', ' literature ', ' 1 ', ' 0 ');
  85. INSERT into Menu VALUES (' 14 ', ' Home of the Programmer ', ' 2 ', ' 13 ');
  86. INSERT into menu VALUES (' All ', ' php ', ' 4 ', ' 4 ');
  87. INSERT into menu VALUES (' + ', ' MySQL ', ' 4 ', ' 4 ');
  88. */
  89. ?>

Copy Code
  • Related Article

    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.