PHP+Mysql无限极分类高效查询
Mysql分类表
id pid level name
1 0 0 test1
2 0 0 test2
3 1 1 test1.1
4 3 2 test1.1.1
PHP查询方法
Mysql数据查询类忽略----(大家都懂的)
我的查询方法:
$My_rs = $my_conn->mysql_query('select * from table where pid = 0');
$class_rs = array();
$Do_While_Static=mysql_num_rows($My_rs)?true:false;
if($Do_While_Static) $Prs=array($My_rs);
while($Do_While_Static){
$My_rs=end($Prs);
if($rs=mysql_fetch_array($My_rs)){
$class_rs[]=$rs;
$My_Prs = $my_conn->mysql_query('select * from table where pid = '.$rs['id']);
$Temp_Static=mysql_num_rows($My_Prs)?true:false;
if($Temp_Static) $Prs[]=$My_Prs;
}else{
array_pop($Prs);
if(count($Prs)==0){
$Do_While_Static=false;
break;
}
}
}foreach($class_rs as $rs){
$T_Str=str_pad('',$rs['level'],"-");
echo $T_Str.$rs['name'];
}