程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 實現PHP+Mysql無限分類的方法匯總,phpmysql

實現PHP+Mysql無限分類的方法匯總,phpmysql

編輯:關於PHP編程

實現PHP+Mysql無限分類的方法匯總,phpmysql


無限分類是個老話題了,來看看PHP結合Mysql如何實現。

第一種方法

這種方法是很常見、很傳統的一種,先看表結構

表:category
id int 主鍵,自增
name varchar 分類名稱
pid int 父類id,默認0
頂級分類的 pid 默認就是0了。當我們想取出某個分類的子分類樹的時候,基本思路就是遞歸,當然,出於效率問題不建議每次遞歸都查詢數據庫,通常的做法是先講所有分類取出來,保存到PHP數組裡,再進行處理,最後還可以將結果緩存起來以提高下次請求的效率。

先來構建一個原始數組,這個直接從數據庫中拉出來就行:

復制代碼 代碼如下:
$categories = array(
    array('id'=>1,'name'=>'電腦','pid'=>0),
    array('id'=>2,'name'=>'手機','pid'=>0),
    array('id'=>3,'name'=>'筆記本','pid'=>1),
    array('id'=>4,'name'=>'台式機','pid'=>1),
    array('id'=>5,'name'=>'智能機','pid'=>2),
    array('id'=>6,'name'=>'功能機','pid'=>2),
    array('id'=>7,'name'=>'超級本','pid'=>3),
    array('id'=>8,'name'=>'游戲本','pid'=>3),
);

目標是將它轉化為下面這種結構

電腦
    筆記本
        超級本
        游戲本
    台式機
手機
    智能機
    功能機
用數組來表示的話,可以增加一個 children 鍵來存儲它的子分類:

復制代碼 代碼如下:
array(
    //1對應id,方便直接讀取
    1 => array(
        'id'=>1,
        'name'=>'電腦',
        'pid'=>0,
        children=>array(
            &array(
                'id'=>3,
                'name'=>'筆記本',
                'pid'=>1,
                'children'=>array(
                    //此處省略
                )
            ),
            &array(
                'id'=>4,
                'name'=>'台式機',
                'pid'=>1,
                'children'=>array(
                    //此處省略
                )
            ),
        )
    ),
    //其他分類省略
)

處理過程:

復制代碼 代碼如下:
$tree = array();
//第一步,將分類id作為數組key,並創建children單元
foreach($categories as $category){
    $tree[$category['id']] = $category;
    $tree[$category['id']]['children'] = array();
}
//第二部,利用引用,將每個分類添加到父類children數組中,這樣一次遍歷即可形成樹形結構。
foreach ($tree as $k=>$item) {
    if ($item['pid'] != 0) {
        $tree[$item['pid']]['children'][] = &$tree[$k];
    }
}
print_r($tree);

打印結果如下:

復制代碼 代碼如下:
Array
(
    [1] => Array
        (
            [id] => 1
            [name] => 電腦
            [pid] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [name] => 筆記本
                            [pid] => 1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 7
                                            [name] => 超級本
                                            [pid] => 3
                                            [children] => Array
                                                (
                                                )
                                        )
                                    [1] => Array
                                        (
                                            [id] => 8
                                            [name] => 游戲本
                                            [pid] => 3
                                            [children] => Array
                                                (
                                                )
                                        )
                                )
                        )
                    [1] => Array
                        (
                            [id] => 4
                            [name] => 台式機
                            [pid] => 1
                            [children] => Array
                                (
                                )
                        )
                )
        )
    [2] => Array
        (
            [id] => 2
            [name] => 手機
            [pid] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [name] => 智能機
                            [pid] => 2
                            [children] => Array
                                (
                                )
                        )
                    [1] => Array
                        (
                            [id] => 6
                            [name] => 功能機
                            [pid] => 2
                            [children] => Array
                                (
                                )
                        )
                )
        )
    [3] => Array
        (
            [id] => 3
            [name] => 筆記本
            [pid] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 7
                            [name] => 超級本
                            [pid] => 3
                            [children] => Array
                                (
                                )
                        )
                    [1] => Array
                        (
                            [id] => 8
                            [name] => 游戲本
                            [pid] => 3
                            [children] => Array
                                (
                                )
                        )
                )
        )
    [4] => Array
        (
            [id] => 4
            [name] => 台式機
            [pid] => 1
            [children] => Array
                (
                )
        )
    [5] => Array
        (
            [id] => 5
            [name] => 智能機
            [pid] => 2
            [children] => Array
                (
                )
        )
    [6] => Array
        (
            [id] => 6
            [name] => 功能機
            [pid] => 2
            [children] => Array
                (
                )
        )
    [7] => Array
        (
            [id] => 7
            [name] => 超級本
            [pid] => 3
            [children] => Array
                (
                )
        )
    [8] => Array
        (
            [id] => 8
            [name] => 游戲本
            [pid] => 3
            [children] => Array
                (
                )
        )
)

優點:關系清楚,修改上下級關系簡單。

缺點:使用PHP處理,如果分類數量龐大,效率也會降低。

第二種方法

這種方法是在表字段中增加一個path字段:

表:category
id int 主鍵,自增
name varchar 分類名稱
pid int 父類id,默認0
path varchar 路徑
示例數據:

id        name        pid        path
1         電腦        0          0
2         手機        0          0
3         筆記本      1          0-1
4         超級本      3          0-1-3
5         游戲本      3          0-1-3
path字段記錄了從根分類到上一級父類的路徑,用id+'-'表示。

這種方式,假設我們要查詢電腦下的所有後代分類,只需要一條sql語句:

select id,name,path from category where path like (select concat(path,'-',id,'%') as path from category where id=1);
結果:

+----+-----------+-------+
| id | name      | path  |
+----+-----------+-------+
| 3  | 筆記本 | 0-1   |
| 4  | 超級本 | 0-1-3 |
| 5  | 游戲本 | 0-1-3 |
+----+-----------+-------+
這種方式也被很多人所采納,我總結了下:

優點:查詢容易,效率高,path字段可以加索引。

缺點:更新節點關系麻煩,需要更新所有後輩的path字段。

以上就是本文的全部內容了,兩種方式,你喜歡哪種?希望大家能夠喜歡。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved