基于THINKPHP3.0无限级分类

jerry 2015年11月18日 收藏
无限极分类在做项目中经常会用到,无限极分类的实现方法也多种实现形式,下面我分享一个基于thinkphp3.0的无极限分类,同时也支持普通传值的无极限分类。说明该类很久以前我在互联网上找到是,原编写者信息没有,这里我只是在原有的基础上做了些修改和格式化。
分类表需包含3个基本字段:cid,fid,name 即:分类cid,父级fid,分类名称。
表结构:
  1. CREATE TABLE `think_category` (
  2.   `cid` int(11) NOT NULL AUTO_INCREMENT,
  3.   `fid` int(11) DEFAULT NULL,
  4.   `name` varchar(30) DEFAULT NULL
  5.   PRIMARY KEY (`cid`)
  6. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
有两种使用方法:
第一种是基于THINKPHP3.0的使用:下载后将Category.class.php放到当前项目的ORG目录下(其他的目录也可只要能正常引用)
  1. import("@.ORG.Category");
  2. $cat = new Category('Category', array('cid', 'fid', 'name', 'fullname'));
  3. $s = $cat->getList();               //获取分类结构
  4. $s = $cat->getList('', 1);          //获取fid=1的子分类结构
  5. $s = $cat->getList($condition, 1);  //$condition为查询条件,获取fid=1的子分类结构
  6. $s = $cat->getPath(3);              //获取分类id=3的路径
  7. $data = array("fid" => 0, "name" => "新分类名称");
  8. $s = $cat->add($data);              //添加分类,$data需要包含上级分类fid
  9. $data = array("cid" => 2, "name" => "修改后分类名称");
  10. $s = $cat->edit($data);             //修改分类,$data需要包含分类ID
  11. $s = $cat->del(10);                 //删除分类id=10的分类
第二种使用方法不需要使用TP的支持,但是数据结构需一样。

分类类文件Category.class.php内容:
  1. <?php

  2. /**
  3.   +------------------------------------------------------------------------------
  4.  * 分类管理
  5.   +------------------------------------------------------------------------------
  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,上级分类fid,分类名称name,格式化后分类名称fullname

  14.     /**
  15.       +----------------------------------------------------------
  16.      * 构造函数,对象初始化
  17.       +----------------------------------------------------------
  18.      * @param array,object  $model      数组或对象,基于TP3.0的数据表模型名称,若不采用TP,可传递空值。
  19.      * @param array         $field      字段映射,分类cid,上级分类fid,分类名称,格式化后分类名称fullname
  20.       +----------------------------------------------------------
  21.      */

  22.     public function __construct($model = '', $fields = array()) {
  23.         if (is_string($model) && (!empty($model))) {
  24.             if (!$this->model = D($model))
  25.                 $this->error = $model . "模型不存在!";
  26.         }
  27.         if (is_object($model))
  28.             $this->model = &$model;

  29.         $this->fields['cid'] = $fields['0'] ? $fields['0'] : 'cid';
  30.         $this->fields['fid'] = $fields['1'] ? $fields['1'] : 'fid';
  31.         $this->fields['name'] = $fields['2'] ? $fields['2'] : 'name';
  32.         $this->fields['fullname'] = $fields['3'] ? $fields['3'] : 'fullname';
  33.     }

  34.     /**
  35.       +----------------------------------------------------------
  36.      * 获取分类信息数据
  37.       +----------------------------------------------------------
  38.      * @param array,string  $condition  查询条件
  39.      * @param string        $orderby    排序
  40.       +----------------------------------------------------------
  41.      */
  42.     private function _findAllCat($condition, $orderby = NULL) {
  43.         $this->rawList = empty($orderby) ? $this->model->where($condition)->select() : $this->model->where($condition)->order($orderby)->select();
  44.     }

  45.     /**
  46.       +----------------------------------------------------------
  47.      * 返回给定上级分类$fid的所有同一级子分类
  48.       +----------------------------------------------------------
  49.      * @param   int     $fid    传入要查询的fid
  50.       +----------------------------------------------------------
  51.      * @return  array           返回结构信息
  52.       +----------------------------------------------------------
  53.      */
  54.     public function getChild($fid) {
  55.         $childs = array();
  56.         foreach ($this->rawList as $Category) {
  57.             if ($Category[$this->fields['fid']] == $fid)
  58.                 $childs[] = $Category;
  59.         }
  60.         return $childs;
  61.     }

  62.     /**
  63.       +----------------------------------------------------------
  64.      * 递归格式化分类前的字符
  65.       +----------------------------------------------------------
  66.      * @param   int     $cid    分类cid
  67.      * @param   string  $space
  68.       +----------------------------------------------------------
  69.      */
  70.     private function _searchList($cid = 0, $space = "") {
  71.         $childs = $this->getChild($cid);
  72.         //下级分类的数组
  73.         //如果没下级分类,结束递归
  74.         if (!($n = count($childs)))
  75.             return;
  76.         $m = 1;
  77.         //循环所有的下级分类
  78.         for ($i = 0; $i < $n; $i++) {
  79.             $pre = "";
  80.             $pad = "";
  81.             if ($n == $m) {
  82.                 $pre = $this->icon[2];
  83.             } else {
  84.                 $pre = $this->icon[1];
  85.                 $pad = $space ? $this->icon[0] : "";
  86.             }
  87.             $childs[$i][$this->fields['fullname']] = ($space ? $space . $pre : "") . $childs[$i][$this->fields['name']];
  88.             $this->formatList[] = $childs[$i];
  89.             $this->_searchList($childs[$i][$this->fields['cid']], $space . $pad . "  "); //递归下一级分类
  90.             $m++;
  91.         }
  92.     }

  93.     /**
  94.       +----------------------------------------------------------
  95.      * 不采用数据模型时,可以从外部传递数据,得到递归格式化分类
  96.       +----------------------------------------------------------
  97.      * @param   array,string     $condition    条件
  98.      * @param   int              $cid          起始分类
  99.      * @param   string           $orderby      排序
  100.       +----------------------------------------------------------
  101.      * @return  array           返回结构信息
  102.       +----------------------------------------------------------
  103.      */
  104.     public function getList($condition = NULL, $cid = 0, $orderby = NULL) {
  105.         unset($this->rawList, $this->formatList);
  106.         $this->_findAllCat($condition, $orderby, $orderby);
  107.         $this->_searchList($cid);
  108.         return $this->formatList;
  109.     }

  110.     /**
  111.       +----------------------------------------------------------
  112.      * 获取结构
  113.       +----------------------------------------------------------
  114.      * @param   array            $data         二维数组数据
  115.      * @param   int              $cid          起始分类
  116.       +----------------------------------------------------------
  117.      * @return  array           递归格式化分类数组
  118.       +----------------------------------------------------------
  119.      */
  120.     public function getTree($data, $cid = 0) {
  121.         unset($this->rawList, $this->formatList);
  122.         $this->rawList = $data;
  123.         $this->_searchList($cid);
  124.         return $this->formatList;
  125.     }

  126.     /**
  127.       +----------------------------------------------------------
  128.      * 获取错误信息
  129.       +----------------------------------------------------------
  130.      * @return  string           错误信息字符串
  131.       +----------------------------------------------------------
  132.      */
  133.     public function getError() {
  134.         return $this->error;
  135.     }

  136.     /**
  137.       +----------------------------------------------------------
  138.      * 检查分类参数$cid,是否为空
  139.       +----------------------------------------------------------
  140.      * @param   int              $cid          起始分类
  141.       +----------------------------------------------------------
  142.      * @return  boolean           递归格式化分类数组
  143.       +----------------------------------------------------------
  144.      */
  145.     private function _checkCatID($cid) {
  146.         if (intval($cid)) {
  147.             return true;
  148.         } else {
  149.             $this->error = "参数分类ID为空或者无效!";
  150.             return false;
  151.         }
  152.     }

  153.     /**
  154.       +----------------------------------------------------------
  155.      * 检查分类参数$cid,是否为空
  156.       +----------------------------------------------------------
  157.      * @param   int         $cid        分类cid
  158.       +----------------------------------------------------------
  159.      */
  160.     private function _searchPath($cid) {
  161.         //检查参数
  162.         if (!$this->_checkCatID($cid))
  163.             return false;
  164.         $rs = $this->model->find($cid);                                        //初始化对象,查找上级Id;
  165.         $this->formatList[] = $rs;                                            //保存结果
  166.         $this->_searchPath($rs[$this->fields['fid']]);
  167.     }

  168.     /**
  169.       +----------------------------------------------------------
  170.      * 查询给定分类cid的路径
  171.       +----------------------------------------------------------
  172.      * @param   int         $cid        分类cid
  173.       +----------------------------------------------------------
  174.      * @return  array                   数组
  175.       +----------------------------------------------------------
  176.      */
  177.     public function getPath($cid) {
  178.         unset($this->rawList, $this->formatList);
  179.         $this->_searchPath($cid);                                               //查询分类路径
  180.         return array_reverse($this->formatList);
  181.     }

  182.     /**
  183.       +----------------------------------------------------------
  184.      * 添加分类
  185.       +----------------------------------------------------------
  186.      * @param   array         $data        一维数组,要添加的数据,$data需要包含上级分类ID。
  187.       +----------------------------------------------------------
  188.      * @return  boolean                    添加成功,返回相应的分类ID,添加失败,返回FALSE;
  189.       +----------------------------------------------------------
  190.      */
  191.     public function add($data) {
  192.         if (empty($data))
  193.             return false;
  194.         return $this->model->data($data)->add();
  195.     }

  196.     /**
  197.       +----------------------------------------------------------
  198.      * 修改分类
  199.       +----------------------------------------------------------
  200.      * @param   array         $data     一维数组,$data需要包含要修改的分类cid。
  201.       +----------------------------------------------------------
  202.      * @return  boolean                 组修改成功,返回相应的分类ID,修改失败,返回FALSE;
  203.       +----------------------------------------------------------
  204.      */
  205.     public function edit($data) {
  206.         if (empty($data))
  207.             return false;
  208.         return $this->model->data($data)->save();
  209.     }

  210.     /**
  211.       +----------------------------------------------------------
  212.      * 删除分类
  213.       +----------------------------------------------------------
  214.      * @param   int         $cid        分类cid
  215.       +----------------------------------------------------------
  216.      * @return  boolean                 删除成功,返回相应的分类ID,删除失败,返回FALSE
  217.       +----------------------------------------------------------
  218.      */
  219.     public function del($cid) {
  220.         $cid = intval($cid);
  221.         if (empty($cid))
  222.             return false;
  223.         $conditon[$this->fields['cid']] = $cid;
  224.         return $this->model->where($conditon)->delete();
  225.     }

  226. }
  227. ?>
这个编辑器太不给力了,编辑框又这么小,详细的使用方法和测试包去我的blog下载吧,http://blog.51edm.org/post/78