最近问无限分类的类树问题比较多,所以一高兴自己写了一个,我刚写完的,大家用用看,看看怎么实现起来更快,更简单,把你的树也贴出来(要只查询一次数据库的)
这是一棵分类列表的类树, 支持无限分类
一个分类下面可以同时有"包含子类的分类"和"最终分类";
唯一的优点是只需要进行一次的数据库查询.
样子不是很好看,不过可以自定义修改,可以自己定义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****/
<?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();
/*
__name__ | |
__inner__ |
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 = "<script language=\"javascript\">
imgopen = \"$this->imgOpen\";
imgclose = \"$this->imgClose\";
</script><script src=\"$this->js\" language=\"javascript\"></script>";
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 .= "<script language=\"javascript\">$str</script>";
}
/*
* 返回当前分类的路径
*/
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 "<body bgcolor=\"blue\">";
$tree = new Catagory($data);
echo "
echo "