实现PHP+Mysql无限分类的方法汇总_.docx

上传人:scccc 文档编号:11347637 上传时间:2021-07-28 格式:DOCX 页数:13 大小:14.23KB
返回 下载 相关 举报
实现PHP+Mysql无限分类的方法汇总_.docx_第1页
第1页 / 共13页
实现PHP+Mysql无限分类的方法汇总_.docx_第2页
第2页 / 共13页
实现PHP+Mysql无限分类的方法汇总_.docx_第3页
第3页 / 共13页
实现PHP+Mysql无限分类的方法汇总_.docx_第4页
第4页 / 共13页
实现PHP+Mysql无限分类的方法汇总_.docx_第5页
第5页 / 共13页
亲,该文档总共13页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《实现PHP+Mysql无限分类的方法汇总_.docx》由会员分享,可在线阅读,更多相关《实现PHP+Mysql无限分类的方法汇总_.docx(13页珍藏版)》请在三一文库上搜索。

1、实现PHP+Mysql无限分类的方法汇总_ 这篇文章主要给大家汇总介绍了实现PHP+Mysql无限分类的2种方法,并对比分析了2种方法的优劣,需要的伴侣可以参考下 无限分类是个老话题了,来看看PHP结合Mysql如何实现。 第一种方法 这种方法是很常见、很传统的一种,先看表结构 表:category id int 主键,自增 name varchar 分类名称 pid int 父类id,默认0 顶级分类的 pid 默认就是0了。当我们想取出某个分类的子分类树的时候,基本思路就是递归,当然,出于效率问题不建议每次递归都查询数据库,通常的做法是先讲全部分类取出来,保存到PHP数组里,再进行处理,最

2、终还可以将结果缓存起来以提高下次恳求的效率。 先来构建一个原始数组,这个挺直从数据库中拉出来就行: 代码如下: $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), )

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( /此处省略 ) ), ) ), /其他分类省略 ) 处理过程: 代码如下:

4、$tree = array(); /第一步,将分类id作为数组key,并创建children单元 foreach($categories as $category) $tree$categoryid = $category; $tree$categoryidchildren = array(); /其次部,利用引用,将每个分类添加到父类children数组中,这样一次遍历即可形成树形结构。 foreach ($tree as $k=$item) if ($itempid != 0) $tree$itempidchildren = $tree$k; print_r($tree); 打印结果如下:

5、 代码如下: 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

6、 = 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 chil

7、dren = 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

8、 = 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 超级本

9、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字段。 以上就是本文的全部内容了,两种方式,你喜爱哪种?盼望大家能够喜爱。 更多信息请查看IT技术专栏 .

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1