)</PRE><BR>
<PRE></PRE>
<PRE></PRE>
php 代码:
<P></P>
<P></P>
<PRE
class
=php name="code"><?php
//数据库我用的 mysql PDO 但是整个思路又是一样的
$conn
=mysql_connect('localhost','root','123');
if
(mysql_errno()){
printf('连接失败'.mysql_error());
}
mysql_select_db('edeng');
mysql_set_charset('utf8');
/*
*递归函数
*@param id 要查询 fid=$id
的所有子类 这里将$id 的默认值为设为 0 是因为我在数据库中
将最顶层的类别的 fid 设置为 0
*/
function
get_array(
$id
=0){
$sql
="select id,fid,cname from e_cat where fid= $id";
$result
=mysql_query(
$sql
);
$arr
=
array
();
if
(
$result
&& mysql_affected_rows()){
while
(
$rows
=mysql_fetch_assoc(
$result
)){
$rows
['child']=get_array(
$rows
['id']);
$arr
[] =
$rows
;
}
return
$arr
;
}
}
echo
'<pre>';
$result
= get_array();
print_r(
$result
);
</PRE><BR>
<BR>
<P></P>
<P> </P>
<P>函数首先查询出所有 fid 为 0 的类</P>
<P>通过
while
逐个循环进行回调查找 fid 为当前类的 id 的子类</P>
<P><BR>