最近问无限分类的类树问题比较多,所以一高兴自己写了一个,我刚写完的,大家用用看,看看怎么实现起来更快,更简单,把你的树也贴出来
这是一棵分类列表的类树, 支持无限分类
一个分类下面可以同时有”包含子类的分类”和”最终分类”;
唯一的优点是*****只需要进行一次的数据库*****查询.
样子不是很好看,不过可以自定义修改,可以自己定义css加里面
缓存方面还没有作,可以自己补上 下面例子的目录结构是这样的。
¦–Catagory.php
¦–images—-tree.jsp
¦–images—-treeopen.gif
¦–images—-treeclose.gif
¦–images—-line.gif
/****************tree.jsp********************/
function expand{ node = document.all; if(node.style.display==”){
node.style.display = ‘none’; document.images.src = imgopen; }else{
node.style.display = ”; document.images.src = imgclose; } }
/****************Catagory.php********************/
imgopen = “$this->imgOpen”; imgclose = “$this->imgClose”;

“; return $str; } /* * 展开分类$cataId * *param $cataId int
要展开的分类的编号 */ function parseCurrent{ $str = ”; $str .=
$this->_parseCurrentTpl; while(isset($this->treeCList[$cataId])
&& $this->treeCList[$cataId]!=0){ $cataId =
$this->treeCList[$cataId]; $str =
$this->_parseCurrentTpl.’->’.$str; } $this->currentStr =
&$str; } function _parseCurrentTpl{ $data =
$this->treeData[$cataId]; $str = preg_replace(‘ ¦__id__ ¦’,
$data[‘id’], $this->currentTpl); $str = preg_replace(‘
¦__name__ ¦’, $data[‘name’], $str); return $str; } /* *
解析当前分类的路径 *新澳门31999 ,一棵php的类树_php基础_脚本之家。 *param $cataId int 要解析的主分类的编号 */
function expand{ if return ; $str = ”;
if(isset($this->treePList[$cataId])) $str .= “expand;”;
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{ $this->imgOpen = $open; $this->imgClose =
$close; $this->imgLine = $line; } function setExpend{
$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;echo “


下面是当前分类的类树


“;//$tree->setExpend;$tree->parseNode;//$tree->parseNode;echo
$tree->getTreeStr();echo “


下面是当前分类的路径


“;$tree->parseCurrent;echo $tree->getCurrentStr();

发表评论

电子邮件地址不会被公开。 必填项已用*标注