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 相关文章推荐
Yii中render和renderPartial的区别
Sep 03 PHP
php制作动态随机验证码
Feb 12 PHP
php获取网页上所有链接的方法
Apr 03 PHP
PHP访问数据库集群的方法小结
Mar 14 PHP
PHP使用new StdClass()创建空对象的方法分析
Jun 06 PHP
PHP-X系列教程之内置函数的使用示例
Oct 16 PHP
php把字符串指定字符分割成数组的方法
Mar 12 PHP
PHP编程实现的TCP服务端和客户端功能示例
Apr 13 PHP
PHPExcel 修改已存在Excel的方法
May 03 PHP
ThinkPHP5 的简单搭建和使用详解
Nov 15 PHP
PHP实现的AES 128位加密算法示例
Sep 16 PHP
解决Laravel使用验证时跳转到首页的问题
Nov 17 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
windows下apache搭建php开发环境
2015/08/27 PHP
PHP 判断字符串是中文还是英文, 或者是中英混合
2021/03/09 PHP
如何在标题栏显示框架内页面的标题
2007/02/03 Javascript
Extjs列表详细信息窗口新建后自动加载解决方法
2010/04/02 Javascript
瀑布流布局并自动加载实现代码
2013/03/12 Javascript
jquery数据验证插件(自制,简单,练手)实例代码
2013/10/24 Javascript
javascript操作html控件实例(javascript添加html)
2013/12/02 Javascript
IE8下jQuery改变png图片透明度时出现的黑边
2015/08/30 Javascript
jQuery往返城市和日期查询实例讲解
2015/10/09 Javascript
JavaScript+html5 canvas绘制渐变区域完整实例
2016/01/26 Javascript
javascript+css3 实现动态按钮菜单特效
2016/02/06 Javascript
Angular 页面跳转时传参问题
2016/08/01 Javascript
详解vuex 中的 state 在组件中如何监听
2017/05/23 Javascript
js获取一组日期中最近连续的天数
2017/05/25 Javascript
浅析Vue中method与computed的区别
2018/03/06 Javascript
小程序开发基础之view视图容器
2018/08/21 Javascript
详解Axios 如何取消已发送的请求
2018/10/20 Javascript
Vue开发之封装分页组件与使用示例
2019/04/25 Javascript
新手入门js闭包学习过程解析
2019/10/08 Javascript
对vuex中getters计算过滤操作详解
2019/11/06 Javascript
Python实现的Google IP 可用性检测脚本
2015/04/23 Python
python3.6.3+opencv3.3.0实现动态人脸捕获
2018/05/25 Python
numpy添加新的维度:newaxis的方法
2018/08/02 Python
python判断一个对象是否可迭代的例子
2019/07/22 Python
Python读取实时数据流示例
2019/12/02 Python
详解Python中字符串前“b”,“r”,“u”,“f”的作用
2019/12/18 Python
Python文件操作模拟用户登陆代码实例
2020/06/09 Python
CSS3中Transition动画属性用法详解
2016/07/04 HTML / CSS
金融专业应届生求职信
2013/11/02 职场文书
国贸专业的职业规划范文
2014/01/23 职场文书
优秀共产党员先进事迹
2014/01/27 职场文书
《蚂蚁和蝈蝈》教学反思
2014/02/24 职场文书
关于运动会的宣传稿
2015/07/23 职场文书
2016年元旦致辞
2015/08/01 职场文书
详解Vue router路由
2021/11/20 Vue.js
sql时间段切分实现每隔x分钟出一份高速门架车流量
2022/02/28 SQL Server