Teach you to use PHP to implement multi-level tree menu function

Source: Internet
Author: User
Tags php tutorial
Tree-type directory structure template program
Menu Catalog Library Field Description:
MENU_ID menu item ID
Menu Name
Menu_grade Menu Level 1 Main Menu 2 for level two menu ...
Menu_superior first-level menu ID number

function My_menu ($menu _content, $i, $menu _grade_temp, $menu _superior_temp)
{
Global $php _self;
$temp 1= $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 "";}
$temp 3= $menu _superior_temp_array;
$temp 3[$menu _grade_temp]= $menu _array[0];
$temp 2=implode ("/", $temp 3);
if ($menu _array[0]== $menu _superior_temp_array[$temp 1-1])
{
$temp 5= $temp 1-1;
$temp 3[$menu _grade_temp]= "";
$temp 6=implode ("/", $temp 3);
echo "$menu _array[1]
";
My_menu ($menu _content, $i, $temp 1, $temp 2);
}
Else
{
$temp 3[$menu _grade_temp+1]= "";
$temp 6=implode ("/", $temp 3);
echo "$menu _array[1]
";
}
}
}
}
Connect to 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);

Getting data from the database
$query _string= "SELECT * from Menu order by Menu_grade";
$db _data=mysql_query ($query _string);

First execution initialization
if ($menu _grade_temp== "")
{
$menu _superior_temp=0;
}

Read all the 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);

/* Attached database structure and simulation data
# phpMyAdmin Mysql-dump
#
# Host: localhost database: Test
# --------------------------------------------------------

#
# The structure of the data table ' menu '
#

CREATE TABLE Menu (
menu_id Int (one) not NULL auto_increment,
Menu varchar (not NULL),
Menu_grade Int (one) is not NULL,
Menu_superior Int (one) is not NULL,
UNIQUE menu_id (menu_id)
);

#
# Export the following database contents ' menu '
#

INSERT into Menu VALUES (' 1 ', ' computer ', ' 1 ', ' 0 ');
INSERT into Menu VALUES (' 2 ', ' Programming ', ' 2 ', ' 1 ');
INSERT into Menu VALUES (' 3 ', ' Network ', ' 2 ', ' 1 ');
INSERT into Menu VALUES (' 4 ', ' PHP with MySQL ', ' 3 ', ' 2 ');
INSERT into Menu VALUES (' 5 ', ' C language ', ' 3 ', ' 2 ');
INSERT into Menu VALUES (' 6 ', ' Page maker ', ' 3 ', ' 3 ');
INSERT into Menu VALUES (' 7 ', ' TCP, IP protocol ', ' 3 ', ' 3 ');
INSERT into Menu VALUES (' 8 ', ' Math ', ' 1 ', ' 0 ');
INSERT into Menu VALUES (' 9 ', ' Advanced math ', ' 2 ', ' 8 ');
INSERT into menu VALUES (' 10 ', ' linear Algebra ', ' 3 ', ' 9 ');
INSERT into menu VALUES (' 11 ', ' Discrete Math ', ' 3 ', ' 9 ');
INSERT into Menu VALUES (' 12 ', ' Elementary math ', ' 2 ', ' 8 ');
INSERT into menu VALUES (' 13 ', ' literature ', ' 1 ', ' 0 ');
INSERT into Menu VALUES (' 14 ', ' Chinese Literature ', ' 2 ', ' 13 ');
INSERT into menu VALUES (' All ', ' php ', ' 4 ', ' 4 ');
INSERT into menu VALUES (' + ', ' MySQL ', ' 4 ', ' 4 ');
*/
?>

The above is introduced to teach you to use PHP to achieve multi-level tree menu functions, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.