无限极分类在做项目中经常会用到,无限极分类的实现方法也多种实现形式,下面我分享一个基于thinkphp3.0的无极限分类,同时也支持普通传值的无极限分类。说明该类很久以前我在互联网上找到是,原编写者信息没有,这里我只是在原有的基础上做了些修改和格式化。
分类表需包含3个基本字段:cid,fid,name 即:分类cid,父级fid,分类名称。
表结构:CREATE TABLE `think_category` (<br>
`cid` int(11) NOT NULL AUTO_INCREMENT,<br>
`fid` int(11) DEFAULT NULL,<br>
`name` varchar(30) DEFAULT NULL<br>
PRIMARY KEY (`cid`)<br>
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
有两种使用方法:
第一种是基于THINKPHP3.0的使用:下载后将Category.class.php放到当前项目的ORG目录下(其他的目录也可只要能正常引用) import("@.ORG.Category");<br>
$cat = new Category('Category', array('cid', 'fid', 'name', 'fullname'));<br>
$s = $cat->getList(); //获取分类结构<br>
$s = $cat->getList('', 1); //获取fid=1的子分类结构<br>
$s = $cat->getList($condition, 1); //$condition为查询条件,获取fid=1的子分类结构<br>
$s = $cat->getPath(3); //获取分类id=3的路径<br>
$data = array("fid" => 0, "name" => "新分类名称");<br>
$s = $cat->add($data); //添加分类,$data需要包含上级分类fid<br>
$data = array("cid" => 2, "name" => "修改后分类名称");<br>
$s = $cat->edit($data); //修改分类,$data需要包含分类ID<br>
$s = $cat->del(10); //删除分类id=10的分类
第二种使用方法不需要使用TP的支持,但是数据结构需一样。
分类类文件Category.class.php内容:<?php <br />
<br>
/**<br>
+------------------------------------------------------------------------------<br>
* 分类管理<br>
+------------------------------------------------------------------------------<br>
*/<br>
class Category {<br>
<br>
private $model; //分类的数据表模型<br>
private $rawList = array(); //原始的分类数据<br>
private $formatList = array(); //格式化后的分类<br>
private $error = ""; //错误信息<br>
private $icon = array('  │', '  ├ ', '  └ '); //格式化的字符<br>
private $fields = array(); //字段映射,分类id,上级分类fid,分类名称name,格式化后分类名称fullname<br>
<br>
/**<br>
+----------------------------------------------------------<br>
* 构造函数,对象初始化<br>
+----------------------------------------------------------<br>
* @param array,object $model 数组或对象,基于TP3.0的数据表模型名称,若不采用TP,可传递空值。<br>
* @param array $field 字段映射,分类cid,上级分类fid,分类名称,格式化后分类名称fullname<br>
+----------------------------------------------------------<br>
*/<br>
<br>
public function __construct($model = '', $fields = array()) {<br>
if (is_string($model) && (!empty($model))) {<br>
if (!$this->model = D($model))<br>
$this->error = $model . "模型不存在!";<br>
}<br>
if (is_object($model))<br>
$this->model = &$model;<br>
<br>
$this->fields['cid'] = $fields['0'] ? $fields['0'] : 'cid';<br>
$this->fields['fid'] = $fields['1'] ? $fields['1'] : 'fid';<br>
$this->fields['name'] = $fields['2'] ? $fields['2'] : 'name';<br>
$this->fields['fullname'] = $fields['3'] ? $fields['3'] : 'fullname';<br>
}<br>
<br>
/**<br>
+----------------------------------------------------------<br>
* 获取分类信息数据<br>
+----------------------------------------------------------<br>
* @param array,string $condition 查询条件<br>
* @param string $orderby 排序<br>
+----------------------------------------------------------<br>
*/<br>
private function _findAllCat($condition, $orderby = NULL) {<br>
$this->rawList = empty($orderby) ? $this->model->where($condition)->select() : $this->model->where($condition)->order($orderby)->select();<br>
}<br>
<br>
/**<br>
+----------------------------------------------------------<br>
* 返回给定上级分类$fid的所有同一级子分类<br>
+----------------------------------------------------------<br>
* @param int $fid 传入要查询的fid<br>
+----------------------------------------------------------<br>
* @return array 返回结构信息<br>
+----------------------------------------------------------<br>
*/<br>
public function getChild($fid) {<br>
$childs = array();<br>
foreach ($this->rawList as $Category) {<br>
if ($Category[$this->fields['fid']] == $fid)<br>
$childs[] = $Category;<br>
}<br>
return $childs;<br>
}<br>
<br>
/**<br>
+----------------------------------------------------------<br>
* 递归格式化分类前的字符<br>
+----------------------------------------------------------<br>
* @param int $cid 分类cid<br>
* @param string $space<br>
+----------------------------------------------------------<br>
*/<br>
private function _searchList($cid = 0, $space = "") {<br>
$childs = $this->getChild($cid);<br>
//下级分类的数组<br>
//如果没下级分类,结束递归<br>
if (!($n = count($childs)))<br>
return;<br>
$m = 1;<br>
//循环所有的下级分类<br>
for ($i = 0; $i
$pre = "";<br>
$pad = "";<br>
if ($n == $m) {<br>
$pre = $this->icon[2];<br>
} else {<br>
$pre = $this->icon[1];<br>
$pad = $space ? $this->icon[0] : "";<br>
}<br>
$childs[$i][$this->fields['fullname']] = ($space ? $space . $pre : "") . $childs[$i][$this->fields['name']];<br>
$this->formatList[] = $childs[$i];<br>
$this->_searchList($childs[$i][$this->fields['cid']], $space . $pad . " "); //递归下一级分类<br>
$m++;<br>
}<br>
}<br>
<br>
/**<br>
+----------------------------------------------------------<br>
* 不采用数据模型时,可以从外部传递数据,得到递归格式化分类<br>
+----------------------------------------------------------<br>
* @param array,string $condition 条件<br>
* @param int $cid 起始分类<br>
* @param string $orderby 排序<br>
+----------------------------------------------------------<br>
* @return array 返回结构信息<br>
+----------------------------------------------------------<br>
*/<br>
public function getList($condition = NULL, $cid = 0, $orderby = NULL) {<br>
unset($this->rawList, $this->formatList);<br>
$this->_findAllCat($condition, $orderby, $orderby);<br>
$this->_searchList($cid);<br>
return $this->formatList;<br>
}<br>
<br>
/**<br>
+----------------------------------------------------------<br>
* 获取结构<br>
+----------------------------------------------------------<br>
* @param array $data 二维数组数据<br>
* @param int $cid 起始分类<br>
+----------------------------------------------------------<br>
* @return array 递归格式化分类数组<br>
+----------------------------------------------------------<br>
*/<br>
public function getTree($data, $cid = 0) {<br>
unset($this->rawList, $this->formatList);<br>
$this->rawList = $data;<br>
$this->_searchList($cid);<br>
return $this->formatList;<br>
}<br>
<br>
/**<br>
+----------------------------------------------------------<br>
* 获取错误信息<br>
+----------------------------------------------------------<br>
* @return string 错误信息字符串<br>
+----------------------------------------------------------<br>
*/<br>
public function getError() {<br>
return $this->error;<br>
}<br>
<br>
/**<br>
+----------------------------------------------------------<br>
* 检查分类参数$cid,是否为空<br>
+----------------------------------------------------------<br>
* @param int $cid 起始分类<br>
+----------------------------------------------------------<br>
* @return boolean 递归格式化分类数组<br>
+----------------------------------------------------------<br>
*/<br>
private function _checkCatID($cid) {<br>
if (intval($cid)) {<br>
return true;<br>
} else {<br>
$this->error = "参数分类ID为空或者无效!";<br>
return false;<br>
}<br>
}<br>
<br>
/**<br>
+----------------------------------------------------------<br>
* 检查分类参数$cid,是否为空<br>
+----------------------------------------------------------<br>
* @param int $cid 分类cid<br>
+----------------------------------------------------------<br>
*/<br>
private function _searchPath($cid) {<br>
//检查参数<br>
if (!$this->_checkCatID($cid))<br>
return false;<br>
$rs = $this->model->find($cid); //初始化对象,查找上级Id;<br>
$this->formatList[] = $rs; //保存结果<br>
$this->_searchPath($rs[$this->fields['fid']]);<br>
}<br>
<br>
/**<br>
+----------------------------------------------------------<br>
* 查询给定分类cid的路径<br>
+----------------------------------------------------------<br>
* @param int $cid 分类cid<br>
+----------------------------------------------------------<br>
* @return array 数组<br>
+----------------------------------------------------------<br>
*/<br>
public function getPath($cid) {<br>
unset($this->rawList, $this->formatList);<br>
$this->_searchPath($cid); //查询分类路径<br>
return array_reverse($this->formatList);<br>
}<br>
<br>
/**<br>
+----------------------------------------------------------<br>
* 添加分类<br>
+----------------------------------------------------------<br>
* @param array $data 一维数组,要添加的数据,$data需要包含上级分类ID。<br>
+----------------------------------------------------------<br>
* @return boolean 添加成功,返回相应的分类ID,添加失败,返回FALSE;<br>
+----------------------------------------------------------<br>
*/<br>
public function add($data) {<br>
if (empty($data))<br>
return false;<br>
return $this->model->data($data)->add();<br>
}<br>
<br>
/**<br>
+----------------------------------------------------------<br>
* 修改分类<br>
+----------------------------------------------------------<br>
* @param array $data 一维数组,$data需要包含要修改的分类cid。<br>
+----------------------------------------------------------<br>
* @return boolean 组修改成功,返回相应的分类ID,修改失败,返回FALSE;<br>
+----------------------------------------------------------<br>
*/<br>
public function edit($data) {<br>
if (empty($data))<br>
return false;<br>
return $this->model->data($data)->save();<br>
}<br>
<br>
/**<br>
+----------------------------------------------------------<br>
* 删除分类<br>
+----------------------------------------------------------<br>
* @param int $cid 分类cid<br>
+----------------------------------------------------------<br>
* @return boolean 删除成功,返回相应的分类ID,删除失败,返回FALSE<br>
+----------------------------------------------------------<br>
*/<br>
public function del($cid) {<br>
$cid = intval($cid);<br>
if (empty($cid))<br>
return false;<br>
$conditon[$this->fields['cid']] = $cid;<br>
return $this->model->where($conditon)->delete();<br>
}<br>
<br>
}<br>
?>
这个编辑器太不给力了,编辑框又这么小,详细的使用方法和测试包去我的blog下载吧,http://blog.51edm.org/post/78
AD:真正免费,域名+虚机+企业邮箱=0元