最近问无限分类的类树问题比较多,所以一高兴自己写了一个,我刚写完的,大家用用看,看看怎么实现起来更快,更简单,把你的树也贴出来(要只查询一次数据库的)
这是一棵分类列表的类树, 支持无限分类
一个分类下面可以同时有包含子类的分类和最终分类;
唯一的优点是*****只需要进行一次的数据库*****查询.
样子不是很好看,不过可以自定义修改,可以自己定义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********************/
_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();
http://www.bkjia.com/phpjc/314589.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/314589.htmltecharticle最近问无限分类的类树问题比较多,所以一高兴自己写了一个,我刚写完的,大家用用看,看看怎么实现起来更快,更简单,把你的树也贴...