PHP之Category类库 无限分类

Category类库 无限分类

以下是使用该类库的方法

  1. include("Common/Category.class.php");
  2. $Category = new Category("ArticleCategory",array('id','pid','name','fullname'));
  3. $categoryList = $Category->getList();

1、通过include包含类库

2、通过new实例化类

3、调用getList()方法获取所有分类列表

4、返回:所有分类列表,可以通过获取fullname显示参考。

效果如图:

PHP之Category类库 无限分类

以下是类库完整源码:

  1. <?php
  2. /**
  3. * 类功能:php无限分类
  4. * author:252588119@qq.com
  5. * 使用方法见:http://liqingbo.cn/blog-434.html
  6. */
  7. class Category {
  8. private $model; //分类的数据表模型
  9. private $rawList = array(); //原始的分类数据
  10. private $formatList = array(); //格式化后的分类
  11. private $error = ""; //错误信息
  12. private $icon = array('&nbsp;&nbsp;│', '&nbsp;&nbsp;├ ', '&nbsp;&nbsp;└ '); //格式化的字符
  13. private $fields = array(); //字段映射,分类id,上级分类pid,分类名称name,格式化后分类名称fullname
  14. /**
  15. * 构造函数,对象初始化
  16. * @param array,object $model 数组或对象,基于TP3.0的数据表模型名称,若不采用TP,可传递空值。
  17. * @param array $field 字段映射,分类cid,上级分类pid,分类名称,格式化后分类名称fullname
  18. */
  19. public function __construct($model = '', $fields = array()) {
  20. if (is_string($model) && (!emptyempty($model))) {
  21. if (!$this->model = D($model))
  22. $this->error = $model . "模型不存在!";
  23. }
  24. if (is_object($model))
  25. $this->model = &$model;
  26. $this->fields['cid'] = $fields['0'] ? $fields['0'] : 'id';
  27. $this->fields['pid'] = $fields['1'] ? $fields['1'] : 'pid';
  28. $this->fields['name'] = $fields['2'] ? $fields['2'] : 'name';
  29. $this->fields['fullname'] = $fields['3'] ? $fields['3'] : 'fullname';
  30. }
  31. /**
  32. * 获取分类信息数据
  33. * @param array,string $condition 查询条件
  34. * @param string $orderby 排序
  35. */
  36. private function _findAllCat($condition, $orderby = NuLL) {
  37. $this->rawList = $this->model->where($condition)->order($orderby)->select();
  38. }
  39. /**
  40. * 返回给定上级分类$pid的所有同一级子分类
  41. * @param int $pid 传入要查询的pid
  42. * @return array 返回结构信息
  43. */
  44. public function getChild($pid) {
  45. $childs = array();
  46. foreach ($this->rawList as $Category) {
  47. if ($Category[$this->fields['pid']] == $pid){
  48. $childs[] = $Category;
  49. }
  50. }
  51. return $childs;
  52. }
  53. /**
  54. * 递归格式化分类前的字符
  55. * @param int $cid 分类cid
  56. * @param string $space
  57. */
  58. private function _searchList($cid = 0, $space = "") {
  59. $childs = $this->getChild($cid);
  60. //下级分类的数组
  61. //如果没下级分类,结束递归
  62. if (!($n = count($childs))){
  63. return;
  64. }
  65. $m = 1;
  66. //循环所有的下级分类
  67. for ($i = 0; $i < $n; $i++) {
  68. $pre = "";
  69. $pad = "";
  70. if ($n == $m) {
  71. $pre = $this->icon[2];
  72. } else {
  73. $pre = $this->icon[1];
  74. $pad = $space ? $this->icon[0] : "";
  75. }
  76. $childs[$i][$this->fields['fullname']] = ($space ? $space . $pre : "") . $childs[$i][$this->fields['name']];
  77. $this->formatList[] = $childs[$i];
  78. $this->_searchList($childs[$i][$this->fields['cid']], $space . $pad . "&nbsp;&nbsp;"); //递归下一级分类
  79. $m++;
  80. }
  81. }
  82. /**
  83. * 不采用数据模型时,可以从外部传递数据,得到递归格式化分类
  84. * @param array,string $condition 条件
  85. * @param int $cid 起始分类
  86. * @param string $orderby 排序
  87. * @return array 返回结构信息
  88. */
  89. public function getList($condition = NuLL, $cid = 0, $orderby = NuLL) {
  90. unset($this->rawList, $this->formatList);
  91. $this->_findAllCat($condition, $orderby);
  92. $this->_searchList($cid);
  93. return $this->formatList;
  94. }
  95. /**
  96. * 获取结构
  97. * @param array $data 二维数组数据
  98. * @param int $cid 起始分类
  99. * @return array 递归格式化分类数组
  100. */
  101. public function getTree($data, $cid = 0) {
  102. unset($this->rawList, $this->formatList);
  103. $this->rawList = $data;
  104. $this->_searchList($cid);
  105. return $this->formatList;
  106. }
  107. /**
  108. * 获取错误信息
  109. * @return string 错误信息字符串
  110. */
  111. public function getError() {
  112. return $this->error;
  113. }
  114. /**
  115. * 检查分类参数$cid,是否为空
  116. * @param int $cid 起始分类
  117. * @return boolean 递归格式化分类数组
  118. */
  119. private function _checkCatID($cid) {
  120. if (intval($cid)) {
  121. return true;
  122. } else {
  123. $this->error = "参数分类ID为空或者无效!";
  124. return false;
  125. }
  126. }
  127. /**
  128. * 检查分类参数$cid,是否为空
  129. * @param int $cid 分类cid
  130. */
  131. private function _searchPath($cid) {
  132. //检查参数
  133. if (!$this->_checkCatID($cid))
  134. return false;
  135. $rs = $this->model->find($cid); //初始化对象,查找上级Id;
  136. $this->formatList[] = $rs; //保存结果
  137. $this->_searchPath($rs[$this->fields['pid']]);
  138. }
  139. /**
  140. * 查询给定分类cid的路径
  141. * @param int $cid 分类cid
  142. * @return array 数组
  143. */
  144. public function getPath($cid) {
  145. unset($this->rawList, $this->formatList);
  146. $this->_searchPath($cid); //查询分类路径
  147. return array_reverse($this->formatList);
  148. }
  149. /**
  150. * 添加分类
  151. * @param array $data 一维数组,要添加的数据,$data需要包含上级分类ID。
  152. * @return boolean 添加成功,返回相应的分类ID,添加失败,返回FALSE;
  153. */
  154. public function add($data) {
  155. if (emptyempty($data))
  156. return false;
  157. return $this->model->data($data)->add();
  158. }
  159. /**
  160. * 修改分类
  161. * @param array $data 一维数组,$data需要包含要修改的分类cid。
  162. * @return boolean 组修改成功,返回相应的分类ID,修改失败,返回FALSE;
  163. */
  164. public function edit($data) {
  165. if (emptyempty($data))
  166. return false;
  167. return $this->model->data($data)->save();
  168. }
  169. /**
  170. * 删除分类
  171. * @param int $cid 分类cid
  172. * @return boolean 删除成功,返回相应的分类ID,删除失败,返回FALSE
  173. */
  174. public function del($cid) {
  175. $cid = intval($cid);
  176. if (emptyempty($cid))
  177. return false;
  178. $conditon[$this->fields['cid']] = $cid;
  179. return $this->model->where($conditon)->delete();
  180. }
  181. /**
  182. * 删除分类
  183. * @param int $cid 分类cid
  184. * @return boolean 删除成功,返回相应的分类ID及所有子ID 数组,返回FALSE
  185. */
  186. public function getIdArr($cid){
  187. $cid = !emptyempty($cid) ? intval($cid) : 0;
  188. if (emptyempty($cid)) return false;
  189. $list = $this->getList($condition = NuLL,$cid, $orderby = NuLL);
  190. foreach($list as $val){
  191. $idArr[] = $val[$this->fields['cid']];
  192. }
  193. unset($list);
  194. $idArr[] = $cid;
  195. return $idArr;
  196. }
  197. }
  198. ?>

demo里包含一个文件夹,三个文件。Helper文件夹包含了无限分类处理类,文件夹放在Application/Common/目录下,CategoryController.class.php是控制器文件,用来演示如何使用无限分类处理类,控制器使用无限分类切记先引入use Common\Helper\Category;category_add.html是视图文件,用来演示如何在模板调用无限分类。

go_category.sql是分类表数据库文件,仅用来参考,分类表的核心字段有id:栏目id,title:栏目名,parent_id:父级栏目id,is_show:是否在前台显示, sort:前台排序。