分类表需包含3个基本字段:cid,fid,name 即:分类cid,父级fid,分类名称。
表结构:create table `think_category` (
`cid` int(11) not null auto_increment,
`fid` int(11) default null,
`name` varchar(30) default null
primary key (`cid`)
) engine=innodb auto_increment=1 default charset=utf8;有两种使用方法:
第一种是基于thinkphp3.0的使用:下载后将category.class.php放到当前项目的org目录下(其他的目录也可只要能正常引用) import(@.org.category);
$cat = new category('category', array('cid', 'fid', 'name', 'fullname'));
$s = $cat->getlist(); //获取分类结构
$s = $cat->getlist('', 1); //获取fid=1的子分类结构
$s = $cat->getlist($condition, 1); //$condition为查询条件,获取fid=1的子分类结构
$s = $cat->getpath(3); //获取分类id=3的路径
$data = array(fid => 0, name => 新分类名称);
$s = $cat->add($data); //添加分类,$data需要包含上级分类fid
$data = array(cid => 2, name => 修改后分类名称);
$s = $cat->edit($data); //修改分类,$data需要包含分类id
$s = $cat->del(10); //删除分类id=10的分类第二种使用方法不需要使用tp的支持,但是数据结构需一样。
分类类文件category.class.php内容:model = d($model))
$this->error = $model . 模型不存在!;
}
if (is_object($model))
$this->model = &$model;
$this->fields['cid'] = $fields['0'] ? $fields['0'] : 'cid';
$this->fields['fid'] = $fields['1'] ? $fields['1'] : 'fid';
$this->fields['name'] = $fields['2'] ? $fields['2'] : 'name';
$this->fields['fullname'] = $fields['3'] ? $fields['3'] : 'fullname';
}
/**
+----------------------------------------------------------
* 获取分类信息数据
+----------------------------------------------------------
* @param array,string $condition 查询条件
* @param string $orderby 排序
+----------------------------------------------------------
*/
private function _findallcat($condition, $orderby = null) {
$this->rawlist = empty($orderby) ? $this->model->where($condition)->select() : $this->model->where($condition)->order($orderby)->select();
}
/**
+----------------------------------------------------------
* 返回给定上级分类$fid的所有同一级子分类
+----------------------------------------------------------
* @param int $fid 传入要查询的fid
+----------------------------------------------------------
* @return array 返回结构信息
+----------------------------------------------------------
*/
public function getchild($fid) {
$childs = array();
foreach ($this->rawlist as $category) {
if ($category[$this->fields['fid']] == $fid)
$childs[] = $category;
}
return $childs;
}
/**
+----------------------------------------------------------
* 递归格式化分类前的字符
+----------------------------------------------------------
* @param int $cid 分类cid
* @param string $space
+----------------------------------------------------------
*/
private function _searchlist($cid = 0, $space = ) {
$childs = $this->getchild($cid);
//下级分类的数组
//如果没下级分类,结束递归
if (!($n = count($childs)))
return;
$m = 1;
//循环所有的下级分类
for ($i = 0; $i $pre = ;
$pad = ;
if ($n == $m) {
$pre = $this->icon[2];
} else {
$pre = $this->icon[1];
$pad = $space ? $this->icon[0] : ;
}
$childs[$i][$this->fields['fullname']] = ($space ? $space . $pre : ) . $childs[$i][$this->fields['name']];
$this->formatlist[] = $childs[$i];
$this->_searchlist($childs[$i][$this->fields['cid']], $space . $pad . ); //递归下一级分类
$m++;
}
}
/**
+----------------------------------------------------------
* 不采用数据模型时,可以从外部传递数据,得到递归格式化分类
+----------------------------------------------------------
* @param array,string $condition 条件
* @param int $cid 起始分类
* @param string $orderby 排序
+----------------------------------------------------------
* @return array 返回结构信息
+----------------------------------------------------------
*/
public function getlist($condition = null, $cid = 0, $orderby = null) {
unset($this->rawlist, $this->formatlist);
$this->_findallcat($condition, $orderby, $orderby);
$this->_searchlist($cid);
return $this->formatlist;
}
/**
+----------------------------------------------------------
* 获取结构
+----------------------------------------------------------
* @param array $data 二维数组数据
* @param int $cid 起始分类
+----------------------------------------------------------
* @return array 递归格式化分类数组
+----------------------------------------------------------
*/
public function gettree($data, $cid = 0) {
unset($this->rawlist, $this->formatlist);
$this->rawlist = $data;
$this->_searchlist($cid);
return $this->formatlist;
}
/**
+----------------------------------------------------------
* 获取错误信息
+----------------------------------------------------------
* @return string 错误信息字符串
+----------------------------------------------------------
*/
public function geterror() {
return $this->error;
}
/**
+----------------------------------------------------------
* 检查分类参数$cid,是否为空
+----------------------------------------------------------
* @param int $cid 起始分类
+----------------------------------------------------------
* @return boolean 递归格式化分类数组
+----------------------------------------------------------
*/
private function _checkcatid($cid) {
if (intval($cid)) {
return true;
} else {
$this->error = 参数分类id为空或者无效!;
return false;
}
}
/**
+----------------------------------------------------------
* 检查分类参数$cid,是否为空
+----------------------------------------------------------
* @param int $cid 分类cid
+----------------------------------------------------------
*/
private function _searchpath($cid) {
//检查参数
if (!$this->_checkcatid($cid))
return false;
$rs = $this->model->find($cid); //初始化对象,查找上级id;
$this->formatlist[] = $rs; //保存结果
$this->_searchpath($rs[$this->fields['fid']]);
}
/**
+----------------------------------------------------------
* 查询给定分类cid的路径
+----------------------------------------------------------
* @param int $cid 分类cid
+----------------------------------------------------------
* @return array 数组
+----------------------------------------------------------
*/
public function getpath($cid) {
unset($this->rawlist, $this->formatlist);
$this->_searchpath($cid); //查询分类路径
return array_reverse($this->formatlist);
}
/**
+----------------------------------------------------------
* 添加分类
+----------------------------------------------------------
* @param array $data 一维数组,要添加的数据,$data需要包含上级分类id。
+----------------------------------------------------------
* @return boolean 添加成功,返回相应的分类id,添加失败,返回false;
+----------------------------------------------------------
*/
public function add($data) {
if (empty($data))
return false;
return $this->model->data($data)->add();
}
/**
+----------------------------------------------------------
* 修改分类
+----------------------------------------------------------
* @param array $data 一维数组,$data需要包含要修改的分类cid。
+----------------------------------------------------------
* @return boolean 组修改成功,返回相应的分类id,修改失败,返回false;
+----------------------------------------------------------
*/
public function edit($data) {
if (empty($data))
return false;
return $this->model->data($data)->save();
}
/**
+----------------------------------------------------------
* 删除分类
+----------------------------------------------------------
* @param int $cid 分类cid
+----------------------------------------------------------
* @return boolean 删除成功,返回相应的分类id,删除失败,返回false
+----------------------------------------------------------
*/
public function del($cid) {
$cid = intval($cid);
if (empty($cid))
return false;
$conditon[$this->fields['cid']] = $cid;
return $this->model->where($conditon)->delete();
}
}
?>这个编辑器太不给力了,编辑框又这么小,详细的使用方法和测试包去我的blog下载吧,http://blog.51edm.org/post/78
ad:真正免费,域名+虚机+企业邮箱=0元