php从数据库查询结果生成树形列表的方法
程序员文章站
2022-03-04 18:20:51
本文实例讲述了php从数据库查询结果生成树形列表的方法。分享给大家供大家参考。具体分析如下:
本代码可以从数据库读取数据生成一个类似于windows的资源管理器的树形列表...
本文实例讲述了php从数据库查询结果生成树形列表的方法。分享给大家供大家参考。具体分析如下:
本代码可以从数据库读取数据生成一个类似于windows的资源管理器的树形列表
<?php /* here are the database definitions (for solid) that i use in this code. * it should not be hard to adapt it to another database. */ /* create table dirent_types ( id integer not null, icon varchar(50), name varchar(50), primary key(id) ); insert into dirent_types values(1, 'folderclosed', 'directory'); insert into dirent_types values(2, 'document', 'file'); create table directory ( id integer not null, parent integer references directory(id), name varchar(200), icon varchar(50), type integer references dirent_types(id), url varchar(200), primary key(id) ); drop index directory_idx; create unique index directory_idx on directory(parent, name); create sequence dirent_id; "create procedure insert_dir_entry (name varchar, parent integer, type integer) returns(id integer) begin exec sql whenever sqlerror abort; exec sequence dirent_id.next into id; exec sql prepare c_insert insert into directory (id, parent, type, name) values(?, ?, ?, ?); exec sql execute c_insert using (id, parent, type, name); exec sql drop c_insert; end"; call insert_dir_entry('my computer', null, 1); call insert_dir_entry('network neighbourhood', null, 1); call insert_dir_entry('lucifer.guardian.no', 2, 1); call insert_dir_entry('rafael.guardian.no', 2, 1); call insert_dir_entry('uriel.guardian.no', 2, 1); call insert_dir_entry('control panel', null, 1); call insert_dir_entry('services', 6, 1); call insert_dir_entry('apache', 7, 2); call insert_dir_entry('solid server 2.2', 7, 2); */ function icon($icon, $name = '', $width = 0, $height = 0) { global $document_root; $icon_loc = '/pics/menu'; $file = "$document_root$icon_loc/$icon.gif"; if (!$width || !$height) { $iconinfo = getimagesize($file); if (!$width) { $width = $iconinfo[0]; } if (!$height) { $height = $iconinfo[1]; } } printf( '<img%s border=0 align=top src="/pics/menu/%s.gif" '. 'width="%d" height="%d">', $name ? " name=\"$name\"" : '', $icon, $width, $height); } function display_directory($parent,$showdepth=0,$ancestors=false){ global $child_nodes, $node_data, $last_child; reset($child_nodes[$parent]); $size = sizeof($child_nodes[$parent]); $lastindex = $size - 1; if (!$ancestors) { $ancestors = array(); } $depth = sizeof($ancestors); printf( '<div id="node_%d" class="direntry" visibility="%s">', $parent, $showdepth > 0 ? 'show' : 'hide'); while (list($index, $node) = each($child_nodes[$parent])) { for ($i = 0; $i < $depth; $i++) { $up_parent = (int)$node_data[$ancestors[$i]][ 'parent']; $last_node_on_generation = $last_child[$up_parent]; $uptree_node_on_generation = $ancestors[$i]; if ($last_node_on_generation == $uptree_node_on_generation) { icon( "blank"); } else { icon( "line"); } } if ($child_nodes[$node]) { // has children, i.e. it is a folder $conn_icon = "plus"; $expand = true; } else { $conn_icon = "join"; $expand = false; } if ($index == $lastindex) { $conn_icon .= "bottom"; } elseif ($depth == 0 && $index == 0) { $conn_icon .= "top"; } if ($expand) { printf( "<a href=\"javascript:document.layers['node_%d'].visibility='show'\">", $node); } icon($conn_icon, "connimg_$node"); if ($expand) { print( "</a>"); } $icon = $node_data[$node][ 'icon']; if (!$icon) { $type = $node_data[$node][ 'type']; $icon = $globals[ 'dirent_icons'][$type]; } icon($icon, "nodeimg_$node"); $name = $node_data[$node][ 'name']; printf( '?<font size="%d">%s</font><br%c>', -1, $name, 10); if ($child_nodes[$node]) { $newdepth = $showdepth; if ($newdepth > 0) { $newdepth--; } $new_ancestors = $ancestors; $new_ancestors[] = $node; display_directory($node, $newdepth, $new_ancestors); } } print( "</div\n>"); } function setup_directory($parent, $maxdepth) { global $dirent_icons, $child_nodes, $node_data, $last_child; $dirent_icons = sql_assoc('select id,icon from dirent_types'); $query = 'select id,parent,type,icon,name '. 'from directory '. 'order by parent,name'; $child_nodes = array(); $node_data = array(); $res = sql($query); while (list($id,$parent,$type,$icon,$name)=db_fetch_row($res)){ $child_nodes[(int)$parent][] = $id; $node_data[$id] = array( 'id' => $id, 'parent' => $parent, 'type' => $type, 'icon' => $icon, 'name' => $name); $last_child[(int)$parent] = $id; } } ?>
希望本文所述对大家的php程序设计有所帮助。
上一篇: 微信自定义菜单的处理开发示例
下一篇: PHP生成唯一订单号的方法汇总
推荐阅读
-
PHP使用mysql_fetch_object从查询结果中获取对象集的方法_PHP教程
-
php查询mysql数据库并将结果保存到数组的方法
-
PHP使用mysql_fetch_object从查询结果中获取对象集的方法
-
php从数据库查询结果生成树形列表的方法
-
php从数据库查询结果生成树形列表的方法_php技巧
-
PHP使用mysql_fetch_object从查询结果中获取对象集的方法
-
php查询mysql数据库并将结果保存到数组的方法
-
php从数据库查询结果生成树形列表的方法_php技巧
-
PHP使用mysql_fetch_object从查询结果中获取对象集的方法
-
php从数据库查询结果生成树形列表的方法