这个无限分类是基于Thinkphp3.2做的实例,首先我们来说一下无限分类的类放在什么地方。
效果图
getList();//获取分类结构
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';
}
private function _findAllCat($condition, $orderby = NuLL) {
$this->rawList = empty($orderby) ? $this->model->where($condition)->select() : $this->model->where($condition)->order($orderby)->select();
}
public function getChild($fid) {
$childs = array();
foreach ($this->rawList as $Category) {
if ($Category[$this->fields['fid']] == $fid)
$childs[] = $Category;
}
return $childs;
}
private function _searchList($cid = 0, $space = "") {
$childs = $this->getChild($cid);
//下级分类的数组
//如果没下级分类,结束递归
if (!($n = count($childs)))
return;
$m = 1;
//循环所有的下级分类
for ($i = 0; $i < $n; $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++;
}
}
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;
}
public function getTree($data, $cid = 0) {
unset($this->rawList, $this->formatList);
$this->rawList = $data;
$this->_searchList($cid);
return $this->formatList;
}
public function getError() {
return $this->error;
}
private function _checkCatID($cid) {
if (intval($cid)) {
return true;
} else {
$this->error = "参数分类ID为空或者无效!";
return false;
}
}
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']]);
}
public function getPath($cid) {
unset($this->rawList, $this->formatList);
$this->_searchPath($cid); //查询分类路径
return array_reverse($this->formatList);
}
public function add($data) {
if (empty($data))
return false;
return $this->model->data($data)->add();
}
public function edit($data) {
if (empty($data))
return false;
return $this->model->data($data)->save();
}
public function del($cid) {
$cid = intval($cid);
if (empty($cid))
return false;
$conditon[$this->fields['cid']] = $cid;
return $this->model->where($conditon)->delete();
}
public function getIdArr($cid){
$cid = !empty($cid) ? intval($cid) : 0;
if (empty($cid)) return false;
$list = $this->getList($condition = NuLL,$cid, $orderby = NuLL);
foreach($list as $val){
$idArr[] = $val['cid'];
}
unset($list);
$idArr[] = $cid;
return $idArr;
}
}
?>数据库:lqb_category.sql 下载
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `lqb_category` -- ---------------------------- DROP TABLE IF EXISTS `lqb_category`; CREATE TABLE `lqb_category` ( `id` smallint(5) unsigned NOT NuLL AuTO_INCREMENT, `pid` smallint(5) unsigned NOT NuLL COMMENT 'parentCategory上级分类', `name` varchar(80) NOT NuLL COMMENT '分类名称', `sort` smallint(8) NOT NuLL DEFAuLT '0', `status` tinyint(1) unsigned NOT NuLL DEFAuLT '1', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAuLT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='新闻分类表'; -- ---------------------------- -- Records of lqb_category -- ----------------------------


