Home > Backend Development > PHP Tutorial > PHP unlimited classification program_PHP tutorial

PHP unlimited classification program_PHP tutorial

WBOY
Release: 2016-07-13 16:55:15
Original
835 people have browsed it

I have often introduced the issue of infinite classification in the past. In most cases, we use PHP recursion to achieve it. The infinite classification introduced today does not require recursion. The method is very simple and friends in need can refer to it.

The code is as follows Copy code

///////////////
//////Infinitely classified database design and examples
//////////////
mysql> create database db_kind;
Query OK, 1 row affected

mysql> use db_kind;
Database changed
mysql> create table tb_kind(
-> id int not null auto_increment primary key,
-> pid int,
-> path varchar(200)
-> );
Query OK, 0 rows affected

mysql> insert into tb_kind values(null,"news",0,0);
Query OK, 1 row affected

mysql> insert into tb_kind values(null,"video",0,0);
Query OK, 1 row affected

mysql> insert into tb_kind values(null,"picture",0,0);
Query OK, 1 row affected

mysql> insert into tb_kind values(null,"Blog",0,0);
Query OK, 1 row affected

mysql> insert into tb_kind values(null,"Sports News",1,"0-1");
Query OK, 1 row affected

mysql> insert into tb_kind values(null,"Entertainment News",1,"0-1");
Query OK, 1 row affected

mysql> insert into tb_kind values(null,"Financial News",1,"0-1");
Query OK, 1 row affected

mysql> select * from db_kind;
ERROR 1146: Table 'db_kind.db_kind' does not exist
mysql> select * from tb
_kind;
+----+----------+-----+------+
| id | pname | pid | path |
+----+----------+-----+------+
| 1 | News | 0 | 0 |
| 2 | Video | 0 | 0 |
| 3 | Pictures | 0 | 0 |
| 4 | Blog | 0 | 0 |
| 5 | Sports News | 1 | 0-1 |
| 6 | Entertainment News | 1 | 0-1 |
| 7 | Financial News | 1 | 0-1 |
+----+----------+-----+------+
7 rows in set
mysql> insert into tb_kind values(null,"Basketball News",5,"0-1-5");
Query OK, 1 row affected

mysql> insert into tb_kind values(null,"Football News",5,"0-1-5");
Query OK, 1 row affected

mysql> select * from tb_kind;
+----+----------+-----+-------+
| id | pname | pid | path |
+----+----------+-----+-------+
| 1 | News | 0 | 0 |
| 2 | Video | 0 | 0 |
| 3 | Pictures | 0 | 0 |
| 4 | Blog | 0 | 0 |
| 5 | Sports News | 1 | 0-1 |
| 6 | Entertainment News | 1 | 0-1 |
| 7 | Financial News | 1 | 0-1 |
| 8 | Basketball News | 5 | 0-1-5 |
| 9 | Football News | 5 | 0-1-5 |
+----+----------+-----+-------+
9 rows in set

mysql> insert into tb_kind values(null,"NBA",8,"0-1-5-8");
Query OK, 1 row affected

mysql> insert into tb_kind values(null,"CBA",8,"0-1-5-8");
Query OK, 1 row affected

mysql> select * from tb_kind;
+----+----------+-----+---------+
| id | pname | pid | path |
+----+----------+-----+---------+
| 1 | News | 0 | 0 |
| 2 | Video | 0 | 0 |
| 3 | Pictures | 0 | 0 |
| 4 | Blog | 0 | 0 |
| 5 | Sports News | 1 | 0-1 |
| 6 | Entertainment News | 1 | 0-1 |
| 7 | Financial News | 1 | 0-1 |
| 8 | Basketball News | 5 | 0-1-5 |
| 9 | Football News | 5 | 0-1-5 |
| 10 | NBA | 8 | 0-1-5-8 |
| 11 | CBA | 8 | 0-1-5-8 |
+----+----------+-----+---------+
11 rows in set

mysql> select concat(path,"-",id) from tb_kind;
+---------------------+
| concat(path,"-",id) |
+---------------------+
| 0-1 |
| 0-2 |
| 0-3 |
| 0-4 |
| 0-1-5 |
| 0-1-6 |
| 0-1-7 |
| 0-1-5-8 |
| 0-1-5-9 |
| 0-1-5-8-10 |
| 0-1-5-8-11 |
+---------------------+
11 rows in set

mysql> select concat(path,"-",id) from tb_kind;
+---------------------+
| concat(path,"-",id) |
+---------------------+
| 0-1 |
| 0-2 |
| 0-3 |
| 0-4 |
| 0-1-5 |
| 0-1-6 |
| 0-1-7 |
| 0-1-5-8 |
| 0-1-5-9 |
| 0-1-5-8-10 |
| 0-1-5-8-11 |
+---------------------+
11 rows in set

mysql> select concat(path,"-",id) as abs from tb_kind order by abs.path;
ERROR 1054 : Unknown column 'abs.path' in 'order clause'
mysql> select concat(path,"-",id) as abs from tb_kind order by abs

+------------+
| abs |
+------------+
| 0-1 |
| 0-1-5 |
| 0-1-5-8 |
| 0-1-5-8-10 |
| 0-1-5-8-11 |
| 0-1-5-9 |
| 0-1-6 |
| 0-1-7 |
| 0-2 |
| 0-3 |
| 0-4 |
+------------+
11 rows in set
mysql> select concat(path,"-",id) as,id,name,path abs from tb_kind order by abs;
ERROR 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'id,name,path abs from tb_kind order by abs' at line 1
mysql> select concat(path,"-",id) as abs,
id,pname,path abs from tb_kind order by abs;
+------------+----+----------+---------+
| abs | id | pname | abs |
+------------+----+----------+---------+
| 0-1 | 1 | 新闻 | 0 |
| 0-1-5 | 5 | 体育新闻 | 0-1 |
| 0-1-5-8 | 8 | 篮球新闻 | 0-1-5 |
| 0-1-5-8-10 | 10 | NBA | 0-1-5-8 |
| 0-1-5-8-11 | 11 | CBA | 0-1-5-8 |
| 0-1-5-9 | 9 | 足球新闻 | 0-1-5 |
| 0-1-6 | 6 | 娱乐新闻 | 0-1 |
| 0-1-7 | 7 | 财经新闻 | 0-1 |
| 0-2 | 2 | 视频 | 0 |
| 0-3 | 3 | 图片 | 0 |
| 0-4 | 4 | 博客 | 0 |
+------------+----+----------+---------+
11 rows in set
mysql>

php处理分类源码





无标题文档




$conn=mysql_connect("localhost","root","root");
mysql_select_db("db_kind");
mysql_query("set names utf8");
$sql="select concat(path,'-',id) as abspath,id,pname,path from tb_kind order by abspath";
$rs=mysql_query($sql);
while($result=mysql_fetch_assoc($rs)){
$num=count(explode("-",$result[path]))-1;
$new_str=str_repeat("---",$num);
echo $new_str.$result[pname];
echo "
";
}
$str=str_repeat("=",10);
echo $str;
$num=count(explode("-","0-1-5-8"))-1;
echo $num;
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/631691.htmlTechArticle以前也经常介绍关于无限分类这个问题,大多数情况我们都是利用php递归来实现,今天介绍的这篇无限分类不需要递归哦,方法很简单有需...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template