实现PHP+Mysql无限分类的方法汇总_PHP教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:Java和PHP在Web开发方面对比分析
本文主要从8个方面对php和java在做web开发方面的优劣性做了分析对比,非常不错的一篇文章,这里推荐给小伙伴们。 比较PHP和JSP这两个Web开发技术,在目前的情况是其实是比较PHP和Java的Web开发。以下是我就几个主要方面进行的比较: 一、 语言比较 PHP是解释执行的服务

 这篇文章主要给大家汇总介绍了实现PHP+Mysql无限分类的2种方法,并对比分析了2种方法的优劣,需要的朋友可以参考下

   

无限分类是个老话题了,来看看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 路径
示例数据:

分享:php中return的用法实例分析
这篇文章主要介绍了php中return的用法,实例分析了php中return的功能及常见的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下 本文实例讲述了php中return的用法。分享给大家供大家参考。具体分析如下: 首先,它的意思就是返回;return()是语言结构而不是函数,仅在

共2页上一页12下一页
来源:模板无忧//所属分类:PHP教程/更新时间:2015-03-03
相关PHP教程