Heim > Artikel > Backend-Entwicklung > PHP+Mysql无限极归类高效查询
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');<br />$class_rs = array();<br />$Do_While_Static=mysql_num_rows($My_rs)?true:false;<br />if($Do_While_Static) $Prs=array($My_rs);<br />while($Do_While_Static){<br /> $My_rs=end($Prs);<br /> if($rs=mysql_fetch_array($My_rs)){<br /> $class_rs[]=$rs;<br /> $My_Prs = $my_conn->mysql_query('select * from table where pid = '.$rs['id']);<br /> $Temp_Static=mysql_num_rows($My_Prs)?true:false;<br /> if($Temp_Static) $Prs[]=$My_Prs;<br /> }else{<br /> array_pop($Prs);<br /> if(count($Prs)==0){<br /> $Do_While_Static=false;<br /> break;<br /> }<br /> }<br />}
foreach($class_rs as $rs){<br /> $T_Str=str_pad('',$rs['level'],"-");<br /> echo $T_Str.$rs['name'];<br />}