PHP无限级分类


layout: post
title: "PHP无限级分类"
date: 2016-04-29 09:36:19 +0800
comments: true
categories: [PHP]


PHP无限级分类主要有两种实现方式,通过递归和全路径实现

一、递归实现

在数据库中存储如下格式的表:
ID pid catename cateorder createtime
1 0 新闻 0 0
2 0 图片 0 0
3 1 国内新闻 0 0
4 1 国际新闻 0 0
5 3 北京新闻 0 0
6 4 美国新闻 0 0
7 2 美女图片 0 0
8 2 风景图片 0 0
create table deepcate
(
id int(10) unsigned not null  auto_increment primary key,
pid int(11) unsigned    not null,
catename    varchar(30) not null,
cateorder   int(11) unsigned    default 0,
createtime  int(10) not null
);

insert into deepcate(pid,catename,cateorder,createtime)values(0,'新闻',0,0);
insert into deepcate(pid,catename,cateorder,createtime)values(0,'图片',0,0);
insert into deepcate(pid,catename,cateorder,createtime)values(1,'国内新闻',0,0);
insert into deepcate(pid,catename,cateorder,createtime)values(1,'国际新闻',0,0);
insert into deepcate(pid,catename,cateorder,createtime)values(3,'北京新闻',0,0);
insert into deepcate(pid,catename,cateorder,createtime)values(4,'美国新闻',0,0);
insert into deepcate(pid,catename,cateorder,createtime)values(2,'美女图片',0,0);
insert into deepcate(pid,catename,cateorder,createtime)values(2,'风景图片',0,0);
insert into deepcate(pid,catename,cateorder,createtime)values(7,'日韩明星',0,0);
insert into deepcate(pid,catename,cateorder,createtime)values(9,'日本AV',0,0);

select * from deepcate;

php 代码实现

";
    foreach($rs as $v){
        if($v['id']==$selected){
            $selected="selected";
        }
       $str.="";
    }
    return $str.= "";
}

echo displayCate();

echo "

"; function getCatePath($cid,&$result){ $sql="SELECT * FROM deepcate WHERE id=$cid "; $rs=mysql_query($sql); $row=mysql_fetch_assoc($rs); if($row){ $result[]=$row; getCatePath($row['pid'],$result); } krsort($result); return $result; } function displayCatePath($cid,$url="deepcate.php?cid="){ $res=getCatePath($cid,$result); $str=""; foreach($res as $v){ $str.= "{$v['catename']}>>"; }return $str; } echo displayCatePath(10);

二、全路径实现

use imooc;
create table    likecate
(
id  int(11) not null  auto_increment primary key,
path    varchar(200)    not null,
catename    varchar(30) not null,
cateorder   int(11) not null    default 0,
createtime  int(11) not null
);

insert into likecate(path,catename,cateorder,createtime)values('','手机',0,0);
insert into likecate(path,catename,cateorder,createtime)values('1','功能手机',0,0);
insert into likecate(path,catename,cateorder,createtime)values('1,2','老人手机',0,0);
insert into likecate(path,catename,cateorder,createtime)values('1,2','儿童手机',0,0);
insert into likecate(path,catename,cateorder,createtime)values('1','智能手机',0,0);
insert into likecate(path,catename,cateorder,createtime)values('1,5','andriod手机',0,0);
insert into likecate(path,catename,cateorder,createtime)values('1,5','ios手机',0,0);
insert into likecate(path,catename,cateorder,createtime)values('1,5','winphoto手机',0,0);
insert into likecate(path,catename,cateorder,createtime)values('1,2,4','色盲手机',0,0);
insert into likecate(path,catename,cateorder,createtime)values('1,2,3','大字手机',0,0);

select * from likecate;

select id,catename,path,concat(path,',',id) as fullpath from likecate where 1 order by fullpath asc;

#结果显示如图
+----+--------------+-------+----------+
| id | catename     | path  | fullpath |
+----+--------------+-------+----------+
|  1 | 手机         |       | ,1       |
|  2 | 功能手机     | 1     | 1,2      |
|  3 | 老人手机     | 1,2   | 1,2,3    |
| 10 | 大字手机     | 1,2,3 | 1,2,3,10 |
|  4 | 儿童手机     | 1,2   | 1,2,4    |
|  9 | 色盲手机     | 1,2,4 | 1,2,4,9  |
|  5 | 智能手机     | 1     | 1,5      |
|  6 | andriod手机  | 1,5   | 1,5,6    |
|  7 | ios手机      | 1,5   | 1,5,7    |
|  8 | winphoto手机 | 1,5   | 1,5,8    |
+----+--------------+-------+----------+

php代码实现

";
foreach($res as $v){
    echo "";
}
echo "";
 
echo "

"; function getPathCate($cateid){ $sql="select *,concat(path,',',id)fullpath from likecate where id=$cateid"; $res=mysql_query($sql); $row=mysql_fetch_assoc($res); $ids=$row['fullpath']; $sql="select * from likecate where id in($ids) order by id asc"; $res=mysql_query($sql); $result=array(); while($row=mysql_fetch_assoc($res)){ $result[]=$row; } return $result; } function displayCatePath($cateid,$link='likecate.php?cid='){ $res=getPathCate($cateid); $str=""; foreach($res as $v){ $str.="{$v['catename']}>"; } return $str; } echo displayCatePath(4,'likecate.php?p=1&cid=');

你可能感兴趣的:(PHP无限级分类)