Home >Backend Development >PHP Tutorial >PHP的开合式多级菜单程序_PHP

PHP的开合式多级菜单程序_PHP

WBOY
WBOYOriginal
2016-06-01 12:27:38933browse

# --------------------------------------------------------
# 数据表的结构 '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)
);
# -------------------------------------------------
#数据表内的内容,根据具体情况而定
# -------------------------------------------------
INSERT INTO menu VALUES( '1', '计算机', '1', '0');
INSERT INTO menu VALUES( '2', '编程', '2', '1');
INSERT INTO menu VALUES( '3', '网络', '2', '1');
INSERT INTO menu VALUES( '4', 'PHP与MySql', '3', '2');
INSERT INTO menu VALUES( '5', 'C语言', '3', '2');
INSERT INTO menu VALUES( '6', '网页制作', '3', '3');
INSERT INTO menu VALUES( '7', 'TCP、IP协议', '3', '3');
INSERT INTO menu VALUES( '8', '数学', '1', '0');
INSERT INTO menu VALUES( '9', '高等数学', '2', '8');
INSERT INTO menu VALUES( '10', '线性代数', '3', '9');
INSERT INTO menu VALUES( '11', '离散数学', '3', '9');
INSERT INTO menu VALUES( '12', '初等数学', '2', '8');
INSERT INTO menu VALUES( '13', '文学', '1', '0');
INSERT INTO menu VALUES( '14', '中国文学', '2', '13');
INSERT INTO menu VALUES( '15', 'php', '4', '4');
INSERT INTO menu VALUES( '16', 'mysql', '4', '4');


//树型目录结构模板程序
//菜单目录库字段说明:
//menu_id 菜单项目 id
//menu 菜单名称
//menu_grade 菜单等级 1 为主菜单 2 为二级菜单 ........
//menu_superior 上一级菜单 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 {
$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";} //显示目录前的等级箭头
$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 " $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]
";
}
}
}
}
// 连接 MySql 数据库
$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);

//从数据库中取得数据
$query_string="select * from menu order by menu_grade";
$db_data=mysql_query($query_string);

//第一次执行初始化
if ($menu_grade_temp=="")
{
$menu_superior_temp=0;
}

//将所有的信息读入数组,并统计数组个数
$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);
?>


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