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

php 无限分类实现原理详解

WBOY
Release: 2016-05-25 16:43:50
Original
1288 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{笔记本}的子分类我们也要记得做相应的处理

代码如下:

<?php
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][&#39;id&#39;]}"; 
        mysql_query($sql); 
         
        del($rs[&#39;id&#39;]);//递归 
    } 
} 
del(6);//执行操作这里你也许你会疑惑为什么那么麻烦用递归,而不是直接这样删除 
?>
Copy after login

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

四、查找

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

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

代码如下:

<?php
function sel($fid) { 
    $sql="SELECT * FROM `type` WHERE `fid`=$fid"; 
    $rs=mysql_query($sql); 
     
    for ($i = 0; $i < count($rs); $i++) { 
        echo $rs[$i][&#39;name&#39;]; 
         
        sel($rs[$i][&#39;id&#39;]);//递归 
    } 
} 
sel(2);
?>
Copy after login

五、实际数据应用

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

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

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

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

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

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

<?php
function dafenglei_select($m,$id, $fenlei ){ 
     global $menu; 
     $n = str_pad(&#39;&#39;,$m,&#39;-&#39;,STR_PAD_RIGHT); 
     $n = str_replace("-","   ",$n); 
     for($i=0;$i<count($fenlei);$i++){ 
     
      if($fenlei[$i][&#39;classid&#39;]==$id){ 
         $menu .= "<option value="".$fenlei[$i][&#39;id&#39;]."">".$n."|&mdash;".$fenlei[$i][&#39;name&#39;]."</option>n";  
       } 
       $this->dafenglei_select($m+1,$fenlei[$i][&#39;id&#39;], $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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template