最近問無限分類的類樹問題比較多,所以一高興自己寫了一個,我剛寫完的,大家用用看,看看怎麼實現起來更快,更簡單,把你的樹也貼出來(要只查詢一次資料庫的)<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();