欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php 三级联动菜单

程序员文章站 2024-03-09 09:34:29
数据库结构 复制代码 代码如下: -- 表的结构 `bigclass` -- create table `bigclass` ( `bigclassid` int(11)...
数据库结构
复制代码 代码如下:

-- 表的结构 `bigclass`
--
create table `bigclass` (
`bigclassid` int(11) not null auto_increment,
`bigclassname` varchar(200) collate utf8_unicode_ci not null,
`sort` int(11) not null,
`suoshu` int(1) not null,
primary key (`bigclassid`)
) engine=myisam default charset=utf8 collate=utf8_unicode_ci auto_increment=19 ;
-- 表的结构 `smallclass`
--
create table `smallclass` (
`smallclassid` int(11) not null auto_increment,
`smallclassname` varchar(200) collate utf8_unicode_ci not null,
`bigclassid` int(11) not null,
`sort` int(11) not null,
primary key (`smallclassid`)
) engine=myisam default charset=utf8 collate=utf8_unicode_ci auto_increment=7 ;
-- 表的结构 `minclass`
--
create table `minclass` (
`minclassid` int(10) not null auto_increment,
`minclassname` varchar(200) not null,
`bigclassid` int(10) not null,
`smallclassid` int(10) not null,
`sort` int(10) not null,
primary key (`minclassid`)
) engine=myisam default charset=latin1 auto_increment=3 ;

代码如下
复制代码 代码如下:

<script language = "javascript">
var onecount;
onecount=0;
subcat = new array();
<?
//类别选择
mysql_select_db($database_lr, $lr);
$sql = "select * from smallclass order by sort";
$result = mysql_query( $sql );
$count = 0;
while($res = mysql_fetch_row($result)){
?>
subcat[<?=$count?>] = new array("<?=$res[1]?>","<?=$res[4]?>","<?=$res[0]?>");
<?
$count++;
}
?>
onecount=<?php echo $count?>
//联动函数
function changelocation(bigclassid)
{
document.myform.smallclassid.length = 0;
var bigclassid=bigclassid;
var i;
document.myform.smallclassid.options[0] = new option('请选择二级分类','');
for (i=0;i < onecount; i++)
{
if (subcat[i][1] == bigclassid)
{
document.myform.smallclassid.options[document.myform.smallclassid.length] = new option(subcat[i][0], subcat[i][2]);
}
}
}
</script>
<?php
mysql_select_db($database_lr, $lr);
$sql2 = "select * from minclass order by sort";
$result2 = mysql_query( $sql2 );
$count2 = 0;
?>
<script language = "javascript">
//如果这个数组中含有双引号则不能使用。即二级和三级类不能含有双引号
var onecount2;
onecount2=0;
subcat2 = new array();
<?php
$count2 = 0;
while($res2 = mysql_fetch_row($result2)){
?>
subcat2[<?php echo $count2?>] = new array("<?php echo $res2[1]?>","<?php echo $res2[3]?>","<?php echo $res2[0]?>");
<?php
$count2++;
}
?>
onecount2=<?php echo $count2?>;
function changelocation2(smallclassid)
{
document.myform.minclassid.length = 0;
var smallclassid=smallclassid;
var j;
document.myform.minclassid.options[0] = new option('请选择三级分类','');
for (j=0;j < onecount2; j++)
{
if (subcat2[j][1] == smallclassid)
{
document.myform.minclassid.options[document.myform.minclassid.length] = new option(subcat2[j][0], subcat2[j][2]);
}
}
}
</script>
<select name="bigclassid" onchange="changelocation(document.myform.bigclassid.options[document.myform.bigclassid.selectedindex].value)" size="1">
<option selected value="">请指定一级分类</option>
<?
$sql = "select * from bigclass order by sort";
$result = mysql_query( $sql );
while($res = mysql_fetch_row($result)){
?>
<option value="<? echo $res[0]; ?>"><? echo $res[1]?></option>
<? } ?>
</select>
<select name="smallclassid" onchange="changelocation2(document.myform.smallclassid.options[document.myform.smallclassid.selectedindex].value)" size="1">
<option selected value="">请指定二级分类</option>
</select>
<select name="minclassid" size="1">
<option selected value="">==所有三级分类==</option>
</select>