亚洲一级免费看,特黄特色大片免费观看播放器,777毛片,久久久久国产一区二区三区四区,欧美三级一区二区,国产精品一区二区久久久久,人人澡人人草

php語言

PHP學(xué)習(xí):Category類庫無限分類

時(shí)間:2025-03-21 18:46:31 php語言 我要投稿
  • 相關(guān)推薦

PHP學(xué)習(xí):Category類庫無限分類

  學(xué)習(xí)是沒有盡頭的,只有在不斷的練習(xí)中才能提高自己。以下是百分網(wǎng)小編精心為大家整理的關(guān)于PHP語言學(xué)習(xí)的Category類庫 無限分類方面的知識(shí),希望對(duì)大家有所幫助!更多內(nèi)容請(qǐng)關(guān)注應(yīng)屆畢業(yè)生網(wǎng)!

PHP學(xué)習(xí):Category類庫無限分類

 

  以下是使用該類庫的方法

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

  1、通過include包含類庫

  2、通過new實(shí)例化類

  3、調(diào)用getList()方法獲取所有分類列表

  4、返回:所有分類列表,可以通過獲取fullname顯示參考。

  效果如圖:

  以下是類庫完整源碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
 
/**
 * 類功能:php無限分類
 * author:252588119@qq.com
 * 使用方法見:http://liqingbo.cn/blog-434.html
 */
class Category {
 
    private $model;                                                           //分類的數(shù)據(jù)表模型
    private $rawList array();                                              //原始的分類數(shù)據(jù)
    private $formatList array();                                           //格式化后的分類
    private $error "";                                                      //錯(cuò)誤信息
    private $icon array('&nbsp;&nbsp;│''&nbsp;&nbsp;├ ''&nbsp;&nbsp;└ ');  //格式化的字符
    private $fields array();                                               //字段映射,分類id,上級(jí)分類pid,分類名稱name,格式化后分類名稱fullname
 
    /**
     * 構(gòu)造函數(shù),對(duì)象初始化
     * @param array,object  $model      數(shù)組或?qū)ο,基于TP3.0的數(shù)據(jù)表模型名稱,若不采用TP,可傳遞空值。
     * @param array         $field      字段映射,分類cid,上級(jí)分類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';
    }
 
    /**
     * 獲取分類信息數(shù)據(jù)
     * @param array,string  $condition  查詢條件
     * @param string        $orderby    排序
     */
    private function _findAllCat($condition$orderby = NULL) {
        $this->rawList = $this->model->where($condition)->order($orderby)->select();
    }
 
    /**
     * 返回給定上級(jí)分類$pid的所有同一級(jí)子分類
     * @param   int     $pid    傳入要查詢的pid
     * @return  array           返回結(jié)構(gòu)信息
     */
    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);
        //下級(jí)分類的數(shù)組
        //如果沒下級(jí)分類,結(jié)束遞歸
        if (!($n count($childs))){
            return;
        }
        $m = 1;
        //循環(huán)所有的下級(jí)分類
        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;"); //遞歸下一級(jí)分類
            $m++;
        }
    }
 
    /**
     * 不采用數(shù)據(jù)模型時(shí),可以從外部傳遞數(shù)據(jù),得到遞歸格式化分類
     * @param   array,string     $condition    條件
     * @param   int              $cid          起始分類
     * @param   string           $orderby      排序
     * @return  array           返回結(jié)構(gòu)信息
     */
    public function getList($condition = NULL, $cid = 0, $orderby = NULL) {
        unset($this->rawList, $this->formatList);
        $this->_findAllCat($condition$orderby);
        $this->_searchList($cid);
        return $this->formatList;
    }
 
    /**
     * 獲取結(jié)構(gòu)
     * @param   array            $data         二維數(shù)組數(shù)據(jù)
     * @param   int              $cid          起始分類
     * @return  array           遞歸格式化分類數(shù)組
     */
    public function getTree($data$cid = 0) {
        unset($this->rawList, $this->formatList);
        $this->rawList = $data;
        $this->_searchList($cid);
        return $this->formatList;
    }
 
    /**
     * 獲取錯(cuò)誤信息
     * @return  string           錯(cuò)誤信息字符串
     */
    public function getError() {
        return $this->error;
    }
 
    /**
     * 檢查分類參數(shù)$cid,是否為空
     * @param   int              $cid          起始分類
     * @return  boolean           遞歸格式化分類數(shù)組
     */
    private function _checkCatID($cid) {
        if (intval($cid)) {
            return true;
        else {
            $this->error = "參數(shù)分類ID為空或者無效!";
            return false;
        }
    }
 
    /**
     * 檢查分類參數(shù)$cid,是否為空
     * @param   int         $cid        分類cid
     */
    private function _searchPath($cid) {
        //檢查參數(shù)
        if (!$this->_checkCatID($cid))
            return false;
        $rs $this->model->find($cid);                                        //初始化對(duì)象,查找上級(jí)Id;
        $this->formatList[] = $rs;                                            //保存結(jié)果
        $this->_searchPath($rs[$this->fields['pid']]);
    }
 
    /**
     * 查詢給定分類cid的路徑
     * @param   int         $cid        分類cid
     * @return  array                   數(shù)組
     */
    public function getPath($cid) {
        unset($this->rawList, $this->formatList);
        $this->_searchPath($cid);                                               //查詢分類路徑
        return array_reverse($this->formatList);
    }
 
    /**
     * 添加分類
     * @param   array         $data        一維數(shù)組,要添加的數(shù)據(jù),$data需要包含上級(jí)分類ID。
     * @return  boolean                    添加成功,返回相應(yīng)的分類ID,添加失敗,返回FALSE;
     */
    public function add($data) {
        if (empty($data))
            return false;
        return $this->model->data($data)->add();
    }
 
    /**
     * 修改分類
     * @param   array         $data     一維數(shù)組,$data需要包含要修改的分類cid。
     * @return  boolean                 組修改成功,返回相應(yīng)的分類ID,修改失敗,返回FALSE;
     */
    public function edit($data) {
        if (empty($data))
            return false;
        return $this->model->data($data)->save();
    }
 
    /**
     * 刪除分類
     * @param   int         $cid        分類cid
     * @return  boolean                 刪除成功,返回相應(yīng)的分類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                 刪除成功,返回相應(yīng)的分類ID及所有子ID 數(shù)組,返回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里包含一個(gè)文件夾,三個(gè)文件。Helper文件夾包含了無限分類處理類,文件夾放在Application/Common/目錄下,CategoryController.class.php是控制器文件,用來演示如何使用無限分類處理類,控制器使用無限分類切記先引入use Common\Helper\Category;category_add.html是視圖文件,用來演示如何在模板調(diào)用無限分類。go_category.sql是分類表數(shù)據(jù)庫文件,僅用來參考,分類表的核心字段有id:欄目id,title:欄目名,parent_id:父級(jí)欄目id,is_show:是否在前臺(tái)顯示, sort:前臺(tái)排序。

【PHP學(xué)習(xí):Category類庫無限分類】相關(guān)文章:

PHP無限分類(樹形類)的深入分析05-12

php無限分類方法講解04-14

PHP實(shí)現(xiàn)無限級(jí)分類的方法07-14

PHP學(xué)習(xí):QRCode PHP生成二維碼類庫04-17

php+mysql實(shí)現(xiàn)無限分類實(shí)例詳解07-23

php兩種無限分類方法實(shí)例06-15

php實(shí)現(xiàn)無限級(jí)分類實(shí)現(xiàn)代碼07-03

PHP學(xué)習(xí):PHP拼音類01-25

20個(gè)PHP常用類庫匯總03-21