PHP无限分类封装
<?php
/**
+------------------------------------------------
* 通用的树型类
+------------------------------------------------
* @author yangyunzhou@foxmail.com
+------------------------------------------------
* @date 2010年11月23日10:09:31
+------------------------------------------------
*/
class Tree
{ /**
+------------------------------------------------
* 生成树型结构所需要的2维数组
+------------------------------------------------
* @author yangyunzhou@foxmail.com
+------------------------------------------------
* @var Array
*/
var $arr = array(); /**
+------------------------------------------------
* 生成树型结构所需修饰符号,可以换成图片
+------------------------------------------------
* @author yangyunzhou@foxmail.com
+------------------------------------------------
* @var Array
*/
var $icon = array('│','├',' └'); /**
* @access private
*/
var $ret = ''; /**
* 构造函数,初始化类
* @param array 2维数组,例如:
* array(
* 1 => array('id'=>'1','parentid'=>0,'name'=>'一级栏目一'),
* 2 => array('id'=>'2','parentid'=>0,'name'=>'一级栏目二'),
* 3 => array('id'=>'3','parentid'=>1,'name'=>'二级栏目一'),
* 4 => array('id'=>'4','parentid'=>1,'name'=>'二级栏目二'),
* 5 => array('id'=>'5','parentid'=>2,'name'=>'二级栏目三'),
* 6 => array('id'=>'6','parentid'=>3,'name'=>'三级栏目一'),
* 7 => array('id'=>'7','parentid'=>3,'name'=>'三级栏目二')
* )
*/
function tree($arr=array())
{
$this->arr = $arr;
$this->ret = '';
return is_array($arr);
} /**
* 得到父级数组
* @param int
* @return array
*/
function get_parent($myid)
{
$newarr = array();
if(!isset($this->arr[$myid])) return false;
$pid = $this->arr[$myid]['parentid'];
$pid = $this->arr[$pid]['parentid'];
if(is_array($this->arr))
{
foreach($this->arr as $id => $a)
{
if($a['parentid'] == $pid) $newarr[$id] = $a;
}
}
return $newarr;
} /**
* 得到子级数组
* @param int
* @return array
*/
function get_child($myid)
{
$a = $newarr = array();
if(is_array($this->arr))
{
foreach($this->arr as $id => $a)
{
if($a['parentid'] == $myid) $newarr[$id] = $a;
}
}
return $newarr ? $newarr : false;
} /**
* 得到当前位置数组
* @param int
* @return array
*/
function get_pos($myid,&$newarr)
{
$a = array();
if(!isset($this->arr[$myid])) return false;
$newarr[] = $this->arr[$myid];
$pid = $this->arr[$myid]['parentid'];
if(isset($this->arr[$pid]))
{
$this->get_pos($pid,$newarr);
}
if(is_array($newarr))
{
krsort($newarr);
foreach($newarr as $v)
{
$a[$v['id']] = $v;
}
}
return $a;
} /**
* -------------------------------------
* 得到树型结构
* -------------------------------------
* @author yangyunzhou@foxmail.com
* @param $myid 表示获得这个ID下的所有子级
* @param $str 生成树形结构基本代码, 例如: "<option value=\$id \$select>\$spacer\$name</option>"
* @param $sid 被选中的ID, 比如在做树形下拉框的时候需要用到
* @param $adds
* @param $str_group
*/
function get_tree($myid, $str, $sid = 0, $adds = '', $str_group = '')
{
$number=1;
$child = $this->get_child($myid);
if(is_array($child)) {
$total = count($child);
foreach($child as $id=>$a) {
$j=$k='';
if($number==$total) {
$j .= $this->icon[2];
} else {
$j .= $this->icon[1];
$k = $adds ? $this->icon[0] : '';
}
$spacer = $adds ? $adds.$j : '';
$selected = $id==$sid ? 'selected' : '';
@extract($a);
$parentid == 0 && $str_group ? eval("\$nstr = \"$str_group\";") : eval("\$nstr = \"$str\";");
$this->ret .= $nstr;
$this->get_tree($id, $str, $sid, $adds.$k.' ',$str_group);
$number++;
}
}
return $this->ret;
} /**
* 同上一方法类似,但允许多选
*/
function get_tree_multi($myid, $str, $sid = 0, $adds = '')
{
$number=1;
$child = $this->get_child($myid);
if(is_array($child))
{
$total = count($child);
foreach($child as $id=>$a)
{
$j=$k='';
if($number==$total)
{
$j .= $this->icon[2];
}
else
{
$j .= $this->icon[1];
$k = $adds ? $this->icon[0] : '';
}
$spacer = $adds ? $adds.$j : ''; $selected = $this->have($sid,$id) ? 'selected' : '';
@extract($a);
eval("\$nstr = \"$str\";");
$this->ret .= $nstr;
$this->get_tree_multi($id, $str, $sid, $adds.$k.' ');
$number++;
}
}
return $this->ret;
} function have($list,$item){
return(strpos(',,'.$list.',',','.$item.','));
} /**
+------------------------------------------------
* 格式化数组
+------------------------------------------------
* @author yangyunzhou@foxmail.com
+------------------------------------------------
*/
function getArray($myid=0, $sid=0, $adds='')
{
$number=1;
$child = $this->get_child($myid);
if(is_array($child)) {
$total = count($child);
foreach($child as $id=>$a) {
$j=$k='';
if($number==$total) {
$j .= $this->icon[2];
} else {
$j .= $this->icon[1];
$k = $adds ? $this->icon[0] : '';
}
$spacer = $adds ? $adds.$j : '';
@extract($a);
$a['name'] = $spacer.' '.$a['name'];
$this->ret[$a['id']] = $a;
$fd = $adds.$k.' ';
$this->getArray($id, $sid, $fd);
$number++;
}
} return $this->ret;
}
}
?> 用法: $tree = new Tree; // new 之前请记得包含tree文件!
$tree->tree($data); // 数据格式请参考 tree方法上面的注释! // 如果使用数组, 请使用 getArray方法
$tree->getArray(); // 下拉菜单选项使用 get_tree方法
$tree->get_tree();
PHP无限分类封装的更多相关文章
- 后台树状菜单,js实现递归无限分类
//新闻类别管理 public function new_classify() { $arr = M('news_classify')->where("fid = 0")-& ...
- MySql无限分类数据结构--预排序遍历树算法
MySql无限分类数据结构--预排序遍历树算法 无限分类是我们开发中非常常见的应用,像论坛的的版块,CMS的类别,应用的地方特别多. 我们最常见最简单的方法就是在MySql里ID ,parentID, ...
- TreeView递归绑定无限分类数据
TreeView递归绑定无限分类数据 实现一个动态绑定,无限级分类数据时,需要将数据绑定到TreeView控件,分类表的结构是这样的: 字段 类型 Id int ParentId int Name N ...
- 关于无限分类的树状输出(id,name,pid)类型的
首先创建无限分类的数据表,我这里采用的是id.name.pid这种类型(当然还有很多种无限分类的方式了,比如:id.name.pid.path.left.right左右节点的形式) CREATE TA ...
- php递归方法实现无限分类实例
数组: 代码如下 复制代码 $items = array( array('id' => 1, 'pid' => 0, 'name' => '一级11' ), array('id' ...
- PHP+Mysql无限分类的方法汇总
无限分类是个老话题了,来看看PHP结合Mysql如何实现.第一种方法这种方法是很常见.很传统的一种,先看表结构表:categoryid int 主键,自增name varchar 分类名称pid in ...
- PHP全路径无限分类导航LINK代码实现
<?php /** * @param php全路径无限分类 */ include('db.inc.php'); function getPathCate($cateid){ $sql = &qu ...
- PHP全路径无限分类原理
全路径无限分类:以一个字段把他所有的父级id按顺序记录下来以此实现的无限分类叫做全路径无限分类 优点:查询方便 缺点:增加,移动分类时数据维护时稍微复杂.
- PHP无限分类分类导航LINK的代码实现
1. 代码数据库的结构: 2.要达到的效果 /** * @param php无限分类分类导航LINK的代码实现 */ include('db.inc.php'); function getCatePa ...
随机推荐
- java.lang.IllegalStateException: class utils.filter.ContentFilter is not a javax.servlet.Filter
1.错误描写叙述 2016-01-12 11:27:01.787:WARN:oejuc.AbstractLifeCycle:FAILED ContentFilter: java.lang.Illega ...
- 初尝CDN:什么是分布式服务节点?
什么是CDN?笔者初见时也是摸不着头脑,查阅了大量的资料才明白什么是CDN,笔者为您揭秘什么是CDN? CDN的全称是Content Delivery Network,即内容分发网络.CDN的通俗理解 ...
- CentOS上使用Squid+Stunnel搭建代理服务器教程
这篇文章主要介绍了CentOS上使用Squid+Stunnel搭建代理服务器教程,同时文中也介绍了用户认证的方法,适合于多用户共同使用代理,这种功能在国内用还是比较exciting的~需要的朋友可以参 ...
- MAC下一些常用的命令行
统计了一下工作中一些会常用到的简单命令,加强记忆 ls 查看当前终端目录下面的文件 ls -a "ls -a"会出现一些带.xxxx的文 ...
- jxta 2.8x启动了
http://chaupal.github.io/ ———————————————————————————————————————————————————————————————————— 至少两个月 ...
- Spring MVC文本框
以下示例显示如何使用Spring Web MVC框架在表单中使用文本框.首先使用Eclipse IDE来创建一个Web工程,按照以下步骤使用Spring Web Framework开发基于动态表单的W ...
- Unity3D学习笔记——NGUI使用常见问题
一:在Scene中右键创建UI组件. 首先要确定NGUI中的红框能看见,我的就是之前无法现实红框,所以右键无法新建UI组件, 原因是:UIPanel没启用. 启用方法: 启用前和启用后NGUI界面对比 ...
- cocos2d-x之读开发技术精解
引擎位置(依次往下): 游戏App->逻辑与规则->引擎->运行的平台->硬件接口(驱动运行库API) 1. 渲染框架 CCNode绘制基类(引擎核心类都继承于它,形成一个链表 ...
- Consul1 在window7安装
1.从下载地址:https://www.consul.io/downloads.html下载解压到某一地方.以本机为例:既解压到D:/tool下 2. 将consul.exe文件所在的文件路径添加到 ...
- 系统时钟和UART的设置
系统时钟: 在开发版上,不同的器件运行在不同的时钟频率上,如CPU可能运行在400Mhz的频率上.SDRAM.DM9000等内存存储运行在100Mhz~133MHz上. 串口i2c等运行在50Mhz上 ...