Home > Backend Development > PHP Tutorial > PHP recursively generates tree row example code_PHP tutorial

PHP recursively generates tree row example code_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 17:41:17
Original
988 people have browsed it

本文介绍 PHP 递归生成树状行实例代码

  1. header("content-type: text/html; charset=utf-8");
  2. $connect = mysql_connect(localhost, root, 123456);
  3. mysql_select_db("wz");
  4. mysql_query("set names utf8");
  5. //获得顶节点
  6. $sql = "select id, name,ischild from tree where parent = 0 order by id asc";
  7. $result = mysql_query($sql);
  8. while($row = mysql_fetch_array($result))
  9. {
  10. extract($row);
  11. //如果有子节点,就在前面加个事件,以便展开或者关闭子节点
  12. $icon = $ischild ? " ":"-";
  13.       $name = $icon . $name;
  14.       echo "
    " . $name;
  15.       if($ischild)
  16.        {
  17.                   //递归获取节点
  18.            getNode($id, 0);
  19.       }
  20.       echo "
";
  •    }
  •    function getNode($id, $level)
  •    {
  •       $sql = "select id, name, ischild from tree where parent = $id order by id asc ";
  •        $result = mysql_query($sql);
  •       $level ;
  •      while($row = mysql_fetch_array($result))
  •     {
  •          extract($row);
  •            $icon = $ischild ? " ":"-";
  •           $name = $icon . $name;  
  •           echo "
    " . echoChar("    ", $level)  . $name;
  •          if($ischild)
  •            {
  •              getNode($id, $level);
  •           }
  •           echo "
  • ";
  •      }
  •    }
  •    
  •   function echoChar($char, $num)
  •   {
  •        for($i=0;$i< $num; $i )
  •       {
  •           $strChar .= $char;
  •       }
  •       return $strChar;
  •    }
  •    ?>
  •   

  • www.bkjia.comtruehttp://www.bkjia.com/PHPjc/486161.htmlTechArticle本文介绍 PHP 递归生成树状行实例代码 ?php header("content-type: text/html; charset=utf-8"); $connect = mysql_connect(localhost, root, 123456); mysql_select_db("wz"); m...
    source:php.cn
    Previous article:PHP output excel file example code_PHP tutorial Next article:HTTP download code written in PHP_PHP tutorial
    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
    Latest Articles by Author
    Latest Issues
    Related Topics
    More>
    Popular Recommendations
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template