栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP > PHP实例

Category类库 无限分类用法

PHP实例 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力



以下是使用该类库的方法

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

1、通过include包含类库

2、通过new实例化类

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

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


效果如图:

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';     }          private function _findAllCat($condition, $orderby = NuLL) {         $this->rawList = $this->model->where($condition)->order($orderby)->select();     }          public function getChild($pid) {         $childs = array();         foreach ($this->rawList as $Category) {             if ($Category[$this->fields['pid']] == $pid){                 $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);         $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['pid']]);     }          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[$this->fields['cid']];  }  unset($list);  $idArr[] = $cid;  return $idArr; } } ?>


  demo里包含一个文件夹,三个文件。Helper文件夹包含了无限分类处理类,文件夹放在Application/Common/目录下,CategoryController.class.php是控制器文件,用来演示如何使用无限分类处理类,控制器使用无限分类切记先引入use CommonHelperCategory;category_add.html是视图文件,用来演示如何在模板调用无限分类。go_category.sql是分类表数据库文件,仅用来参考,分类表的核心字段有id:栏目id,title:栏目名,parent_id:父级栏目id,is_show:是否在前台显示, sort:前台排序。


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/824930.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号