|
以下是使用該類庫的方法 include("Common/Category.class.php");
$Category = new Category("ArticleCategory",array('id','pid','name','fullname'));
$categoryList = $Category->getList();1、通過include包含類庫 2、通過new實例化類 3、調(diào)用getList()方法獲取所有分類列表 4、返回:所有分類列表,可以通過獲取fullname顯示參考。 效果如圖:
以下是類庫完整源碼: <?php
/**
* 類功能:php無限分類
* author:252588119@qq.com
* 使用方法見:http:///blog-434.html
*/
class Category {
private $model; //分類的數(shù)據(jù)表模型
private $rawList = array(); //原始的分類數(shù)據(jù)
private $formatList = array(); //格式化后的分類
private $error = ""; //錯誤信息
private $icon = array(' │', ' ├ ', ' └ '); //格式化的字符
private $fields = array(); //字段映射,分類id,上級分類pid,分類名稱name,格式化后分類名稱fullname
/**
* 構(gòu)造函數(shù),對象初始化
* @param array,object $model 數(shù)組或?qū)ο?,基于TP3.0的數(shù)據(jù)表模型名稱,若不采用TP,可傳遞空值。
* @param array $field 字段映射,分類cid,上級分類pid,分類名稱,格式化后分類名稱fullname
*/
public function __construct($model = '', $fields = array()) {
if (is_string($model) && (!empty($model))) {
if (!$this->model = D($model))
$this->error = $model . "模型不存在!";
}
if (is_object($model))
$this->model = &$model;
$this->fields['cid'] = $fields['0'] ? $fields['0'] : 'id';
$this->fields['pid'] = $fields['1'] ? $fields['1'] : 'pid';
$this->fields['name'] = $fields['2'] ? $fields['2'] : 'name';
$this->fields['fullname'] = $fields['3'] ? $fields['3'] : 'fullname';
}
/**
* 獲取分類信息數(shù)據(jù)
* @param array,string $condition 查詢條件
* @param string $orderby 排序
*/
private function _findAllCat($condition, $orderby = NULL) {
$this->rawList = $this->model->where($condition)->order($orderby)->select();
}
/**
* 返回給定上級分類$pid的所有同一級子分類
* @param int $pid 傳入要查詢的pid
* @return array 返回結(jié)構(gòu)信息
*/
public function getChild($pid) {
$childs = array();
foreach ($this->rawList as $Category) {
if ($Category[$this->fields['pid']] == $pid){
$childs[] = $Category;
}
}
return $childs;
}
/**
* 遞歸格式化分類前的字符
* @param int $cid 分類cid
* @param string $space
*/
private function _searchList($cid = 0, $space = "") {
$childs = $this->getChild($cid);
//下級分類的數(shù)組
//如果沒下級分類,結(jié)束遞歸
if (!($n = count($childs))){
return;
}
$m = 1;
//循環(huán)所有的下級分類
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++;
}
}
/**
* 不采用數(shù)據(jù)模型時,可以從外部傳遞數(shù)據(jù),得到遞歸格式化分類
* @param array,string $condition 條件
* @param int $cid 起始分類
* @param string $orderby 排序
* @return array 返回結(jié)構(gòu)信息
*/
public function getList($condition = NULL, $cid = 0, $orderby = NULL) {
unset($this->rawList, $this->formatList);
$this->_findAllCat($condition, $orderby);
$this->_searchList($cid);
return $this->formatList;
}
/**
* 獲取結(jié)構(gòu)
* @param array $data 二維數(shù)組數(shù)據(jù)
* @param int $cid 起始分類
* @return array 遞歸格式化分類數(shù)組
*/
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;
}
/**
* 檢查分類參數(shù)$cid,是否為空
* @param int $cid 起始分類
* @return boolean 遞歸格式化分類數(shù)組
*/
private function _checkCatID($cid) {
if (intval($cid)) {
return true;
} else {
$this->error = "參數(shù)分類ID為空或者無效!";
return false;
}
}
/**
* 檢查分類參數(shù)$cid,是否為空
* @param int $cid 分類cid
*/
private function _searchPath($cid) {
//檢查參數(shù)
if (!$this->_checkCatID($cid))
return false;
$rs = $this->model->find($cid); //初始化對象,查找上級Id;
$this->formatList[] = $rs; //保存結(jié)果
$this->_searchPath($rs[$this->fields['pid']]);
}
/**
* 查詢給定分類cid的路徑
* @param int $cid 分類cid
* @return array 數(shù)組
*/
public function getPath($cid) {
unset($this->rawList, $this->formatList);
$this->_searchPath($cid); //查詢分類路徑
return array_reverse($this->formatList);
}
/**
* 添加分類
* @param array $data 一維數(shù)組,要添加的數(shù)據(jù),$data需要包含上級分類ID。
* @return boolean 添加成功,返回相應(yīng)的分類ID,添加失敗,返回FALSE;
*/
public function add($data) {
if (empty($data))
return false;
return $this->model->data($data)->add();
}
/**
* 修改分類
* @param array $data 一維數(shù)組,$data需要包含要修改的分類cid。
* @return boolean 組修改成功,返回相應(yīng)的分類ID,修改失敗,返回FALSE;
*/
public function edit($data) {
if (empty($data))
return false;
return $this->model->data($data)->save();
}
/**
* 刪除分類
* @param int $cid 分類cid
* @return boolean 刪除成功,返回相應(yīng)的分類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();
}
/**
* 刪除分類
* @param int $cid 分類cid
* @return boolean 刪除成功,返回相應(yīng)的分類ID及所有子ID 數(shù)組,返回FALSE
*/
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[$this->fields['cid']];
}
unset($list);
$idArr[] = $cid;
return $idArr;
}
}
?>demo里包含一個文件夾,三個文件。Helper文件夾包含了無限分類處理類,文件夾放在Application/Common/目錄下,CategoryController.class.php是控制器文件,用來演示如何使用無限分類處理類,控制器使用無限分類切記先引入use Common\Helper\Category;category_add.html是視圖文件,用來演示如何在模板調(diào)用無限分類。go_category.sql是分類表數(shù)據(jù)庫文件,僅用來參考,分類表的核心字段有id:欄目id,title:欄目名,parent_id:父級欄目id,is_show:是否在前臺顯示, sort:前臺排序。 |
|
|