当前位置:Gxlcms > PHP教程 > PHP递归生成树状行实例代码_PHP教程

PHP递归生成树状行实例代码_PHP教程

时间:2021-07-01 10:21:17 帮助过:2人阅读

本文介绍 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 "";
  21. }
  22. function getNode($id, $level)
  23. {
  24. $sql = "select id, name, ischild from tree where parent = $id order by id asc ";
  25. $result = mysql_query($sql);
  26. $level ;
  27. while($row = mysql_fetch_array($result))
  28. {
  29. extract($row);
  30. $icon = $ischild ? " ":"-";
  31. $name = $icon . $name;
  32. echo "" . echoChar(" ", $level) . $name;
  33. if($ischild)
  34. {
  35. getNode($id, $level);
  36. }
  37. echo "";
  38. }
  39. }
  40. function echoChar($char, $num)
  41. {
  42. for($i=0;$i< $num; $i )
  43. {
  44. $strChar .= $char;
  45. }
  46. return $strChar;
  47. }
  48. ?>

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...

人气教程排行