<pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

          <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

          <p id="bbfd9"></p>

          <p id="bbfd9"><cite id="bbfd9"></cite></p>

            <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

            <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
            <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>

            PHP學習:Category類庫無限分類

            時間:2025-10-25 10:24:05 php語言

            PHP學習:Category類庫無限分類

              學習是沒有盡頭的,只有在不斷的練習中才能提高自己。以下是百分網小編精心為大家整理的關于PHP語言學習的Category類庫 無限分類方面的知識,希望對大家有所幫助!更多內容請關注應屆畢業生網!

            PHP學習: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實例化類

              3、調用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
             * 使用方法見:/pic/blog-434.html
             */
            class Category {
             
                private $model;                                                           /pic/code>
                private $rawList array();                                              /pic/code>
                private $formatList array();                                           /pic/code>
                private $error "";                                                      /pic/code>
                private $icon array('&nbsp;&nbsp;│''&nbsp;&nbsp;├ ''&nbsp;&nbsp;└ ');  /pic/code>
                private $fields array();                                               /pic/code>
             
                /**
                 * 構造函數,對象初始化
                 * @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);
                    /pic/code>
                    /pic/code>
                    if (!($n count($childs))){
                        return;
                    }
                    $m = 1;
                    /pic/code>
                    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;"); /pic/code>
                        $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) {
                    /pic/code>
                    if (!$this->_checkCatID($cid))
                        return false;
                    $rs $this->model->find($cid);                                        /pic/code>
                    $this->formatList[] = $rs;                                            /pic/code>
                    $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);                                               /pic/code>
                    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無限分類(樹形類)的深入分析11-04

            php無限分類方法講解10-01

            PHP實現無限級分類的方法10-12

            PHP學習:QRCode PHP生成二維碼類庫11-09

            PHP學習:PHP拼音類01-25

            php+mysql實現無限分類實例詳解09-11

            php兩種無限分類方法實例12-08

            php實現無限級分類實現代碼10-05

            PHP數據庫操作類-ezSQL02-19

                    <pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

                    <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

                    <p id="bbfd9"></p>

                    <p id="bbfd9"><cite id="bbfd9"></cite></p>

                      <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

                      <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
                      <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>
                      飘沙影院