<script language = "JavaScript">
var onecount;
onecount=0;
subcat = new Array();
<?
mysql_connect("localhost","gzty","123456");
mysql_select_db( "testing" );
$sql = "select * from icars_news_class where fatherID != 0";
$result = mysql_query( $sql );
$count = 0;
while($res = mysql_fetch_row($result)){
?>
subcat[<? echo $count; ?>] = new Array("<? echo $res[2]; ?>","<? echo $res[1]; ?>","<? echo $res[0]; ?>");
<?
$count++;
}
echo "onecount=$count";
?>function changelocation(locationid)
{
document.myform.TypeAddSmall.length = 0; var locationid=locationid;
var i;
for (i=0;i < onecount; i++)
{
if (subcat[i][1] == locationid)
{
document.myform.TypeAddSmall.options[document.myform.TypeAddSmall.length] = new Option(subcat[i][0], subcat[i][2]);
}
}
}
</script> <form action='?class=edittype_del_ok' method=post name=myform>
<select class="Submit" name="type" onChange="changelocation(document.myform.type.options[document.myform.type.selectedIndex].value)" size="1">
<option selected value="">-- 选择大类 --</option>
<?
$sqlI = "select * from icars_news_class where fatherID = 0";
$resultI = mysql_query( $sqlI );
while($resI = mysql_fetch_row($resultI)){
?>
<option value="<? echo $resI[0]; ?>"><? echo $resI[2]; ?></option>
<? } ?>
</select>
<select class="Submit" name="TypeAddSmall">
<option selected value="">请指定小分类</option>
</select>
</form>
var onecount;
onecount=0;
subcat = new Array();
<?
mysql_connect("localhost","gzty","123456");
mysql_select_db( "testing" );
$sql = "select * from icars_news_class where fatherID != 0";
$result = mysql_query( $sql );
$count = 0;
while($res = mysql_fetch_row($result)){
?>
subcat[<? echo $count; ?>] = new Array("<? echo $res[2]; ?>","<? echo $res[1]; ?>","<? echo $res[0]; ?>");
<?
$count++;
}
echo "onecount=$count";
?>function changelocation(locationid)
{
document.myform.TypeAddSmall.length = 0; var locationid=locationid;
var i;
for (i=0;i < onecount; i++)
{
if (subcat[i][1] == locationid)
{
document.myform.TypeAddSmall.options[document.myform.TypeAddSmall.length] = new Option(subcat[i][0], subcat[i][2]);
}
}
}
</script> <form action='?class=edittype_del_ok' method=post name=myform>
<select class="Submit" name="type" onChange="changelocation(document.myform.type.options[document.myform.type.selectedIndex].value)" size="1">
<option selected value="">-- 选择大类 --</option>
<?
$sqlI = "select * from icars_news_class where fatherID = 0";
$resultI = mysql_query( $sqlI );
while($resI = mysql_fetch_row($resultI)){
?>
<option value="<? echo $resI[0]; ?>"><? echo $resI[2]; ?></option>
<? } ?>
</select>
<select class="Submit" name="TypeAddSmall">
<option selected value="">请指定小分类</option>
</select>
</form>
就可以实现无限级分类扩展
我的注释比较详细了:<?php
$db=mysql_connect("localhost","root","123456");
$seldb=mysql_select_db("company",$db);$query1=mysql_query("select * from country where language_kind = '0' order by id asc"); //大类
$fMenu="";
$fValue="";
while($data1=mysql_fetch_array($query1)){
$fMenu.="\"".$data1[name]."\",";
$fValue.="\"".$data1[id]."\","; }
$fMenu=substr($fMenu,0,(strlen($fMenu)-1));
$fMenu="[".$fMenu."]";//*****************************得到var fMenu
$fValue=substr($fValue,0,(strlen($fValue)-1));
$fValue="[".$fValue."]";//*****************************得到var fValue //得到*****************************var sMenu
$query1=mysql_query("select * from country where language_kind = '0' order by id asc"); //大类
while($data1=mysql_fetch_array($query1)){
$province_id1=$data1[id]; $query2=mysql_query("select * from province where province_id='$province_id1' order by province_id desc"); //小类
while($data2=mysql_fetch_array($query2)){
$num=mysql_num_rows($query2);
$i++;
$sMenu.="\"".$data2[name]."\",";
if($i==$num){
$sMenu="[".$sMenu."],[";
$i=0;
}
}
}
$sMenu.="]";
$sMenu=str_replace("\",]","\"]",$sMenu);
$sMenu=str_replace(",[]","]",$sMenu);
if((substr($sMenu,0,2))=="[\""){
$sMenu="[".$sMenu;
}else{
$sMenu=preg_replace("/\[{3,}/is","[[\\1",$sMenu);//匹配
} //得到*****************************var sValue
$query1=mysql_query("select * from country where language_kind = '0' order by id asc");
while($data1=mysql_fetch_array($query1)){
$province_id=$data1[id]; $query2=mysql_query("select * from province where province_id='$province_id' order by province_id asc");
while($data2=mysql_fetch_array($query2)){
$nums=mysql_num_rows($query2);
$j++;
$sValue.="\"".$data2[id]."\",";
if($j==$nums){
$sValue="[".$sValue."],[";
$j=0;
}
}
}
$sValue.="]";
$sValue=str_replace("\",]","\"]",$sValue);
$sValue=str_replace(",[]","]",$sValue);
if((substr($sValue,0,2))=="[\""){
$sValue="[".$sValue;
}else{
$sValue=preg_replace("/\[{3,}/is","[[\\1",$sValue);//匹配
} ?>
<div id="tar"></div>
<SCRIPT LANGUAGE="JavaScript" DEFER>
var fMenu = <?php echo $fMenu; ?>;
var fValue = <?php echo $fValue; ?>;
var sMenu = <?php echo $sMenu; ?>;
var sValue = <?php echo $sValue; ?>; var oWhere = document.all.tar;
var ofMenu = document.createElement("<SELECT name='bigclass'>");
var osMenu = document.createElement("<SELECT name='smallclass'>");
with(oWhere)appendChild(ofMenu),appendChild(osMenu); createMainOptions();
createSubOptions(0); ofMenu.onchange = function() {createSubOptions(this.selectedIndex);}; function createMainOptions() {
for(var i=0;i<fMenu.length;i++)ofMenu.options[i] = new Option(fMenu[i],fValue[i]);
}
function createSubOptions(j) {
with(osMenu) {
length=0;
for(var i=0;i<sMenu[j].length;i++)osMenu.options[i] = new Option(sMenu[j][i],sValue[j][i]);
}
}
</SCRIPT>
<?php
/*
调用方法:
将此代码保存为文件ld2.php
例如要在write.php页面放一个二级联动,则在write.php页面
要放置二级联动的地方加一句require_once("ld2.php");
若write.php要向save.php页面提交数据则在save.php中使用
$bigclass=$_POST["bigclass"];//取得大类的id值
$smallclass=$_POST["smallclass"];//取得小类的id值
接下来就知道该怎么做了吧……
附表的结构:
可能有的表的结构和我有些不同,但基本点都一样,修改程序
中对应字段和表名就ok
---------------------------------------------------------
大类的表news_bclass结构 CREATE TABLE news_bclass (
id int(11) NOT NULL auto_increment,
bname varchar(10) NOT NULL default '',
PRIMARY KEY (id),
UNIQUE KEY id_2 (id),
KEY id (id)
) TYPE=MyISAM; 字段说明:bname为大类中文名称
------------------------------ 小类的表news_sclass结构 CREATE TABLE news_sclass (
id int(11) NOT NULL auto_increment,
sname varchar(10) NOT NULL default '',
parentid int(10) NOT NULL default '0',
bname varchar(10) NOT NULL default '',
PRIMARY KEY (id),
UNIQUE KEY id_2 (id),
KEY id (id)
) TYPE=MyISAM; 字段说明:sname为小类中文名称
parendid为大类中的id值
bname为大类中文名称
-----------------------------------------------------------
*/
?>