实现PHP+Mysql无限分类的方法

实现PHP+Mysql无限分类的方法,第1张

概述实现PHP+Mysql无限分类方法

无限分类是个老话题了,来看看PHP结合MysqL如何实现。

【相关学习推荐: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字段。

总结

以上是内存溢出为你收集整理的实现PHP+Mysql无限分类的方法全部内容,希望文章能够帮你解决实现PHP+Mysql无限分类的方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1159909.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存