Home> php教程> php手册> body text

php 无限分类实现原理详解

WBOY
Release: 2016-05-25 16:43:50
Original
1202 people have browsed it

在php中无限分类是我们在实际开发中经常用到的一种数据结构,一般我们称之为树形结构,像我网站的分类有php入门,正则等等分类,这种是二级不是无限级了,如果是无限级就可以在子类下加子类了.

我们先来看我实现无限分类的具体过程.

题设:类似淘宝的商品分类,可以在任意分类设置其子类.

一、创建`type`数据表,代码如下:

`id` 自增长 `fid` int(11) 默认(0) ,父节点id `name` varchar(50),分类名称 CREATE TABLE `type` ( `id` int(11) NOT NULL AUTO_INCREMENT, `fid` int(11) NOT NULL DEFAULT '0', `name` varchar(50) NOT NULL, PRIMARY KEY (`id`) )
Copy after login

二、添加,我们先添加几个顶级分类,代码如下:

INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '0', '手机'); INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '0', '电脑'); INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '0', '鞋子'); INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '0', '衣服');这里fid=0是代表顶级分类
Copy after login

接着我们为{电脑}添加几个个子分类,代码如下:

INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '2', '台式'), (NULL, '2', '笔记本');

这里fid=2,2这个id是分类{电脑}的id,如果是添加{鞋子}的子分类则fid=3,同理我们为{笔记本}添加子分类则fid=6,代码如下:

INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '6', 'ausu'), (NULL, '6', 'hp');

三、删除,如果我们想删除{笔记本}这个分类,很简单:

DELETE FROM `type` WHERE `id`=6{笔记本}的子分类我们也要记得做相应的处理

代码如下:

Copy after login

DELETE FROM `type` WHERE `fid`=6这样我们不就可以直接删除{ausu}、{hp}?但是假设{ausu}有一个子分类{a1},{a1}也有一个子分类{a2},如果不用递归我们就无法彻底删除数据.

四、查找

1.查找{电脑}的子分类

SELECT * FROM `type` WHERE `fid`=22.查找{电脑}的所有子分类

代码如下:

Copy after login

五、实际数据应用

在数据表添加一个字段`tid`,字段值为记录所属分类`type`表的id,必须是id不能是name,因为name的值可能会改变.

例如查询属于{电脑}分类的商品,代码如下:

SELECT * FROM `goods` WHERE `tid`=2

下面再看个实例,直接操作数组,代码如下:

 1, 'name' => 'dev', 'parentid' => 0 ), array( 'id' => 2, 'name' => 'php', 'parentid' => 1 ), array( 'id' => 3, 'name' => 'smarty', 'parentid' => 2 ), array( 'id' => 4, 'name' => 'life', 'parentid' => 0 ), array( 'id' => 5, 'name' => 'pdo', 'parentid' => 2 ), array( 'id' => 6, 'name' => 'pdo-mysql', 'parentid' => 5 ), array( 'id' => 7, 'name' => 'java', 'parentid' => 1 ) ); // 72648 // 84072 function findChild(&$arr,$id){ $childs=array(); foreach ($arr as $k => $v){ if($v['parentid']== $id){ $childs[]=$v; } } return $childs; } function build_tree($root_id){ global $rows; $childs=findChild($rows,$root_id); if(emptyempty($childs)){ return null; } foreach ($childs as $k => $v){ $rescurTree=build_tree($v[id]); if( null != $rescurTree){ $childs[$k]['childs']=$rescurTree; } } return $childs; } $tree=build_tree(0); echo memory_get_usage(); print_r($tree); ?>
Copy after login

我自己用的可以做那种下拉效果并带有级数的效果,代码如下:

".$n."|—".$fenlei[$i]['name']."n"; } $this->dafenglei_select($m+1,$fenlei[$i]['id'], $fenlei ); } } return $menu; } dafenglei_select(0,0, $fenlei ); ?>
Copy after login

$fenlei 无限分类数组 $id是选择从哪个分类开始写0代表顶级开始分,只要把数组放进去就可以分了.

永久链接:

转载随意!带上文章地址吧。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!