PHP之Category类库 无限分类

PHP之Category类库 无限分类,第1张

概述PHP之Category类库 无限分类

category类库 无限分类

以下是使用该类库的方法

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显示参考。

效果如图:

以下是类库完整源码:

<?PHP /** * 类功能:PHP无限分类 * author:[email protected] * 使用方法见:http://liqingbo.cn/blog-434.HTML */class category {     private $model;                                                           //分类的数据表模型    private $rawList = array();                                              //原始的分类数据    private $formatList = array();                                           //格式化后的分类    private $error = "";                                                      //错误信息    private $icon = array('&nbsp;&nbsp;│', '&nbsp;&nbsp;├ ', '&nbsp;&nbsp;└ ');  //格式化的字符    private $fIElds = array();                                               //字段映射,分类ID,上级分类pID,分类名称name,格式化后分类名称fullname     /**     * 构造函数,对象初始化     * @param array,object  $model      数组或对象,基于TP3.0的数据表模型名称,若不采用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';    }     /**     * 获取分类信息数据     * @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           返回结构信息     */    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);        //下级分类的数组        //如果没下级分类,结束递归        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 . "&nbsp;&nbsp;"); //递归下一级分类            $m++;        }    }     /**     * 不采用数据模型时,可以从外部传递数据,得到递归格式化分类     * @param   array,string     $condition    条件     * @param   int              $cID          起始分类     * @param   string           $orderby      排序     * @return  array           返回结构信息     */    public function getList($condition = NulL, $cID = 0, $orderby = NulL) {        unset($this->rawList, $this->formatList);        $this->_findAllCat($condition, $orderby);        $this->_searchList($cID);        return $this->formatList;    }     /**     * 获取结构     * @param   array            $data         二维数组数据     * @param   int              $cID          起始分类     * @return  array           递归格式化分类数组     */    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;    }     /**     * 检查分类参数$cID,是否为空     * @param   int              $cID          起始分类     * @return  boolean           递归格式化分类数组     */    private function _checkCatID($cID) {        if (intval($cID)) {            return true;        } else {            $this->error = "参数分类ID为空或者无效!";            return false;        }    }     /**     * 检查分类参数$cID,是否为空     * @param   int         $cID        分类cID     */    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']]);    }     /**     * 查询给定分类cID的路径     * @param   int         $cID        分类cID     * @return  array                   数组     */    public function getPath($cID) {        unset($this->rawList, $this->formatList);        $this->_searchPath($cID);                                               //查询分类路径        return array_reverse($this->formatList);    }     /**     * 添加分类     * @param   array         $data        一维数组,要添加的数据,$data需要包含上级分类ID。     * @return  boolean                    添加成功,返回相应的分类ID,添加失败,返回FALSE;     */    public function add($data) {        if (empty($data))            return false;        return $this->model->data($data)->add();    }     /**     * 修改分类     * @param   array         $data     一维数组,$data需要包含要修改的分类cID。     * @return  boolean                 组修改成功,返回相应的分类ID,修改失败,返回FALSE;     */    public function edit($data) {        if (empty($data))            return false;        return $this->model->data($data)->save();    }     /**     * 删除分类     * @param   int         $cID        分类cID     * @return  boolean                 删除成功,返回相应的分类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                 删除成功,返回相应的分类ID及所有子ID 数组,返回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是视图文件,用来演示如何在模板调用无限分类。

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

以上是编程之家为你收集整理的PHP之Category类库 无限分类全部内容,希望文章能够帮你解决PHP之Category类库 无限分类所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/997776.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存