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 file_exists 检查文件或目录是否存在的函数
May 10 PHP
php的array_multisort()使用方法介绍
May 16 PHP
用PHP+MySQL搭建聊天室功能实例代码
Aug 20 PHP
PHP添加Xdebug扩展的方法
Feb 12 PHP
使用PHP导出Redis数据到另一个Redis中的代码
Mar 12 PHP
thinkphp区间查询、统计查询与SQL直接查询实例分析
Nov 24 PHP
PHP+MySQL删除操作实例
Jan 21 PHP
php中动态调用函数的方法
Mar 16 PHP
PHP 9 大缓存技术总结
Sep 17 PHP
ThinkPHP 在阿里云上的nginx.config配置实例详解
Oct 11 PHP
PHP实现浏览器中直接输出图片的方法示例
Mar 14 PHP
tp5框架前台无限极导航菜单类实现方法分析
Mar 29 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中对数组的一些常用的增、删、插操作函数总结
2015/11/27 PHP
基于jsTree的无限级树JSON数据的转换代码
2010/07/27 Javascript
jQuery 关于伪类选择符的使用说明
2013/04/24 Javascript
JQuery对class属性的操作实现按钮开关效果
2013/10/11 Javascript
JS去除字符串两端空格的简单实例
2013/12/27 Javascript
ExtJS4 表格的嵌套 rowExpander应用
2014/05/02 Javascript
javascript中offset、client、scroll的属性总结
2015/08/13 Javascript
jQuery如何使用自动触发事件trigger
2015/11/29 Javascript
jquery显示隐藏元素的实现代码
2016/05/19 Javascript
Bootstrap CSS组件之导航(nav)
2016/12/17 Javascript
JS轮播图实现简单代码
2021/02/19 Javascript
Angular4编程之表单响应功能示例
2017/12/13 Javascript
nodejs微信扫码支付功能实现
2018/02/17 NodeJs
详解ES6中的三种异步解决方案
2018/06/28 Javascript
vue-music 使用better-scroll遇到轮播图不能自动轮播问题
2018/12/03 Javascript
vue路由中前进后退的一些事儿
2019/05/18 Javascript
vue项目中全局引入1个.scss文件的问题解决
2019/08/01 Javascript
python根据出生日期获得年龄的方法
2015/03/31 Python
详解爬虫被封的问题
2019/04/23 Python
python 自动轨迹绘制的实例代码
2019/07/05 Python
解决pytorch 的state_dict()拷贝问题
2021/03/03 Python
前端隐藏出边界内容的实现方法
2016/04/14 HTML / CSS
浅谈html5 video 移动端填坑记
2018/01/15 HTML / CSS
经济实惠的豪华背包和行李袋:Packs Project
2018/10/17 全球购物
同步和异步有何异同,在什么情况下分别使用他们?举例说明
2014/02/27 面试题
学校安全检查制度
2014/01/27 职场文书
创业培训计划书
2014/05/03 职场文书
机械工程学院大学生求职信
2014/05/25 职场文书
小学学校门卫岗位职责
2014/08/03 职场文书
委托函范文
2015/01/29 职场文书
2019年大学生职业生涯规划书
2019/03/25 职场文书
python 调用js的四种方式
2021/04/11 Python
javascript拖曳互换div的位置实现示例
2021/06/28 Javascript
Redis实现一个账号只能登录一个设备
2022/04/19 Redis
MySQL数据库 安全管理
2022/05/06 MySQL
GoFrame基于性能测试得知grpool使用场景
2022/06/21 Golang