PHP 递归生成树状行实例代码
来源:未知 责任编辑:智问网络 发表时间:2013-11-04 19:49 点击:次
本文介绍 PHP 递归生成树状行实例代码
- <?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 ? "<a href=javascript.:expand("div$id");> </a>":"-";
- $name = $icon . $name;
- echo "<div id=div" . $id . ">" . $name;
- if($ischild)
- {
- //递归获取节点
- getNode($id, 0);
- }
- echo "</div>";
- }
- 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 ? "<a href=javascript.:expand("div$id");> </a>":"-";
- $name = $icon . $name;
- echo "<div id=div" . $id . ">" . echoChar(" ", $level) . $name;
- if($ischild)
- {
- getNode($id, $level);
- }
- echo "</div>";
- }
- }
- function echoChar($char, $num)
- {
- for($i=0;$i< $num; $i )
- {
- $strChar .= $char;
- }
- return $strChar;
- }
- ?>
- <script language="javascript">
- function expand(id)
- {
- var obj = document.getElementById(id).childNodes;
- for(var i=0;i<obj.length;i )
- {
- if(obj[i].nodeName == "DIV")
- {
- switch(obj[i].style.display)
- {
- case "":
- case "block":
- obj[i].style.display = "none";
- break;
- case "none":
- obj[i].style.display = "block";
- break;
- }
- }
- }
- }
- </script>
相关新闻>>
- 发表评论
-
- 最新评论 更多>>