一棵php的类树(支持无限分类)_php基础
程序员文章站
2024-01-09 16:22:46
...
最近问无限分类的类树问题比较多,所以一高兴自己写了一个,我刚写完的,大家用用看,看看怎么实现起来更快,更简单,把你的树也贴出来(要只查询一次数据库的)
这是一棵分类列表的类树, 支持无限分类
一个分类下面可以同时有"包含子类的分类"和"最终分类";
唯一的优点是*****只需要进行一次的数据库*****查询.
样子不是很好看,不过可以自定义修改,可以自己定义css加里面
缓存方面还没有作,可以自己补上
下面例子的目录结构是这样的。
¦--Catagory.php
¦--images----tree.jsp
¦--images----treeopen.gif
¦--images----treeclose.gif
¦--images----line.gif
/****************tree.jsp********************/
function expand(id){
node = document.all('node'+id);
if(node.style.display==''){
node.style.display = 'none';
document.images('img'+id).src = imgopen;
}else{
node.style.display = '';
document.images('img'+id).src = imgclose;
}
}
/****************Catagory.php********************/
define('CATAGORY_TREE_EXPEND_NONE',0);
define('CATAGORY_TREE_EXPEND_ALL',1);
class Catagory{
//基础分类数据
var $treeData = array();
//分类的等级结构数组,以分类的id值作为数组的关键字
var $treePList = array();
//自分类对应上级类的关系
var $treeCList = array();
/*
* 这个是大分类的模板
*
* __id__ 分类的编号
* __name__ 分类的名称
* __image__ 分类前面显示的图像名称 $imgOpen or $imgClose
* __open__ 分类当前是否是展开的
* __inner__ 子分类显示的位置 * 样式可以根据自己的需要任意修改 ,但是不能删除现有的元素
*/
var $blockTpl = '
';
/*
* 这个是小分类的模板
*
* see $blockTpl
*/
var $elementTpl = '__name__
';
/*
* 这个是当前位置显示模板
*
* see $blockTpl
*/
var $currentTpl = '__name__';
var $js = "images/tree.js";
var $imgOpen = 'images/treeopen.gif';
var $imgClose = 'images/treeclose.gif';
var $imgLine = 'images/line.gif';
var $cachFile = '';
var $expand = 0;
var $result = array();
var $treeStr = '';
var $currentStr = '';
/*
* 用来初始化,传入分类数据
*
*param $data array()
*/
function Catagory(&$data){
$this->_init($data);
}
function _init($tmpData){
$plevel = $clevel = $treeData = array(); foreach($tmpData as $value){
$treeData[$value['id']] = $value;
$plevel[$value['pid']][$value['id']] = 'END';
$clevel[$value['id']] = $value['pid'];
}
$this->treeData = &$treeData;
$this->treePList = &$plevel;
$this->treeCList = &$clevel;
}
/*
* 解析分类列表
*
*param $cataId int 要解析的主分类的编号
*/
function parseNode($cataId=0){
$this->result = $this->treePList[$cataId];
if($this->result==null) die("Catagory id error");
$this->treeStr = $this->_doNode($this->result);
$this->treeStr .= $this->_jsParse();
}
function &_doNode(&$result){
$nstr = $estr = '';
foreach($result as $key=>$value){
if(isset($this->treePList[$key])){
$result[$key] = $this->treePList[$key];
$inner = $this->_doNode($result[$key]);
$nstr .= $this->_parseNodeTpl($key, $inner);
}else{
$estr .= $this->_parseElementTpl($key);
}
}
return $nstr.$estr;
}
function &_parseNodeTpl($cataId, $inner){
$data = $this->treeData[$cataId];
$str = preg_replace(' ¦__id__ ¦', $data['id'], $this->blockTpl);
$str = preg_replace(' ¦__name__ ¦', $data['name'], $str);
$str = preg_replace(' ¦__image__ ¦', ($this->expand? $this->imgClose:$this->imgOpen), $str);
$str = preg_replace(' ¦__open__ ¦', ($this->expand?'':'none'), $str);
$str = preg_replace(' ¦__inner__ ¦', $inner, $str);
return $str;
}
function _parseElementTpl($cataId){
$data = $this->treeData[$cataId];
$str = preg_replace(' ¦__id__ ¦', $data['id'], $this->elementTpl);
$str = preg_replace(' ¦__name__ ¦', $data['name'], $str);
$str = preg_replace(' ¦__image__ ¦', $this->imgLine, $str);
return $str;
} function _jsParse(){
$str = "";
return $str;
}
/*
* 展开分类$cataId
*
*param $cataId int 要展开的分类的编号
*/
function parseCurrent($cataId){
$str = '';
$str .= $this->_parseCurrentTpl($cataId);
while(isset($this->treeCList[$cataId]) && $this->treeCList[$cataId]!=0){
$cataId = $this->treeCList[$cataId];
$str = $this->_parseCurrentTpl($cataId).'->'.$str;
}
$this->currentStr = &$str;
}
function _parseCurrentTpl($cataId){
$data = $this->treeData[$cataId];
$str = preg_replace(' ¦__id__ ¦', $data['id'], $this->currentTpl);
$str = preg_replace(' ¦__name__ ¦', $data['name'], $str);
return $str;
}
/*
* 解析当前分类的路径
*
*param $cataId int 要解析的主分类的编号
*/
function expand($cataId){
if($this->expand>0) return ;
$str = '';
if(isset($this->treePList[$cataId])) $str .= "expand($cataId);";
while(isset($this->treeCList[$cataId]) && $this->treeCList[$cataId]!=0){
$str .= "expand(".$this->treeCList[$cataId].");";
$cataId = $this->treeCList[$cataId];
}
$this->treeStr .= "";
}
/*
* 返回当前分类的路径
*/
function getCurrentStr(){ return $this->currentStr;
}
/*
* 返回分类的类树
*/
function getTreeStr(){
return $this->treeStr;
}
function setTpl($blockTpl, $elementTpl, $currentTpl, $js){
$this->blockTpl = $blockTpl;
$this->elementTpl = $elementTpl;
$this->currentTpl = $currentTpl;
$this->js = $js;
}
function setImage($open, $close, $line){
$this->imgOpen = $open;
$this->imgClose = $close;
$this->imgLine = $line;
}
function setExpend($expand){
$this->expand = $expand;
}
}
//分类的基础数据的样子如下:
$data = array(array('id'=>1, 'name'=>'name1', 'pid'=>0, 'order'=>1),
array('id'=>2, 'name'=>'name2', 'pid'=>1, 'order'=>1),
array('id'=>3, 'name'=>'name3', 'pid'=>0, 'order'=>1),
array('id'=>4, 'name'=>'name4', 'pid'=>3, 'order'=>1),
array('id'=>5, 'name'=>'name5', 'pid'=>6, 'order'=>1),
array('id'=>6, 'name'=>'name6', 'pid'=>2, 'order'=>1),
array('id'=>7, 'name'=>'name7', 'pid'=>6, 'order'=>1),
array('id'=>8, 'name'=>'name8', 'pid'=>3, 'order'=>1),
array('id'=>9, 'name'=>'name9', 'pid'=>6, 'order'=>1),
array('id'=>10, 'name'=>'name10', 'pid'=>0, 'order'=>1),
array('id'=>11, 'name'=>'name11', 'pid'=>10, 'order'=>1),
array('id'=>12, 'name'=>'name12', 'pid'=>10, 'order'=>1),
array('id'=>13, 'name'=>'name13', 'pid'=>10, 'order'=>1),
array('id'=>14, 'name'=>'name14', 'pid'=>12, 'order'=>1),
array('id'=>15, 'name'=>'name15', 'pid'=>12, 'order'=>4),
); echo "";
$tree = new Catagory($data);
echo "
下面是当前分类的类树
";
//$tree->setExpend(1);
$tree->parseNode(0);
//$tree->parseNode(1);
//$tree->expand(9);
echo $tree->getTreeStr();
echo "
下面是当前分类(分类的编号是9)的路径
";
$tree->parseCurrent(9);
echo $tree->getCurrentStr();
这是一棵分类列表的类树, 支持无限分类
一个分类下面可以同时有"包含子类的分类"和"最终分类";
唯一的优点是*****只需要进行一次的数据库*****查询.
样子不是很好看,不过可以自定义修改,可以自己定义css加里面
缓存方面还没有作,可以自己补上
下面例子的目录结构是这样的。
¦--Catagory.php
¦--images----tree.jsp
¦--images----treeopen.gif
¦--images----treeclose.gif
¦--images----line.gif
/****************tree.jsp********************/
function expand(id){
node = document.all('node'+id);
if(node.style.display==''){
node.style.display = 'none';
document.images('img'+id).src = imgopen;
}else{
node.style.display = '';
document.images('img'+id).src = imgclose;
}
}
/****************Catagory.php********************/
define('CATAGORY_TREE_EXPEND_NONE',0);
define('CATAGORY_TREE_EXPEND_ALL',1);
class Catagory{
//基础分类数据
var $treeData = array();
//分类的等级结构数组,以分类的id值作为数组的关键字
var $treePList = array();
//自分类对应上级类的关系
var $treeCList = array();
/*
* 这个是大分类的模板
*
* __id__ 分类的编号
* __name__ 分类的名称
* __image__ 分类前面显示的图像名称 $imgOpen or $imgClose
* __open__ 分类当前是否是展开的
* __inner__ 子分类显示的位置 * 样式可以根据自己的需要任意修改 ,但是不能删除现有的元素
*/
var $blockTpl = '
__name__ | |
__inner__ |
/*
* 这个是小分类的模板
*
* see $blockTpl
*/
var $elementTpl = '__name__
';
/*
* 这个是当前位置显示模板
*
* see $blockTpl
*/
var $currentTpl = '__name__';
var $js = "images/tree.js";
var $imgOpen = 'images/treeopen.gif';
var $imgClose = 'images/treeclose.gif';
var $imgLine = 'images/line.gif';
var $cachFile = '';
var $expand = 0;
var $result = array();
var $treeStr = '';
var $currentStr = '';
/*
* 用来初始化,传入分类数据
*
*param $data array()
*/
function Catagory(&$data){
$this->_init($data);
}
function _init($tmpData){
$plevel = $clevel = $treeData = array(); foreach($tmpData as $value){
$treeData[$value['id']] = $value;
$plevel[$value['pid']][$value['id']] = 'END';
$clevel[$value['id']] = $value['pid'];
}
$this->treeData = &$treeData;
$this->treePList = &$plevel;
$this->treeCList = &$clevel;
}
/*
* 解析分类列表
*
*param $cataId int 要解析的主分类的编号
*/
function parseNode($cataId=0){
$this->result = $this->treePList[$cataId];
if($this->result==null) die("Catagory id error");
$this->treeStr = $this->_doNode($this->result);
$this->treeStr .= $this->_jsParse();
}
function &_doNode(&$result){
$nstr = $estr = '';
foreach($result as $key=>$value){
if(isset($this->treePList[$key])){
$result[$key] = $this->treePList[$key];
$inner = $this->_doNode($result[$key]);
$nstr .= $this->_parseNodeTpl($key, $inner);
}else{
$estr .= $this->_parseElementTpl($key);
}
}
return $nstr.$estr;
}
function &_parseNodeTpl($cataId, $inner){
$data = $this->treeData[$cataId];
$str = preg_replace(' ¦__id__ ¦', $data['id'], $this->blockTpl);
$str = preg_replace(' ¦__name__ ¦', $data['name'], $str);
$str = preg_replace(' ¦__image__ ¦', ($this->expand? $this->imgClose:$this->imgOpen), $str);
$str = preg_replace(' ¦__open__ ¦', ($this->expand?'':'none'), $str);
$str = preg_replace(' ¦__inner__ ¦', $inner, $str);
return $str;
}
function _parseElementTpl($cataId){
$data = $this->treeData[$cataId];
$str = preg_replace(' ¦__id__ ¦', $data['id'], $this->elementTpl);
$str = preg_replace(' ¦__name__ ¦', $data['name'], $str);
$str = preg_replace(' ¦__image__ ¦', $this->imgLine, $str);
return $str;
} function _jsParse(){
$str = "";
return $str;
}
/*
* 展开分类$cataId
*
*param $cataId int 要展开的分类的编号
*/
function parseCurrent($cataId){
$str = '';
$str .= $this->_parseCurrentTpl($cataId);
while(isset($this->treeCList[$cataId]) && $this->treeCList[$cataId]!=0){
$cataId = $this->treeCList[$cataId];
$str = $this->_parseCurrentTpl($cataId).'->'.$str;
}
$this->currentStr = &$str;
}
function _parseCurrentTpl($cataId){
$data = $this->treeData[$cataId];
$str = preg_replace(' ¦__id__ ¦', $data['id'], $this->currentTpl);
$str = preg_replace(' ¦__name__ ¦', $data['name'], $str);
return $str;
}
/*
* 解析当前分类的路径
*
*param $cataId int 要解析的主分类的编号
*/
function expand($cataId){
if($this->expand>0) return ;
$str = '';
if(isset($this->treePList[$cataId])) $str .= "expand($cataId);";
while(isset($this->treeCList[$cataId]) && $this->treeCList[$cataId]!=0){
$str .= "expand(".$this->treeCList[$cataId].");";
$cataId = $this->treeCList[$cataId];
}
$this->treeStr .= "";
}
/*
* 返回当前分类的路径
*/
function getCurrentStr(){ return $this->currentStr;
}
/*
* 返回分类的类树
*/
function getTreeStr(){
return $this->treeStr;
}
function setTpl($blockTpl, $elementTpl, $currentTpl, $js){
$this->blockTpl = $blockTpl;
$this->elementTpl = $elementTpl;
$this->currentTpl = $currentTpl;
$this->js = $js;
}
function setImage($open, $close, $line){
$this->imgOpen = $open;
$this->imgClose = $close;
$this->imgLine = $line;
}
function setExpend($expand){
$this->expand = $expand;
}
}
//分类的基础数据的样子如下:
$data = array(array('id'=>1, 'name'=>'name1', 'pid'=>0, 'order'=>1),
array('id'=>2, 'name'=>'name2', 'pid'=>1, 'order'=>1),
array('id'=>3, 'name'=>'name3', 'pid'=>0, 'order'=>1),
array('id'=>4, 'name'=>'name4', 'pid'=>3, 'order'=>1),
array('id'=>5, 'name'=>'name5', 'pid'=>6, 'order'=>1),
array('id'=>6, 'name'=>'name6', 'pid'=>2, 'order'=>1),
array('id'=>7, 'name'=>'name7', 'pid'=>6, 'order'=>1),
array('id'=>8, 'name'=>'name8', 'pid'=>3, 'order'=>1),
array('id'=>9, 'name'=>'name9', 'pid'=>6, 'order'=>1),
array('id'=>10, 'name'=>'name10', 'pid'=>0, 'order'=>1),
array('id'=>11, 'name'=>'name11', 'pid'=>10, 'order'=>1),
array('id'=>12, 'name'=>'name12', 'pid'=>10, 'order'=>1),
array('id'=>13, 'name'=>'name13', 'pid'=>10, 'order'=>1),
array('id'=>14, 'name'=>'name14', 'pid'=>12, 'order'=>1),
array('id'=>15, 'name'=>'name15', 'pid'=>12, 'order'=>4),
); echo "";
$tree = new Catagory($data);
echo "
下面是当前分类的类树
";
//$tree->setExpend(1);
$tree->parseNode(0);
//$tree->parseNode(1);
//$tree->expand(9);
echo $tree->getTreeStr();
echo "
下面是当前分类(分类的编号是9)的路径
";
$tree->parseCurrent(9);
echo $tree->getCurrentStr();