Home  >  Article  >  Backend Development  >  Teach you to use PHP to implement multi-level tree menu function

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

WBOY
WBOYOriginal
2016-08-08 09:33:511443browse

//Tree directory structure template program
//Menu catalog field description:
//menu_id menu item id
//menu menu name
//menu_grade Menu level 1 is the main menu and 2 is the secondary menu.....
//menu_superior Previous menu id number

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 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);

//Get data from database
$query_string="select * from menu order by menu_grade";
$db_data=mysql_query($query_string);

//First execution of 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
#------------------------------------------------ -------

#
# Structure of data table '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', 'Programming', '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 Production', '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 Mathematics', '2', '8');
INSERT INTO menu VALUES('10', 'Linear Algebra', '3', '9');
INSERT INTO menu VALUES('11', 'Discrete Mathematics', '3', '9');
INSERT INTO menu VALUES('12', 'Elementary Mathematics', '2', '8');
INSERT INTO menu VALUES('13', 'Literature', '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 has introduced how to use PHP to implement the multi-level tree menu function, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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