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

以下是使用該類(lèi)庫的方法
|
1
2
3
|
include("Common/Category.class.php");$Category = new Category("ArticleCategory",array('id','pid','name','fullname'));$categoryList = $Category->getList(); |
1、通過(guò)include包含類(lèi)庫
2、通過(guò)new實(shí)例化類(lèi)
3、調用getList()方法獲取所有分類(lèi)列表
4、返回:所有分類(lèi)列表,可以通過(guò)獲取fullname顯示參考。
效果如圖:
以下是類(lèi)庫完整源碼:
|
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 /** * 類(lèi)功能:php無(wú)限分類(lèi) * author:252588119@qq.com * 使用方法見(jiàn):http://liqingbo.cn/blog-434.html */class Category { private $model; //分類(lèi)的數據表模型 private $rawList = array(); //原始的分類(lèi)數據 private $formatList = array(); //格式化后的分類(lèi) private $error = ""; //錯誤信息 private $icon = array(' │', ' ├ ', ' └ '); //格式化的字符 private $fields = array(); //字段映射,分類(lèi)id,上級分類(lèi)pid,分類(lèi)名稱(chēng)name,格式化后分類(lèi)名稱(chēng)fullname /** * 構造函數,對象初始化 * @param array,object $model 數組或對象,基于TP3.0的數據表模型名稱(chēng),若不采用TP,可傳遞空值。 * @param array $field 字段映射,分類(lèi)cid,上級分類(lèi)pid,分類(lèi)名稱(chēng),格式化后分類(lèi)名稱(chēng)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'; } /** * 獲取分類(lèi)信息數據 * @param array,string $condition 查詢(xún)條件 * @param string $orderby 排序 */ private function _findAllCat($condition, $orderby = NULL) { $this->rawList = $this->model->where($condition)->order($orderby)->select(); } /** * 返回給定上級分類(lèi)$pid的所有同一級子分類(lèi) * @param int $pid 傳入要查詢(xún)的pid * @return array 返回結構信息 */ public function getChild($pid) { $childs = array(); foreach ($this->rawList as $Category) { if ($Category[$this->fields['pid']] == $pid){ $childs[] = $Category; } } return $childs; } /** * 遞歸格式化分類(lèi)前的字符 * @param int $cid 分類(lèi)cid * @param string $space */ private function _searchList($cid = 0, $space = "") { $childs = $this->getChild($cid); //下級分類(lèi)的數組 //如果沒(méi)下級分類(lèi),結束遞歸 if (!($n = count($childs))){ return; } $m = 1; //循環(huán)所有的下級分類(lèi) 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 . " "); //遞歸下一級分類(lèi) $m++; } } /** * 不采用數據模型時(shí),可以從外部傳遞數據,得到遞歸格式化分類(lèi) * @param array,string $condition 條件 * @param int $cid 起始分類(lèi) * @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 起始分類(lèi) * @return array 遞歸格式化分類(lèi)數組 */ 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; } /** * 檢查分類(lèi)參數$cid,是否為空 * @param int $cid 起始分類(lèi) * @return boolean 遞歸格式化分類(lèi)數組 */ private function _checkCatID($cid) { if (intval($cid)) { return true; } else { $this->error = "參數分類(lèi)ID為空或者無(wú)效!"; return false; } } /** * 檢查分類(lèi)參數$cid,是否為空 * @param int $cid 分類(lèi)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']]); } /** * 查詢(xún)給定分類(lèi)cid的路徑 * @param int $cid 分類(lèi)cid * @return array 數組 */ public function getPath($cid) { unset($this->rawList, $this->formatList); $this->_searchPath($cid); //查詢(xún)分類(lèi)路徑 return array_reverse($this->formatList); } /** * 添加分類(lèi) * @param array $data 一維數組,要添加的數據,$data需要包含上級分類(lèi)ID。 * @return boolean 添加成功,返回相應的分類(lèi)ID,添加失敗,返回FALSE; */ public function add($data) { if (empty($data)) return false; return $this->model->data($data)->add(); } /** * 修改分類(lèi) * @param array $data 一維數組,$data需要包含要修改的分類(lèi)cid。 * @return boolean 組修改成功,返回相應的分類(lèi)ID,修改失敗,返回FALSE; */ public function edit($data) { if (empty($data)) return false; return $this->model->data($data)->save(); } /** * 刪除分類(lèi) * @param int $cid 分類(lèi)cid * @return boolean 刪除成功,返回相應的分類(lèi)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(); } /** * 刪除分類(lèi) * @param int $cid 分類(lèi)cid * @return boolean 刪除成功,返回相應的分類(lèi)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里包含一個(gè)文件夾,三個(gè)文件。Helper文件夾包含了無(wú)限分類(lèi)處理類(lèi),文件夾放在A(yíng)pplication/Common/目錄下,CategoryController.class.php是控制器文件,用來(lái)演示如何使用無(wú)限分類(lèi)處理類(lèi),控制器使用無(wú)限分類(lèi)切記先引入use Common\Helper\Category;category_add.html是視圖文件,用來(lái)演示如何在模板調用無(wú)限分類(lèi)。go_category.sql是分類(lèi)表數據庫文件,僅用來(lái)參考,分類(lèi)表的核心字段有id:欄目id,title:欄目名,parent_id:父級欄目id,is_show:是否在前臺顯示, sort:前臺排序。
【PHP學(xué)習:Category類(lèi)庫無(wú)限分類(lèi)】相關(guān)文章:
PHP無(wú)限分類(lèi)(樹(shù)形類(lèi))的深入分析05-12
php無(wú)限分類(lèi)方法講解04-14
PHP實(shí)現無(wú)限級分類(lèi)的方法07-14
PHP學(xué)習:QRCode PHP生成二維碼類(lèi)庫04-17
php+mysql實(shí)現無(wú)限分類(lèi)實(shí)例詳解07-23
php兩種無(wú)限分類(lèi)方法實(shí)例06-15