<?php
mysql_connect("localhost","root","root");
mysql_select_db("test");
mysql_query("set names gb2312");
function select($fid,$kong){
$query=mysql_query("select * from class where class_id_father='$fid' order by class_id asc");//------------------------------------ 查询$fid的儿子分类
while($arr=mysql_fetch_array($query)){
echo "<option value=\"".$arr[class_id]."\">";
for($i=0;$i<$kong;$i++){//------------------------------------根据$kong输出选项前面的空格.$kong控制子父分类的缩进
echo " ";
}
$he=$kong;
$kong++;
echo "┗".$arr[class_name];
echo "</option>";
select($arr[class_id],$kong);//------------------------------------递归查询此类的儿子分类
$kong=$he;//------------------------------------同级的儿子分类查询完后做这句,将$kong还原到此同级分类前使用的值
}
}
if($_POST['add']!=""){
if(mysql_query("insert into class (class_name,class_id_father) values ('$_POST[class_name]','$_POST[class_id]')"))
echo "<script>alert('恭喜,分类添加成功!!');window.location.href='./wxfl.php';</script>";
}
?>
<form name="form_class" action="" method="post">
添加分类
<select name="class_id" style="width:200px;">
<option vlaue="0" selected="selected">顶层分类</option>
<?php select(0,0); ?>
</select>
<input type="text" name="class_name" />
<input type="submit" name="add" value="添加分类">
</form>
<?php
function all($fid,$kong){//------------------------------------------------------------以下为输出列表,和上面的有一点不同,但原理都一样
$query=mysql_query("select * from class where class_id_father='$fid' order by class_id asc");
while($arr=mysql_fetch_array($query)){
for($i=0;$i<$kong;$i++){
echo " ";
}
$he=$kong;
$kong++;
echo "┗".$arr[class_name]." <a href='?id=".$arr[class_id]."'>修改</a> <a href='?id=".$arr[class_id]."'>删除</a><hr>";
all($arr[class_id],$kong);
$kong=$he;
}
}
all(0,0);
?>