php 无限分类实现原理详解
我们先来看我实现无限分类的具体过程。
题设:类似淘宝的商品分类,可以在任意分类设置其子类。
一、创建`type`数据表
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5836')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5836>`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`)
)
二、添加
我们先添加几个顶级分类
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5876')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5876>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是代表顶级分类
接着我们为{电脑}添加几个个子分类
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5695')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5695>INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '2', '台式'), (NULL, '2', '笔记本');这里fid=2,
2这个id是分类{电脑}的id,如果是添加{鞋子}的子分类则fid=3
同理我们为{笔记本}添加子分类则fid=6
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9442')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9442>INSERT INTO `type` (`id`, `fid`, `name`) VALUES (NULL, '6', 'ausu'), (NULL, '6', 'hp');
三、删除
如果我们想删除{笔记本}这个分类,很简单
DELETE FROM `type` WHERE `id`=6{笔记本}的子分类我们也要记得做相应的处理
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2454')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2454>
function del($fid) {
$sql="SELECT * FROM `type` WHERE `fid`=$fid";
$rs=mysql_query($sql);
for ($i = 0; $i < count($rs); $i ) {
$sql="DELETE FROM `type` WHERE `id`={$rs[$i]['id']}";
mysql_query($sql);
del($rs['id']);//递归
}
}
del(6);//执行操作这里你也许你会疑惑为什么那么麻烦用递归,而不是直接这样删除
DELETE FROM `type` WHERE `fid`=6这样我们不就可以直接删除{ausu}、{hp}?但是假设{ausu}有一个子分类{a1},{a1}也有一个子分类{a2},如果不用递归我们就无法彻底删除数据。
三、查找
1.查找{电脑}的子分类
SELECT * FROM `type` WHERE `fid`=22.查找{电脑}的所有子分类
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1671')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1671>
function sel($fid) {
$sql="SELECT * FROM `type` WHERE `fid`=$fid";
$rs=mysql_query($sql);
for ($i = 0; $i < count($rs); $i ) {
echo $rs[$i]['name'];
sel($rs[$i]['id']);//递归
}
}
sel(2);
四、实际数据应用
在数据表添加一个字段`tid`,字段值为记录所属分类`type`表的id。必须是id不能是name,因为name的值可能会改变。
例如查询属于{电脑}分类的商品
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4647')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4647>SELECT * FROM `goods` WHERE `tid`=2
下面再看个实例,直接操作数组
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3095')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3095>
<?php
$rows = array(
array(
'id' => 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(empty($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);
?>
我自己用的可以做那种下拉效果并带有级数的效果
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6270')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6270>
function dafenglei_select($m,$id, $fenlei ){
global $menu;
$n = str_pad('',$m,'-',STR_PAD_RIGHT);
$n = str_replace("-"," ",$n);
for($i=0;$i<count($fenlei);$i ){
if($fenlei[$i]['classid']==$id){
$menu .= "<option value="".$fenlei[$i]['id']."">".$n."|—".$fenlei[$i]['name']."</option>n";
}
$this->dafenglei_select($m 1,$fenlei[$i]['id'], $fenlei );
}
}
return $menu;
}
dafenglei_select(0,0, $fenlei );
$fenlei 无限分类数组 $id是选择从哪个分类开始 写0代表顶级开始分,只要把数组放进去就可以分了
您可能感兴趣的文章:
php无限极分类原理
php文件上传代码大全(实例分享)
浅谈PHP无限极分类原理
PHP 无限分类程序代码
php用递归方法实现无限级分类的代码
python web为什么不火
php无限级分类的递归函数
php递归实现无限分类生成下拉列表函数代码
django 怎么写权限
django如何添加权限