美文网首页
PHP无限级分类

PHP无限级分类

作者: EwanRenton | 来源:发表于2018-07-26 16:22 被阅读0次

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 代码实现

<?php
header("Content-Type:text/html;Charset=UTF-8");
// php无限分类下拉列表的代码实现
include_once('db.inc.php') ;

function getList($pid=0,&$result=array(),$spac=0)
{
    $spac=$spac+2;
    $sql=" SELECT * FROM deepcate WHERE pid=$pid ";
    $res=mysql_query($sql);
   
    while($row=mysql_fetch_assoc($res)){
        $row['catename']=str_repeat('&nbsp;&nbsp;',$spac).'|--'.$row['catename'];
        $result[]=$row;
        getList($row['id'],$result,$spac);
    }
    return $result;
}

function displayCate($pid=0,$selected=0){
     $rs=getList($pid);
     $str="";
     $str.="<select name='cate'>";
    foreach($rs as $v){
        if($v['id']==$selected){
            $selected="selected";
        }
       $str.="<option {$selected}>";
       $str.= $v['catename'];
       $str.= "</option>";
    }
    return $str.= "</select>";
}

echo displayCate();

echo "<hr/><hr/>";
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.= "<a href='{$url}{$v['id']}'>{$v['catename']}</a>>>";
    }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代码实现

<?php
header("Content-Type:text/html;Charset=UTF-8");
//递归无限分类原理
include_once ('db.inc.php');
 
function likecate(){
    $sql="select id,catename,path,concat(path,',',id) as fullpath from likecate where 1 order by fullpath asc";
    $res=mysql_query($sql);
    $result=array();
    while($row=mysql_fetch_assoc($res)){
        $deep=count(explode(',',trim($row['fullpath'],',')));
         
        $row['catename']=str_repeat('&nbsp;&nbsp;',$deep*4).'|--'.$row['catename'];
        $result[]=$row;
    }
    return $result;
}
 
 
$res=likecate();
echo "<select name='cate'>";
foreach($res as $v){
    echo "<option>{$v['catename']}</option>";
}
echo "</select>";
 
echo "<hr/><hr/>";
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.="<a href='{$link}'>{$v['catename']}</a>>";
    }
    return $str;
}  
 
echo displayCatePath(4,'likecate.php?p=1&cid=');

相关文章

  • PHP无限级分类

    layout: posttitle: "PHP无限级分类"date: 2016-04-29 09:36:19 +0...

  • PHP递归实现无限级分类

    PHP递归实现无限级分类 在一些复杂的系统中,要求对信息栏目进行无限级的分类,以增强系统的灵活性。那么PHP是如何...

  • PHP实现无限级分类

    php中经常用到无限级分类,牵涉到两种情况 找指定栏目的子孙栏目,即子孙树 找指定的栏目的父栏目/父栏目....顶...

  • php->无限级分类

    用php实现下面的功能,方法:数据库表的合理设计+递归 1.业务逻辑(1)表结构分类表字段:cat_id、cat_...

  • PHP实现无限级分类

    数据格式: 非递归算法 递归算法 最终结果 原创作品,允许转载,转载时请务必以超链接形式标明原始出处、作者信息和本...

  • PHP无限级分类(一)

    无限级分类在web项目中应用非常广泛,比如商品分类、权限节点、组织架构等等。下面介绍两种将二维数组整理成无限级树形...

  • PHP无限级分类(二)

    在 PHP无限级分类(一) 中,我们介绍了两种简单的无限级分类构造方法。下面介绍一种从树形结构中获取子结构的方法:...

  • PHP中实用无限级分类

    在我们工作中,无限级分类应用非常多,其实其思想并不难,无非运用递归思想自己调用自己罢了。下面就来看看我写的无限级分...

  • PHP树形结构无限级分类

    参考地址:https://bubaijun.com/page.php?id=140https://learnku....

  • PHP上机题无限级分类!

网友评论

      本文标题:PHP无限级分类

      本文链接:https://www.haomeiwen.com/subject/ydbbmftx.html