转载自:玉米串
关于PHP的无限分类代码,网上已经有很多了,讲解的也很到位,在这里我分享一下我用着很顺手的一个。
下面一段代码是创建相应数据库的sql代码:
1 //////////////
2 //////无限分类的数据库设计及样例
3 //////////////
4 mysql> create database db_kind;
5 Query OK, 1 row affected
6
7 mysql> use db_kind;
8 Database changed
9 mysql> create table tb_kind(
10 -> id int not null auto_increment primary key,
11 -> pid int,
12 -> path varchar(200)
13 -> );
14 Query OK, 0 rows affected
15
16 mysql> insert into tb_kind values(null,"新闻",0,0);
17 Query OK, 1 row affected
18
19 mysql> insert into tb_kind values(null,"视频",0,0);
20 Query OK, 1 row affected
21
22 mysql> insert into tb_kind values(null,"图片",0,0);
23 Query OK, 1 row affected
24
25 mysql> insert into tb_kind values(null,"博客",0,0);
26 Query OK, 1 row affected
27
28 mysql> insert into tb_kind values(null,"体育新闻",1,"0-1");
29 Query OK, 1 row affected
30
31 mysql> insert into tb_kind values(null,"娱乐新闻",1,"0-1");
32 Query OK, 1 row affected
33
34 mysql> insert into tb_kind values(null,"财经新闻",1,"0-1");
35 Query OK, 1 row affected
36
37 mysql> select * from db_kind;
38 ERROR 1146 : Table 'db_kind.db_kind' doesnot exist
39 mysql> select * from tb
40 _kind;
41 +----+----------+-----+------+
42 | id | pname | pid | path |
43 +----+----------+-----+------+
44 | 1 | 新闻 | 0 | 0 |
45 | 2 | 视频 | 0 | 0 |
46 | 3 | 图片 | 0 | 0 |
47 | 4 | 博客 | 0 | 0 |
48 | 5 | 体育新闻 | 1 | 0-1 |
49 | 6 | 娱乐新闻 | 1 | 0-1 |
50 | 7 | 财经新闻 | 1 | 0-1 |
51 +----+----------+-----+------+
52 7 rows in set
53 mysql> insert into tb_kind values(null,"篮球新闻",5,"0-1-5");
54 Query OK, 1 row affected
55
56 mysql> insert into tb_kind values(null,"足球新闻",5,"0-1-5");
57 Query OK, 1 row affected
58
59 mysql> select * from tb_kind;
60 +----+----------+-----+-------+
61 | id | pname | pid | path |
62 +----+----------+-----+-------+
63 | 1 | 新闻 | 0 | 0 |
64 | 2 | 视频 | 0 | 0 |
65 | 3 | 图片 | 0 | 0 |
66 | 4 | 博客 | 0 | 0 |
67 | 5 | 体育新闻 | 1 | 0-1 |
68 | 6 | 娱乐新闻 | 1 | 0-1 |
69 | 7 | 财经新闻 | 1 | 0-1 |
70 | 8 | 篮球新闻 | 5 | 0-1-5 |
71 | 9 | 足球新闻 | 5 | 0-1-5 |
72 +----+----------+-----+-------+
73 9 rows in set
74
75 mysql> insert into tb_kind values(null,"NBA",8,"0-1-5-8");
76 Query OK, 1 row affected
77
78 mysql> insert into tb_kind values(null,"CBA",8,"0-1-5-8");
79 Query OK, 1 row affected
80
81 mysql> select * from tb_kind;
82 +----+----------+-----+---------+
83 | id | pname | pid | path |
84 +----+----------+-----+---------+
85 | 1 | 新闻 | 0 | 0 |
86 | 2 | 视频 | 0 | 0 |
87 | 3 | 图片 | 0 | 0 |
88 | 4 | 博客 | 0 | 0 |
89 | 5 | 体育新闻 | 1 | 0-1 |
90 | 6 | 娱乐新闻 | 1 | 0-1 |
91 | 7 | 财经新闻 | 1 | 0-1 |
92 | 8 | 篮球新闻 | 5 | 0-1-5 |
93 | 9 | 足球新闻 | 5 | 0-1-5 |
94 | 10 | NBA | 8 | 0-1-5-8 |
95 | 11 | CBA | 8 | 0-1-5-8 |
96 +----+----------+-----+---------+
97 11 rows in set
98
99 mysql> select concat(path,"-",id) from tb_kind;
100 +---------------------+
101 | concat(path,"-",id) |
102 +---------------------+
103 | 0-1 |
104 | 0-2 |
105 | 0-3 |
106 | 0-4 |
107 | 0-1-5 |
108 | 0-1-6 |
109 | 0-1-7 |
110 | 0-1-5-8 |
111 | 0-1-5-9 |
112 | 0-1-5-8-10 |
113 | 0-1-5-8-11 |
114 +---------------------+
115 11 rows in set
116
117 mysql> select concat(path,"-",id) from tb_kind;
118 +---------------------+
119 | concat(path,"-",id) |
120 +---------------------+
121 | 0-1 |
122 | 0-2 |
123 | 0-3 |
124 | 0-4 |
125 | 0-1-5 |
126 | 0-1-6 |
127 | 0-1-7 |
128 | 0-1-5-8 |
129 | 0-1-5-9 |
130 | 0-1-5-8-10 |
131 | 0-1-5-8-11 |
132 +---------------------+
133 11 rows in set
134
135 mysql> select concat(path,"-",id) as abs from tb_kind order by abs.path;
136 ERROR 1054 : Unknown column 'abs.path' in 'order clause'
137 mysql> select concat(path,"-",id) as abs from tb_kind order by abs
138 ;
139 +------------+
140 | abs |
141 +------------+
142 | 0-1 |
143 | 0-1-5 |
144 | 0-1-5-8 |
145 | 0-1-5-8-10 |
146 | 0-1-5-8-11 |
147 | 0-1-5-9 |
148 | 0-1-6 |
149 | 0-1-7 |
150 | 0-2 |
151 | 0-3 |
152 | 0-4 |
153 +------------+
154 11 rows in set
155 mysql> select concat(path,"-",id) as,id,name,path abs from tb_kind order by abs;
156 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
157 mysql> select concat(path,"-",id) as abs,
158 id,pname,path abs from tb_kind order by abs;
159 +------------+----+----------+---------+
160 | abs | id | pname | abs |
161 +------------+----+----------+---------+
162 | 0-1 | 1 | 新闻 | 0 |
163 | 0-1-5 | 5 | 体育新闻 | 0-1 |
164 | 0-1-5-8 | 8 | 篮球新闻 | 0-1-5 |
165 | 0-1-5-8-10 | 10 | NBA | 0-1-5-8 |
166 | 0-1-5-8-11 | 11 | CBA | 0-1-5-8 |
167 | 0-1-5-9 | 9 | 足球新闻 | 0-1-5 |
168 | 0-1-6 | 6 | 娱乐新闻 | 0-1 |
169 | 0-1-7 | 7 | 财经新闻 | 0-1 |
170 | 0-2 | 2 | 视频 | 0 |
171 | 0-3 | 3 | 图片 | 0 |
172 | 0-4 | 4 | 博客 | 0 |
173 +------------+----+----------+---------+
174 11 rows in set
175 mysql>
下面是php源文件:
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>无标题文档</title>
6 </head>
7
8 <body>
9 <!--显示结果
10 新闻
11 体育新闻
12 篮球新闻
13 NBA
14 CBA
15 足球新闻
16 娱乐新闻
17 财经新闻
18 视频
19 图片
20 博客
21 -->
22 <?
23 $conn=mysql_connect("localhost","root","root");
24 mysql_select_db("db_kind");
25 mysql_query("set names utf8");
26 $sql="select concat(path,'-',id) as abspath,id,pname,path from tb_kind order by abspath";
27 $rs=mysql_query($sql);
28 while($result=mysql_fetch_assoc($rs)){
29 $num=count(explode("-",$result[path]))-1;
30 $new_str=str_repeat("---",$num);
31 echo $new_str.$result[pname];
32 echo "<br>";
33 }
34 $str=str_repeat("=",10);
35 echo $str;
36 $num=count(explode("-","0-1-5-8"))-1;
37 echo $num;
38
39
40 ?>
41 </body>
42 </html>