递归无限分类树,如何控制深度

WBOY
Release: 2016-06-13 11:53:56
Original
1506 people have browsed it

递归无限分类树,怎么控制深度?

本帖最后由 XingGuangYingYing 于 2014-03-19 02:52:34 编辑 代码如下,如 echo nav(0,0); 就像是全部   nav(0,1);就显示一级  $deep 应该怎么判断呢?

另外,递归效率是个问题,可否一次性从数据库取出来,然后数组变啊变的  输出树形目录呢  用ul li包围

function nav($parent,$deep){
$res = '';
  $sql = mysql_query("SELECT * from menu where parent = $parent  order by ordering DESC");

  while($row = mysql_fetch_array($sql)){
  
  $res .= '
  • '.$row['cname'].'';
     
        $t = nav($row['id'],$deep); 
        $res .= '
      '.$t.'
    ';


        $res .= '
  • ';
      }
      return $res;
    }


    }
    ------解决方案--------------------
    引用传递

    $deep = 0;
    function nav($parent,&$deep){
     $depp++;
    }
    ------解决方案--------------------
    深度控制可以定义一个静态变量,每次执行一次递增,如果$deep大于0且等于静态变量的值,则不再继续递归。
    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