首页 > 开发 > PHP > 正文

php从数据库查询结果生成树形列表的方法

2024-05-04 23:34:25
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了php从数据库查询结果生成树形列表的方法,涉及php操作html元素生成树形列表的技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了php从数据库查询结果生成树形列表的方法。分享给大家供大家参考。具体分析如下:

本代码可以从数据库读取数据生成一个类似于windows的资源管理器的树形列表

 

 
  1. <?php 
  2. /* Here are the database definitions (for Solid) that i use in this code. 
  3. * It should not be hard to adapt it to another database. 
  4. */ 
  5. /* 
  6. CREATE TABLE dirent_types ( 
  7. id INTEGER NOT NULL, 
  8. icon VARCHAR(50), 
  9. name VARCHAR(50), 
  10. PRIMARY KEY(id) 
  11. ); 
  12. INSERT INTO dirent_types VALUES(1, 'folderclosed', 'Directory'); 
  13. INSERT INTO dirent_types VALUES(2, 'document', 'File'); 
  14. CREATE TABLE directory ( 
  15. id INTEGER NOT NULL, 
  16. parent INTEGER REFERENCES directory(id), 
  17. name VARCHAR(200), 
  18. icon VARCHAR(50), 
  19. type INTEGER REFERENCES dirent_types(id), 
  20. url VARCHAR(200), 
  21. PRIMARY KEY(id) 
  22. ); 
  23. DROP INDEX directory_idx; 
  24. CREATE UNIQUE INDEX directory_idx ON directory(parent, name); 
  25. CREATE SEQUENCE dirent_id; 
  26. "CREATE PROCEDURE insert_dir_entry 
  27. (name VARCHAR, parent INTEGER, type INTEGER) 
  28. RETURNS(id INTEGER) 
  29. BEGIN 
  30. EXEC SQL WHENEVER SQLERROR ABORT; 
  31. EXEC SEQUENCE dirent_id.NEXT INTO id; 
  32. EXEC SQL PREPARE c_insert 
  33. INSERT INTO directory 
  34. (id, parent, type, name) 
  35. VALUES(?, ?, ?, ?); 
  36. EXEC SQL EXECUTE c_insert USING (id, parent, type, name); 
  37. EXEC SQL DROP c_insert; 
  38. END"; 
  39. CALL insert_dir_entry('My Computer', NULL, 1); 
  40. CALL insert_dir_entry('Network Neighbourhood', NULL, 1); 
  41. CALL insert_dir_entry('lucifer.guardian.no', 2, 1); 
  42. CALL insert_dir_entry('rafael.guardian.no', 2, 1); 
  43. CALL insert_dir_entry('uriel.guardian.no', 2, 1); 
  44. CALL insert_dir_entry('Control Panel', NULL, 1); 
  45. CALL insert_dir_entry('Services', 6, 1); 
  46. CALL insert_dir_entry('Apache', 7, 2); 
  47. CALL insert_dir_entry('Solid Server 2.2', 7, 2); 
  48. */ 
  49. function icon($icon, $name = '', $width = 0, $height = 0) { 
  50. global $DOCUMENT_ROOT; 
  51. $icon_loc = '/pics/menu'; 
  52. $file = "$DOCUMENT_ROOT$icon_loc/$icon.gif"; 
  53. if (!$width || !$height) { 
  54. $iconinfo = getimagesize($file); 
  55. if (!$width) { 
  56. $width = $iconinfo[0]; 
  57. } 
  58. if (!$height) { 
  59. $height = $iconinfo[1]; 
  60. } 
  61. } 
  62. printf( '<img%s border=0 align=top src="/pics/menu/%s.gif" '. 
  63. 'width="%d" height="%d">', $name ? " name=/"$name/"" : '', 
  64. $icon, $width, $height); 
  65. } 
  66. function display_directory($parent,$showdepth=0,$ancestors=false){ 
  67. global $child_nodes, $node_data, $last_child; 
  68. reset($child_nodes[$parent]); 
  69. $size = sizeof($child_nodes[$parent]); 
  70. $lastindex = $size - 1; 
  71. if (!$ancestors) { 
  72. $ancestors = array(); 
  73. } 
  74. $depth = sizeof($ancestors); 
  75. printf( '<div id="node_%d" class="dirEntry" visibility="%s">', 
  76. $parent, $showdepth > 0 ? 'show' : 'hide'); 
  77. while (list($index, $node) = each($child_nodes[$parent])) { 
  78. for ($i = 0; $i < $depth; $i++) { 
  79. $up_parent = (int)$node_data[$ancestors[$i]][ 'parent']; 
  80. $last_node_on_generation = $last_child[$up_parent]; 
  81. $uptree_node_on_generation = $ancestors[$i]; 
  82. if ($last_node_on_generation == $uptree_node_on_generation) { 
  83. icon( "blank"); 
  84. } else { 
  85. icon( "line"); 
  86. } 
  87. } 
  88. if ($child_nodes[$node]) { 
  89. // has children, i.e. it is a folder 
  90. $conn_icon = "plus"; 
  91. $expand = true; 
  92. } else { 
  93. $conn_icon = "join"; 
  94. $expand = false; 
  95. } 
  96. if ($index == $lastindex) { 
  97. $conn_icon .= "bottom"; 
  98. } elseif ($depth == 0 && $index == 0) { 
  99. $conn_icon .= "top"; 
  100. } 
  101. if ($expand) { 
  102. printf( "<a href=/"javascript:document.layers['node_%d'].visibility='show'/">", $node); 
  103. } 
  104. icon($conn_icon, "connImg_$node"); 
  105. if ($expand) { 
  106. print( "</a>"); 
  107. } 
  108. $icon = $node_data[$node][ 'icon']; 
  109. if (!$icon) { 
  110. $type = $node_data[$node][ 'type']; 
  111. $icon = $GLOBALS[ 'dirent_icons'][$type]; 
  112. } 
  113. icon($icon, "nodeImg_$node"); 
  114. $name = $node_data[$node][ 'name']; 
  115. printf( '?<font size="%d">%s</font><br%c>', -1, $name, 10); 
  116. if ($child_nodes[$node]) { 
  117. $newdepth = $showdepth; 
  118. if ($newdepth > 0) { 
  119. $newdepth--; 
  120. } 
  121. $new_ancestors = $ancestors; 
  122. $new_ancestors[] = $node; 
  123. display_directory($node, $newdepth, $new_ancestors); 
  124. } 
  125. } 
  126. print( "</div/n>"); 
  127. } 
  128. function setup_directory($parent, $maxdepth) 
  129. { 
  130. global $dirent_icons, $child_nodes, $node_data, $last_child; 
  131. $dirent_icons = sql_assoc('SELECT id,icon FROM dirent_types'); 
  132. $query = 'SELECT id,parent,type,icon,name '. 
  133. 'FROM directory '. 
  134. 'ORDER BY parent,name'; 
  135. $child_nodes = array(); 
  136. $node_data = array(); 
  137. $res = sql($query); 
  138. while (list($id,$parent,$type,$icon,$name)=db_fetch_row($res)){ 
  139. $child_nodes[(int)$parent][] = $id; 
  140. $node_data[$id] = array( 'id' => $id, 
  141. 'parent' => $parent, 
  142. 'type' => $type, 
  143. 'icon' => $icon, 
  144. 'name' => $name); 
  145. $last_child[(int)$parent] = $id; 
  146. } 
  147. } 
  148. ?> 

希望本文所述对大家的php程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表