Home >Backend Development >PHP Tutorial >PHP open and close multi-level menu program_PHP tutorial
#------------------------------------------------ -------
# 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)
);
# --- --------------------------------------------------
# The content in the data sheet depends on the specific situation
# ---------------------------------- ---------------
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', 'Web page 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');
//Tree directory structure template program
/ /Menu directory field description:
//menu_id Menu item id
//menu Menu name
//menu_grade Menu level 1 is the main menu 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 ">";} //Display the grade arrow in front of the directory
$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 "< ;a href="$PHP_SELF?menu_grade_temp=".$temp5."&menu_superior_temp=
$temp6">$menu_array[1]
";
my_menu($menu_content, $i,$temp1,$temp2);
}
else
{
$temp3[$menu_grade_temp+1]="";
$temp6=implode("/",$ temp3);
echo "$temp6">$menu_array[1]
" ;
}
}
}
}
// Connect to MySql database
$db_host="localhost";
$db_user="root";
$db_password ="zmxj";
$db_name="joss";
mysql_connect($db_host,$db_user,$db_password);
mysql_select_db($db_name);
//From the database Get data
$query_string="select * from menu order by menu_grade";
$db_data=mysql_query($query_string);
//First time 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);
?>