首页 > 作文

一棵php的类树(支持无限分类)

更新时间:2023-04-06 08:35:26 阅读: 评论:0

最近问无限分类的类树问题比较多,所以一高兴自己写了一个,我刚写完的,大家用用看,看看怎么实现起来更快,更简单,把你的树也贴出来(要只查询一次数据库的)<br>

这是一棵分类列表的类树, 支持无限分类<br>

一个分类下面可以同时有”包含子类的分类”和”最终分类”;<br>

唯一的优点是*****只需要进行一次的数据库*****查询.<br>

样子不是很好看,不过可以自定义修改,可以自己定义css加里面<br>

缓存方面还没有作,可以自己补上

下面例子的目录结构是这样的。

¦–catagory.php <br>

¦–images—-tree.jsp <br>

¦–images—-treeopen.gif <br>

¦–images—-treeclo.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;

}el{

node.style.display = ”;

document.images(‘img’+id).src = imgclo;

}

}

/****************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 $imgclo

* __open__ 分类当前是否是展开的

* __inner__ 子分类显示的位置 * 样式可以根据自己的需要任意修改 ,但是不能删除现有的元素

*/

var $blocktpl = ‘

<table border=”0″ cellpadding=”0″ cellspacing=”0″>

<tr>

<td colspan=”2″><a onclick=”expand(__id__); return fal;” href=”#”>

<img src=”__image__” border=”0″ width=”15″ height=”15″ id=”img__id__”></a>

<a onclick=”expand(__id__); return fal;” href=”#”>

__name__</a></td>

</tr>

<tr id=”node__id__” style=”display:__open__;”>

<td width=”20″></td><td>__inner__</td>

</tr>

</table>’;

/*

* 这个是小分类的模板

*

* e $blocktpl

*/

var $elementtpl = ‘<img src=”images/line.gif”顿组词语; width=”15″ height=”15″><a href=”?id=__id__”><font color=”white”>__name__</font></a><br/>’;

/*

* 这个是当前位置显示模板

*

* e $blocktpl

*/

var $currenttpl = ‘<a href=”?id=__id__”><font color=”white”>__name__</font></a>’;

var $js = “images/tree.js”;

var $imgopen = ‘images/treeopen.gif’;

var $imgclo = ‘images/treeclo.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 parnode($cataid=0){

$this->result = $this->treeplist[$cataid];

if($this->result==null) die(“catagory id error”);

$this->treestr = $this->_donode($this->result);

$this->treestr .= $this->_jspar();

}

function &_donode(&$result){

$nstr = $estr = ”;

foreach($result as $key=>$value){

if(ist($this->treeplist[$key])){

$result[$key] = $this->treeplist[$key];

$inner = $this->_donode($result[$key]);

$nstr .= $this->_par高考励志nodetpl($key, $inner);

}el{

$estr .= $this->_parelementtpl($key);

}

}

return $nstr.$estr;

}

function &_parnodetpl($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->imgclo:$this->imgopen), $str);

$str = preg_replace(‘ ¦__open__ ¦’, ($this->expand?”:’none’), $str);

$str = preg_replace(‘ ¦__inner__ ¦’, $inner, $str);

return $str;

}

function _parelementtpl($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 _jspar(){

$str = “<script language=\”javascript\”>

imgopen = \”$this->imgopen\”;

imgclo = \”$this->imgclo\”;

</script><script src=\”$this->js\” language=\”javascript\”></script>”;

return $str;

}

/*

* 展开分类$cataid

*

*param $cataid int 要展开的分类的编号

*/

function parcurrent($cataid){

$str = ”;

$str .= $this->_parcurrenttpl($cataid);

while(ist($this->treeclist[$cataid]) && $this->treeclist[$cataid]!=0){

$cataid = $this->treeclist[$cataid];

$str = $this->_parcurrenttpl($cataid).’->’.$str;

}

$this->currentstr = &$str;

}

function _parcurrenttpl($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(ist($this->treeplist[$cataid])) $str .= “expand($cataid);”;

while(ist($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 ttpl($blocktpl, $elementtpl, $currenttpl, $js){

$this->blocktpl = $blocktpl;

$this->elementtpl = $elementtpl;

$this->currenttpl = $currenttpl;

$this->js = $js;

}

function timage($open, $clo, $line){

$thi微笑的味道 作文s->imgopen = $open;

$this->imgclo = $clo;

$this->imgline = $line;

}

function texpend($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(‘idR我不想说再见 牛奶咖啡17;=>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, R二本毕业生16;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->texpend(1);

$tree->parnode(0);

//$tree->parnode(1);

//$tree->expand(9);

echo $tree->gettreestr();

echo “<hr>下面是当前分类(分类的编号是9)的路径<hr>”;

$tree->parcurrent(9);

echo $tree->getcurrentstr();

本文发布于:2023-04-06 08:35:24,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/8a647543b9ac8baade4840436771f31e.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:一棵php的类树(支持无限分类).doc

本文 PDF 下载地址:一棵php的类树(支持无限分类).pdf

标签:编号   路径   模板   数组
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图