There are already a lot of PHP unlimited classification code on the Internet, and the explanation is also in place, here, I would like to share with you the following piece of code that makes it easy to use:
Copy codeThe code is as follows:
//////////////
///// Database design and example of unlimited classification
//////////////
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> 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 | news | 0 |
| 0-1-5 | 5 | sports news | 0-1 |
| 0-1-5-8 | 8 | basketball news | 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 | Football News | 0-1-5 |
| 0-1-6 | 6 | entertainment news | 0-1 |
| 0-1-7 | 7 | financial news | 0-1 |
| 0-2 | 2 | video | 0 |
| 0-3 | 3 | Image | 0 |
| 0-4 | 4 | blog | 0 |
+ ------------ + ---- + ---------- + --------- +
11 rows in set
Mysql>
The following is the php source file:
Copy codeThe code is as follows:
Untitled Document
$ 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;
?>
In the above code, there is actually a space in the middle of the input effect is very good, please test locally. The layout is messy due to editor problems.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.