How to use PHP to implement multi-level tree menu

Source: Internet
Author: User
: This article describes how to use PHP to implement a multi-level tree menu. For more information about PHP tutorials, see. // Tree directory structure template program
// Menu directory field description:
// Menu_id menu Project id
// Menu name
// Menu_grade Level 1: Main Menu 2: Level 2 ........
// Menu_superior top-level menu id

Function my_menu ($ menu_content, $ I, $ menu_grade_temp, $ menu_superior_temp)
{
Global $ php_SELF;
$ Temp1 = $ menu_grade_temp + 1;
$ Menu_superior_temp_array = split ("/", $ menu_superior_temp );
For ($ t = 0; $ t <$ I; $ t ++)
{
$ Menu_array = split ("/", $ menu_content [$ t]);
If ($ menu_array [2] ==$ menu_grade_temp) & ($ menu_array [3] ==$ menu_superior_temp_array [$ menu_grade_temp-1])
{
For ($ p = 1; $ p <= $ menu_grade_temp; $ p ++) {echo "";}
$ Temp3 = $ menu_superior_temp_array;
$ Temp3 [$ menu_grade_temp] = $ menu_array [0];
$ Temp2 = implode ("/", $ temp3 );
If ($ menu_array [0] ==$ menu_superior_temp_array [$ temp1-1])
{
$ Temp5 = $ temp1-1;
$ Temp3 [$ menu_grade_temp] = "";
$ Temp6 = implode ("/", $ temp3 );
Echo "$ menu_array [1]
";
My_menu ($ menu_content, $ I, $ temp1, $ temp2 );
}
Else
{
$ Temp3 [$ menu_grade_temp + 1] = "";
$ Temp6 = implode ("/", $ temp3 );
Echo "$ menu_array [1]
";
}
}
}
}
// Connect to the MySQL database
$ Db_host = "localhost ";
$ Db_user = "dkj ";
$ Db_passWord = "123 ";
$ Db_name = "test ";
Mysql_connect ($ db_host, $ db_user, $ db_password );
Mysql_select_db ($ db_name );

// Obtain data from the database
$ Query_string = "select * from menu order by menu_grade ";
$ Db_data = mysql_query ($ query_string );

// Perform initialization for the first time
If ($ menu_grade_temp = "")
{
$ Menu_superior_temp = 0;
}

// Read all information into the array and count the number of arrays
$ I = 0;
While (list ($ menu_id, $ menu, $ menu_grade, $ menu_superior) = mysql_fetch_row ($ db_data ))
{
$ Menu_content [$ I] = $ menu_id. "/". $ menu. "/". $ menu_grade. "/". $ menu_superior;
$ I ++;
}
My_menu ($ menu_content, $ I, 1, $ menu_superior_temp );

/* Attach the database structure and simulation data
# PhpMyAdmin MySQL-Dump
#
# HOST: localhost database: test
#--------------------------------------------------------

#
# Data table structure 'menu'
#

Create table menu (
Menu_id int (11) not null auto_increment,
Menu varchar (20) not null,
Menu_grade int (11) not null,
Menu_superior int (11) not null,
UNIQUE menu_id (menu_id)
);

#
# Export the following database content 'menu'
#

Insert into menu VALUES ('1', 'computer ', '1', '0 ');
Insert into menu VALUES ('2', 'programmatically ', '2', '1 ');
Insert into menu VALUES ('3', 'network', '2', '1 ');
Insert into menu VALUES ('4', 'php and mysql', '3', '2 ');
Insert into menu VALUES ('5', 'c language ', '3', '2 ');
Insert into menu VALUES ('6', 'webpage maker ', '3', '3 ');
Insert into menu VALUES ('7', 'tcp, ipprotocol ', '3', '3 ');
Insert into menu VALUES ('8', 'mat', '1', '0 ');
Insert into menu VALUES ('9', 'advanced Mathematics ', '2', '8 ');
Insert into menu VALUES ('10', 'linear algebra ', '3', '9 ');
Insert into menu VALUES ('11', 'Discrete mat', '3', '9 ');
Insert into menu VALUES ('12', 'Elementary mat', '2', '8 ');
Insert into menu VALUES ('13', 'literary ', '1', '0 ');
Insert into menu VALUES ('14', 'Chinese literature ', '2', '13 ');
Insert into menu VALUES ('15', 'php', '4', '4 ');
Insert into menu VALUES ('16', 'mysql', '4', '4 ');
*/
?>

The above introduces how to use PHP to implement the multi-level tree menu function, including some content, and hope to help friends who are interested in PHP tutorials.

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.