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开发文件系统实例讲解
Oct 09 PHP
php学习笔记 面向对象中[接口]与[多态性]的应用
Jun 16 PHP
php笔记之:数据类型与常量的使用分析
May 14 PHP
PHP错误WARNING: SESSION_START() [FUNCTION.SESSION-START]解决方法
May 04 PHP
PHP按指定键值对二维数组进行排序的方法
Dec 22 PHP
详解WordPress开发中过滤属性以及Sql语句的函数使用
Dec 25 PHP
WordPress中给媒体文件添加分类和标签的PHP功能实现
Dec 31 PHP
yii框架无限极分类的实现方法
Apr 08 PHP
Laravel 批量更新多条数据的示例
Nov 27 PHP
PHP+MariaDB数据库操作基本技巧备忘总结
May 21 PHP
PHP中如何使用Redis接管文件存储Session详解
Nov 28 PHP
phpfpm的作用和用法
Oct 10 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 年龄计算函数(精确到天)
2012/06/07 PHP
php图片处理函数获取类型及扩展名实例
2014/11/19 PHP
PHP ajax 异步执行不等待执行结果的处理方法
2015/05/27 PHP
php实现计算百度地图坐标之间距离的方法
2016/05/05 PHP
ThinkPHP框架表单验证操作方法
2017/07/19 PHP
php-fpm添加service服务的例子
2018/04/27 PHP
Gambit vs CL BO3 第二场 2.13
2021/03/10 DOTA
改进:论坛UBB代码自动插入方式
2006/12/22 Javascript
javascript new fun的执行过程
2010/08/05 Javascript
原生javascript实现图片轮播效果代码
2010/09/03 Javascript
extjs中grid中嵌入动态combobox的应用
2011/01/01 Javascript
微信小程序 简单DEMO布局,逻辑,样式的练习
2016/11/30 Javascript
js判断iframe中元素是否存在的实现代码
2016/12/24 Javascript
jquery easyui DataGrid简单示例
2017/01/23 Javascript
jQuery实现基本隐藏与显示效果的方法详解
2018/09/05 jQuery
js实现类似iphone的网页滑屏解锁功能示例【附源码下载】
2019/06/10 Javascript
Python爬虫框架Scrapy安装使用步骤
2014/04/01 Python
Python构建图像分类识别器的方法
2019/01/12 Python
Django如何将URL映射到视图
2019/07/29 Python
python类的实例化问题解决
2019/08/31 Python
opencv python 图片读取与显示图片窗口未响应问题的解决
2020/04/24 Python
tensorflow实现残差网络方式(mnist数据集)
2020/05/26 Python
python如何写出表白程序
2020/06/01 Python
Python 爬虫性能相关总结
2020/08/03 Python
python Matplotlib数据可视化(2):详解三大容器对象与常用设置
2020/09/30 Python
opencv python 对指针仪表读数识别的两种方式
2021/01/14 Python
使用CSS3设计地图上的雷达定位提示效果
2016/04/05 HTML / CSS
HTML5中5个简单实用的API
2014/04/28 HTML / CSS
html svg生成环形进度条的实现方法
2019/09/23 HTML / CSS
荷兰游戏商店:Allyouplay
2019/03/16 全球购物
员工考核管理制度
2014/02/02 职场文书
护理人员的自我评价分享
2014/03/15 职场文书
不尊敬老师的检讨书
2014/12/21 职场文书
2015年商场工作总结
2015/04/27 职场文书
小学校园广播稿
2015/08/18 职场文书
优秀乡村医生事迹材料(2016精选版)
2016/02/29 职场文书