php 无限级分类代码
<?php
$conn = mysql_connect("localhost","root","root") or die("数据库连接失入");
mysql_select_db("new",$conn);
?>
分类:<select name="fid">
<?php
function mainfl()
{
global $conn;
$result=mysql_query("select id,fid,name from a where fid=0 order by id ",$conn);//这里默认的fid=0,表示这是第一级。通过这个可以查出所有fid=0的项,即所有的第一级主类
if ($myrow=mysql_fetch_array($result))
{
do {
?>
<option value=<?php echo $myrow["id"];?>><?php echo $myrow["name"];?></option>//$myrow["id"]输出主类的ID为查询子类做准备,$myrwo["name"]主类名字
<?php echo subfl($myrow["id"],"--");?>//调用子类函数subfl,使用--<?php
}
while ($myrow=mysql_fetch_array($result));
}
}
//定义子级分类
function subfl($fid,$tag)//$fid那是主类ID,$tag为连接符
{
global $conn;
$result1=mysql_query("select id,fid,name from a where fid=$fid order by id",$conn);//查询出fid=$fid的所有面,这里的$fid值为$myrow["id"]
if ($myrow1=mysql_fetch_array($result1))
{
do {
?>
<option value=<?php echo $myrow1["id"];?>><?php echo $tag.$myrow1["name"];?></option>//输出子类
<?php
subfl($myrow1["id"],"-".$tag);
}
while ($myrow1=mysql_fetch_array($result1));
}
}
echo mainfl();
?>
</select>