递归目录树,怎样套上ul li
本帖最后由 xingguangyingying 于 2013-04-15 08:18:54 编辑 function nav($parent){
$sql = mysql_query(select * from menu where parent = '$parent');
while($row = mysql_fetch_array($sql)){
echo ''.$row['name'].'';
nav($row['id']);
echo '';
}
}
子类要怎么套上ul输出呢?
类似这样
音乐
流行
经典
80年代
90年代
电影
书籍
------解决方案--------------------
你边查询边输出,于是你就无法知道当前节点是否有子节点(因为还未读到)
所以你需要想将查询结果读到数组 http://bbs.csdn.net/topics/390364669
然后再递归输出
你也可以用变量缓存待输出的内容,等递归结束时再输出
function nav($parent){
$res = '';
$sql = mysql_query(select * from menu where parent = '$parent');
while($row = mysql_fetch_array($sql)){
$res .= ''.$row['name'].'';
$t = nav($row['id']);
if(! empty($t)) $res .= $t;
$res .= '';
}
return $res;
}
调用时
echo nav($id);
我都是用 ajax 动态加载的,所以没有递归。
至少目前不适合你