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


Posted in PHP onApril 17, 2015

本文实例讲述了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运行速度的一些小技巧分享
Jul 03 PHP
PHP最常用的2种设计模式工厂模式和单例模式介绍
Aug 14 PHP
深入探讨<br />和 \r\n两者有什么区别??
Jun 05 PHP
浅析关于PHP位运算的简单权限设计
Jun 30 PHP
php/js获取客户端mac地址的实现代码
Jul 08 PHP
注意:php5.4删除了session_unregister函数
Aug 05 PHP
php+ajax实时输入自动搜索匹配的方法
Dec 26 PHP
php超快高效率统计大文件行数
Jul 05 PHP
PHP的Yii框架中过滤器相关的使用总结
Mar 29 PHP
php中引用&amp;的用法分析【变量引用,函数引用,对象引用】
Dec 12 PHP
laravel获取不到session的三种解决办法【推荐】
Sep 16 PHP
PHP全局使用Laravel辅助函数dd
Dec 26 PHP
php实现阿拉伯数字和罗马数字相互转换的方法
Apr 17 #PHP
php实现根据词频生成tag云的方法
Apr 17 #PHP
php计算两个坐标(经度,纬度)之间距离的方法
Apr 17 #PHP
php使用GD创建保持宽高比缩略图的方法
Apr 17 #PHP
PHP中preg_match正则匹配中的/u、/i、/s含义
Apr 17 #PHP
php和editplus正则表达式去除空白行
Apr 17 #PHP
PHP生成唯一订单号的方法汇总
Apr 16 #PHP
You might like
实现dedecms全站URL静态化改造的代码
2007/03/29 PHP
PHP利用header跳转失效的解决方法
2014/10/24 PHP
php验证码实现代码(3种)
2015/09/07 PHP
使用PHP实现微信摇一摇周边红包
2016/01/04 PHP
thinkPHP自动验证机制详解
2016/12/05 PHP
JS创建优美的页面滑动块效果 - Glider.js
2007/09/27 Javascript
jquery实现每个数字上都带进度条的幻灯片
2013/02/20 Javascript
jQuery表格插件ParamQuery简单使用方法示例
2013/12/05 Javascript
js实现获取焦点后光标在字符串后
2014/09/17 Javascript
JavaScript通过元素索引号删除数组中对应元素的方法
2015/03/18 Javascript
javascript实现rgb颜色转换成16进制格式
2015/07/10 Javascript
JS时间特效最常用的三款
2015/08/19 Javascript
JavaScript操作select元素和option的实例代码
2016/01/29 Javascript
javascript每日必学之运算符
2016/02/16 Javascript
纯js实现倒计时功能
2017/01/06 Javascript
React 组件中的 bind(this)示例代码
2018/09/16 Javascript
vue项目打包部署_nginx代理访问方法详解
2018/09/20 Javascript
[10:49]2014国际邀请赛 叨叨刀塔第二期为真正的电竞喝彩
2014/07/21 DOTA
python数据结构链表之单向链表(实例讲解)
2017/07/25 Python
Python实现螺旋矩阵的填充算法示例
2017/12/28 Python
详解python Todo清单实战
2018/11/01 Python
对python条件表达式的四种实现方法小结
2019/01/30 Python
Python代码块及缓存机制原理详解
2019/12/13 Python
Python Opencv 通过轨迹(跟踪)栏实现更改整张图像的背景颜色
2020/03/09 Python
python+gdal+遥感图像拼接(mosaic)的实例
2020/03/10 Python
Python如何使用正则表达式爬取京东商品信息
2020/06/01 Python
matplotlib之属性组合包(cycler)的使用
2021/02/24 Python
html5 canvas合成海报所遇问题及解决方案总结
2017/08/03 HTML / CSS
五分钟学会HTML5的WebSocket协议
2019/11/22 HTML / CSS
TripAdvisor越南:全球领先的旅游网站
2017/09/21 全球购物
德国在线香料制造商:Gewürzland
2020/03/10 全球购物
俄罗斯三星品牌商店:Samsungstore
2020/04/05 全球购物
优秀求职信范文分享
2014/01/26 职场文书
开业主持词
2014/03/21 职场文书
2015年骨干教师工作总结
2015/05/26 职场文书
2016基督教会圣诞节开幕词
2016/03/04 职场文书