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 相关文章推荐
最省空间的计数器
Oct 09 PHP
php在字符串中查找另一个字符串
Nov 19 PHP
Drupal 添加模块出现莫名其妙的错误的解决方法(往往出现在模块较多时)
Apr 18 PHP
fleaphp crud操作之findByField函数的使用方法
Apr 23 PHP
php设计模式 Factory(工厂模式)
Jun 26 PHP
PHP文章采集URL补全函数(FormatUrl)
Aug 02 PHP
php ios推送(代码)
Jul 01 PHP
PHP中include与require使用方法区别详解
Oct 19 PHP
php循环table实现一行两列显示的方法
Jun 04 PHP
windows8.1下Apache+Php+MySQL配置步骤
Oct 30 PHP
浅谈PHP接入(第三方登录)QQ登录 OAuth2.0 过程中遇到的坑
Oct 13 PHP
Laravel框架自定义公共函数的引入操作示例
Apr 16 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
redis 队列操作的例子(php)
2012/04/12 PHP
linux下使用crontab实现定时PHP计划任务失败的原因分析
2014/07/05 PHP
php命令行(cli)模式下报require 加载路径错误的解决方法
2015/11/23 PHP
详解HTTP Cookie状态管理机制
2016/01/14 PHP
网页常用特效代码整理
2006/06/23 Javascript
js调试工具 Javascript Debug Toolkit 2.0.0版本发布
2008/12/02 Javascript
JavaScript中数组的排序、乱序和搜索实现代码
2011/11/30 Javascript
统计jQuery中各字符串出现次数的工具
2012/05/03 Javascript
jQuery.query.js 取参数的两点问题分析
2012/08/06 Javascript
JQuery插件开发示例代码
2013/11/06 Javascript
jquery解析xml字符串简单示例
2014/04/11 Javascript
javascript获取dom的下一个节点方法
2014/09/05 Javascript
JsRender实用入门教程
2014/10/31 Javascript
jQuery中prependTo()方法用法实例
2015/01/08 Javascript
js读取并解析JSON类型数据的方法
2015/11/14 Javascript
很不错的两款Bootstrap Icon图标选择组件
2016/01/28 Javascript
如何判断Javascript对象是否存在的简单实例
2016/05/18 Javascript
详解JS中定时器setInterval和setTImeout的this指向问题
2017/01/06 Javascript
jQuery实现三级联动效果
2017/03/02 Javascript
React Native实现进度条弹框的示例代码
2017/07/17 Javascript
ES6中Array.find()和findIndex()函数的用法详解
2017/09/16 Javascript
JS使用栈判断给定字符串是否是回文算法示例
2019/03/04 Javascript
微信小程序动态添加view组件的实例代码
2019/05/23 Javascript
JS函数本身的作用域实例分析
2020/03/16 Javascript
vue element 关闭当前tab 跳转到上一路由操作
2020/07/22 Javascript
Antd表格滚动 宽度自适应 不换行的实例
2020/10/27 Javascript
Python基于pyCUDA实现GPU加速并行计算功能入门教程
2018/06/19 Python
python实现flappy bird小游戏
2018/12/24 Python
python安装pil库方法及代码
2019/06/25 Python
Python 给定的经纬度标注在地图上的实现方法
2019/07/05 Python
关于windows下Tensorflow和pytorch安装教程
2020/02/04 Python
社区国庆节活动方案
2014/02/05 职场文书
大学生个人求职口试自我评价
2014/02/16 职场文书
2016年三八节红领巾广播稿
2015/12/17 职场文书
2016大学生形势与政策心得体会
2016/01/12 职场文书
node.js如何自定义实现一个EventEmitter
2021/07/16 Javascript