源自:3-1 php无限分类下拉列表的代码实现
<?php
header("Content-Type:text/html;charset=utf-8");
include('./db.inc.php');
function cateList($pid=0,&$result=array(),$spac=0){
$spac+=2;
$sql="SELECT * FROM cate WHERE pid=$pid";
$res=mysql_query($sql);
while($row=mysql_fetch_assoc($res)){
$row['catename']=str_repeat(' ', $spac).'|--'.$row['catename'];
$result[]=$row;
cateList($row['id'],$result,$spac);
}
return $result;
}
function displayCate($pid=0,$select=1){
$rs=cateList($pid);
$str='';
$str.= "<select name='cate'>";
foreach ($rs as $k => $v) {
$selectstr='';
if($v['id']==$select){
$selectstr="selected";
}
$str.= "<option {$selectstr}>{$v['catename']}</option>";
}
return $str.= "</select>";
}
echo displayCate(0,5);
?>
提问者:noprom
2014-09-30 20:45