<?php
// 连接数据库
$conn = mysql_connect("localhost","root", "root");
if (! $conn) {
echo "数据库链接失败";
//exit();
}
// 选择所要操作的数据库
mysql_select_db("test",$conn);
// 设置数据库编码格式
mysql_query("SET NAMES UTF8");
function getList($pid=0,$result=array(),$spac=0){
$spac=$spac+2;
//查询表中某一个固定数据
$sql="SELECT * FROM deepcate WHERE pid =$pid ";
$res=mysql_query($sql);
//$result=array();
while ($row = mysql_fetch_assoc($res)){
$row['catename']=str_repeat(' ', $spac).'|--'.$row['catename'];
$result[]=$row;
getList($row['id'],$result,$spac);
}
return $result;
}
function displayCate($pid=0,$selected=1){
$rs=getList($pid);
$str="";
$str.="<select name = 'cate'>";
foreach($rs as $key=>$val){
$selectedstr="";
if($val['id']==$selected){
$selectedstr="selected";
}
$str.="<option {$selectedstr}>{$val['catename']}</option>";
}
return $str.='</select>';
}
echo displayCate(0,2);
kihoor