递归目录树,怎么套上UL LI

WBOY
Release: 2016-06-13 12:48:48
Original
1019 people have browsed it

递归目录树,怎样套上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){<br />
      $res = '';<br />
      $sql = mysql_query("select * from menu where parent = '$parent'");<br />
      while($row = mysql_fetch_array($sql)){<br />
        $res .= '<li><a href="'.$row['id'].'">'.$row['name'].'</a>';<br />
        $t = nav($row['id']);<br />
        if(! empty($t)) $res .= "<ul>$t</ul>";<br />
        $res .= '</li>';<br />
      }<br />
      return $res;<br />
    }
    Copy after login

    调用时
    echo nav($id);

    我都是用 ajax 动态加载的,所以没有递归。
    至少目前不适合你
    Related labels:
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!