本文介绍 PHP 递归生成树状行实例代码
- header("content-type: text/html; charset=utf-8");
- $connect = mysql_connect(localhost, root, 123456);
- mysql_select_db("wz");
- mysql_query("set names utf8");
- //获得顶节点
- $sql = "select id, name,ischild from tree where parent = 0 order by id asc";
- $result = mysql_query($sql);
- while($row = mysql_fetch_array($result))
- {
- extract($row);
- //如果有子节点,就在前面加个事件,以便展开或者关闭子节点
- $icon = $ischild ? " ":"-";
- $name = $icon . $name;
- echo "
" . $name;
- if($ischild)
- {
- //递归获取节点
- getNode($id, 0);
- }
- 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;
- }
- ?>
-
- function Expand(id)
- {
- var obj = ドキュメント.getElementById(id).childNodes;
- for(var i=0;i
- {
- if(obj[i].nodeName == "DIV")
- {
- スイッチ(obj[i].style.display)
- {
- case "":
- case "ブロック":
- obj[i].style.display = "none";
- ブレーク;
- case "none ":
- obj[i].style.display = "ブロック";
- ブレーク;
- }
- }
- }
- }
-
http://www.bkjia.com/PHPjc/486161.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/486161.html技術記事本書介绍 PHP 递归生成树状態行实例码 ?php header("content-type: text/html; charset=utf-8"); $connect = mysql_connect(localhost, root, 123456); mysql_select_db("wz");も...