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中基于ts与nts版本- vc6和vc9编译版本的区别详解
Apr 26 PHP
PHP中Header使用的HTTP协议及常用方法小结
Nov 04 PHP
Thinkphp搭建包括JS多语言的多语言项目实现方法
Nov 24 PHP
PHP读取txt文本文件并分页显示的方法
Mar 11 PHP
php实现RSA加密类实例
Mar 26 PHP
PHP、Java des加密解密实例
Apr 27 PHP
详解PHP序列化反序列化的方法
Oct 27 PHP
PHP+Ajax验证码验证用户登录
Jul 20 PHP
PHP实现的获取文件mimes类型工具类示例
Apr 08 PHP
Laravel第三方包报class not found的解决方法
Oct 13 PHP
php-7.3.6 编译安装过程
Feb 11 PHP
PHP基于array_unique实现二维数组去重
Jul 14 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过滤敏感词的示例
2014/03/31 PHP
PHP浮点数的一个常见问题
2016/03/10 PHP
PHP正则表达式入门教程(推荐)
2016/05/18 PHP
Laravel 类和接口注入相关的代码
2019/10/15 PHP
页面中js执行顺序
2009/11/09 Javascript
用nodejs实现PHP的print_r函数代码
2014/03/14 NodeJs
基于iframe实现类似于ajax的页面无刷新
2014/05/31 Javascript
JS显示下拉列表框内全部元素的方法
2015/03/31 Javascript
jQuery焦点图轮播特效代码分享(3款)
2015/09/05 Javascript
jquery彩色投票进度条简单实例演示
2020/07/23 Javascript
基于jquery实现三级下拉菜单
2016/05/10 Javascript
ionic2 tabs 图标自定义实例
2017/03/08 Javascript
基于vue 实现token验证的实例代码
2017/12/14 Javascript
json解析大全 双引号、键值对不在一起的情况
2019/12/06 Javascript
node 版本切换的实现
2020/02/02 Javascript
JS字符串补全方法padStart()和padEnd()
2020/05/27 Javascript
Openlayers绘制聚合标注
2020/09/28 Javascript
[01:17:12]职来职往完美电竞专场
2014/09/18 DOTA
Python爬虫实例_城市公交网络站点数据的爬取方法
2018/01/10 Python
python自动发邮件库yagmail的示例代码
2018/02/23 Python
用Python读取几十万行文本数据
2018/12/24 Python
django celery redis使用具体实践
2019/04/08 Python
详解Python3 pickle模块用法
2019/09/16 Python
Django框架ORM数据库操作实例详解
2019/11/07 Python
虚拟机下载python是否需要联网
2020/07/27 Python
详解Pytorch显存动态分配规律探索
2020/11/17 Python
python 爬虫爬取京东ps4售卖情况
2020/12/18 Python
CSS Grid布局教程之什么是网格布局
2014/12/30 HTML / CSS
意大利会呼吸的鞋:Geox健乐士
2017/02/12 全球购物
Clos19英国:高档香槟、葡萄酒和烈酒在线购物平台
2020/07/10 全球购物
Athleta官网:购买女士瑜伽服、技术运动服和休闲运动服
2020/11/12 全球购物
幼儿园教师教学反思
2014/02/06 职场文书
大学生团员个人总结
2015/02/14 职场文书
2015中学学校工作总结
2015/07/20 职场文书
幼儿园小班教学反思
2016/03/03 职场文书
干货:我将这样书写我的演讲稿!
2019/05/09 职场文书