一棵php的类树(支持无限分类)
程序员文章站
2022-07-10 23:54:44
最近问无限分类的类树问题比较多,所以一高兴自己写了一个,我刚写完的,大家用用看,看看怎么实现起来更快,更简单...
最近问无限分类的类树问题比较多,所以一高兴自己写了一个,我刚写完的,大家用用看,看看怎么实现起来更快,更简单,把你的树也贴出来(要只查询一次数据库的)<br>
这是一棵分类列表的类树, 支持无限分类<br>
一个分类下面可以同时有"包含子类的分类"和"最终分类";<br>
唯一的优点是*****只需要进行一次的数据库*****查询.<br>
样子不是很好看,不过可以自定义修改,可以自己定义css加里面<br>
缓存方面还没有作,可以自己补上
下面例子的目录结构是这样的。
¦--catagory.php <br>
¦--images----tree.jsp <br>
¦--images----treeopen.gif <br>
¦--images----treeclose.gif <br>
¦--images----line.gif <br>
/****************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();
/*
* 这个是大分类的模板
*
* __id__ 分类的编号
* __name__ 分类的名称
* __image__ 分类前面显示的图像名称 $imgopen or $imgclose
* __open__ 分类当前是否是展开的
* __inner__ 子分类显示的位置 * 样式可以根据自己的需要任意修改 ,但是不能删除现有的元素
*/
var $blocktpl = '
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2"><a onclick="expand(__id__); return false;" href="#">
<img src="__image__" border="0" width="15" height="15" id="img__id__"></a>
<a onclick="expand(__id__); return false;" href="#">
__name__</a></td>
</tr>
<tr id="node__id__" style="display:__open__;">
<td width="20"></td><td>__inner__</td>
</tr>
</table>';
/*
* 这个是小分类的模板
*
* see $blocktpl
*/
var $elementtpl = '<img src="images/line.gif" width="15" height="15"><a href="?id=__id__"><font color="white">__name__</font></a><br/>';
/*
* 这个是当前位置显示模板
*
* see $blocktpl
*/
var $currenttpl = '<a href="?id=__id__"><font color="white">__name__</font></a>';
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 = "<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 "<hr>下面是当前分类的类树<hr>";
//$tree->setexpend(1);
$tree->parsenode(0);
//$tree->parsenode(1);
//$tree->expand(9);
echo $tree->gettreestr();
echo "<hr>下面是当前分类(分类的编号是9)的路径<hr>";
$tree->parsecurrent(9);
echo $tree->getcurrentstr();
上一篇: PHPlet在Windows下的安装
下一篇: PHP 中执行系统外部命令