<?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>