PHP之Category类库 无限分类

coldplay.xixi
Freigeben: 2023-04-08 18:44:02
nach vorne
3740 人浏览过

PHP之Category类库 无限分类

Category类库 无限分类

以下是使用该类库的方法

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

1、通过include包含类库

2、通过new实例化类

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

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

效果如图:

041b415a74501625b963129815e6b37.png

以下是类库完整源码:

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 . "  "); //递归下一级分类
            $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;
    }
 
}
?>
Nach dem Login kopieren

   

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中文网其他相关文章!

Verwandte Etiketten:
Quelle:csdn.net
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!