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&amp;MYSQL分页原理及实现
Jan 02 PHP
用PHP的ob_start() 控制您的浏览器cache
Aug 03 PHP
浅析php插件 HTMLPurifier HTML解析器
Jul 01 PHP
php inc文件使用的风险和注意事项
Nov 12 PHP
安装ImageMagick出现error while loading shared libraries的解决方法
Sep 23 PHP
php获取指定范围内最接近数的方法
Jun 02 PHP
隐性调用php程序的方法
Jun 13 PHP
php简单实现多语言切换的方法
May 09 PHP
PHP中explode函数和split函数的区别小结
Aug 24 PHP
php中序列化与反序列化详解
Feb 13 PHP
php7 安装yar 生成docker镜像
May 09 PHP
thinkphp 框架数据库切换实现方法分析
May 18 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
PHP脚本中include文件出错解决方法
2008/11/20 PHP
PHP n个不重复的随机数生成代码
2009/06/23 PHP
php在线代理转向代码
2012/05/05 PHP
php使用百度翻译api示例分享
2014/01/31 PHP
PHP mysql事务问题实例分析
2016/01/18 PHP
Jquery实现列表(隔行换色,全选,鼠标滑过当前行)效果实例
2013/06/09 Javascript
js阻止冒泡及jquery阻止事件冒泡示例介绍
2013/11/19 Javascript
jquery中map函数与each函数的区别实例介绍
2014/06/23 Javascript
JavaScript Promise启示录
2014/08/12 Javascript
JavaScript实现的Tween算法及缓冲特效实例代码
2015/11/03 Javascript
纯JS实现可拖拽表单的简单实例
2016/09/02 Javascript
vue2.0实现导航菜单切换效果
2017/05/08 Javascript
Nginx 配置多站点vhost 的方法
2018/01/07 Javascript
Vue.js最佳实践(五招助你成为vuejs大师)
2018/05/04 Javascript
Vue常用传值方式、父传子、子传父及非父子实例分析
2020/02/24 Javascript
vue深度监听(监听对象和数组的改变)与立即执行监听实例
2020/09/04 Javascript
[36:33]完美世界DOTA2联赛循环赛 Matador vs Forest 第一场 11.06
2020/11/06 DOTA
Windows下实现Python2和Python3两个版共存的方法
2015/06/12 Python
python获取外网IP并发邮件的实现方法
2017/10/01 Python
python编程培训 python培训靠谱吗
2018/01/17 Python
python3获取url文件大小示例代码
2019/09/18 Python
TensorFlow索引与切片的实现方法
2019/11/20 Python
Python基于class()实现面向对象原理详解
2020/03/26 Python
调整Jupyter notebook的启动目录操作
2020/04/10 Python
python matplotlib模块基本图形绘制方法小结【直线,曲线,直方图,饼图等】
2020/04/26 Python
利用Python实现自动扫雷小脚本
2020/12/17 Python
班组长岗位职责范本
2014/01/05 职场文书
18岁生日感言
2014/01/12 职场文书
遗体告别仪式答谢词
2014/01/23 职场文书
消防安全责任书范本
2014/04/15 职场文书
小学五年级学生评语
2014/04/22 职场文书
白莲教口号
2014/06/18 职场文书
求职自我推荐信
2014/06/25 职场文书
写得不错的求职信范文
2014/07/11 职场文书
Springboot如何使用logback实现多环境配置?
2021/06/16 Java/Android
Rhit高效可视化Nginx日志查看工具
2021/11/01 Servers