下面一段代码是创建相应数据库的sql代码:
//////////////<BR> //////无限分类的数据库设计及样例<BR> //////////////<BR> mysql> create database db_kind;<BR> Query OK, 1 row affected<br><br> mysql> use db_kind;<BR> Database changed<BR> mysql> create table tb_kind(<BR> -> id int not null auto_increment primary key,<BR> -> pid int,<BR> -> path varchar(200)<BR> -> );<BR> Query OK, 0 rows affected<br><br> mysql> insert into tb_kind values(null,"新闻",0,0);<BR> Query OK, 1 row affected<br><br> mysql> insert into tb_kind values(null,"视频",0,0);<BR> Query OK, 1 row affected<br><br> mysql> insert into tb_kind values(null,"图片",0,0);<BR> Query OK, 1 row affected<br><br> mysql> insert into tb_kind values(null,"博客",0,0);<BR> Query OK, 1 row affected<br><br> mysql> insert into tb_kind values(null,"体育新闻",1,"0-1");<BR> Query OK, 1 row affected<br><br> mysql> insert into tb_kind values(null,"娱乐新闻",1,"0-1");<BR> Query OK, 1 row affected<br><br> mysql> insert into tb_kind values(null,"财经新闻",1,"0-1");<BR> Query OK, 1 row affected<br><br> mysql> select * from db_kind;<BR> ERROR 1146 : Table 'db_kind.db_kind' doesnot exist<BR> mysql> selec<span>@本文来*源gaodai#ma#com搞*!代#%^码$网*</span><textarea>搞gaodaima代码</textarea>t * from tb<BR> _kind;<BR> +----+----------+-----+------+<BR> | id | pname | pid | path |<BR> +----+----------+-----+------+<BR> | 1 | 新闻 | 0 | 0 |<BR> | 2 | 视频 | 0 | 0 |<BR> | 3 | 图片 | 0 | 0 |<BR> | 4 | 博客 | 0 | 0 |<BR> | 5 | 体育新闻 | 1 | 0-1 |<BR> | 6 | 娱乐新闻 | 1 | 0-1 |<BR> | 7 | 财经新闻 | 1 | 0-1 |<BR> +----+----------+-----+------+<BR> 7 rows in set<BR> mysql> insert into tb_kind values(null,"篮球新闻",5,"0-1-5");<BR> Query OK, 1 row affected<br><br> mysql> insert into tb_kind values(null,"足球新闻",5,"0-1-5");<BR> Query OK, 1 row affected<br><br> mysql> select * from tb_kind;<BR> +----+----------+-----+-------+<BR> | id | pname | pid | path |<BR> +----+----------+-----+-------+<BR> | 1 | 新闻 | 0 | 0 |<BR> | 2 | 视频 | 0 | 0 |<BR> | 3 | 图片 | 0 | 0 |<BR> | 4 | 博客 | 0 | 0 |<BR> | 5 | 体育新闻 | 1 | 0-1 |<BR> | 6 | 娱乐新闻 | 1 | 0-1 |<BR> | 7 | 财经新闻 | 1 | 0-1 |<BR> | 8 | 篮球新闻 | 5 | 0-1-5 |<BR> | 9 | 足球新闻 | 5 | 0-1-5 |<BR> +----+----------+-----+-------+<BR> 9 rows in set<br><br> mysql> insert into tb_kind values(null,"NBA",8,"0-1-5-8");<BR> Query OK, 1 row affected<br><br> mysql> insert into tb_kind values(null,"CBA",8,"0-1-5-8");<BR> Query OK, 1 row affected<br><br> mysql> select * from tb_kind;<BR> +----+----------+-----+---------+<BR> | id | pname | pid | path |<BR> +----+----------+-----+---------+<BR> | 1 | 新闻 | 0 | 0 |<BR> | 2 | 视频 | 0 | 0 |<BR> | 3 | 图片 | 0 | 0 |<BR> | 4 | 博客 | 0 | 0 |<BR> | 5 | 体育新闻 | 1 | 0-1 |<BR> | 6 | 娱乐新闻 | 1 | 0-1 |<BR> | 7 | 财经新闻 | 1 | 0-1 |<BR> | 8 | 篮球新闻 | 5 | 0-1-5 |<BR> | 9 | 足球新闻 | 5 | 0-1-5 |<BR> | 10 | NBA | 8 | 0-1-5-8 |<BR> | 11 | CBA | 8 | 0-1-5-8 |<BR> +----+----------+-----+---------+<BR> 11 rows in set<br><br> mysql> select concat(path,"-",id) from tb_kind;<BR> +---------------------+<BR> | concat(path,"-",id) |<BR> +---------------------+<BR> | 0-1 |<BR> | 0-2 |<BR> | 0-3 |<BR> | 0-4 |<BR> | 0-1-5 |<BR> | 0-1-6 |<BR> | 0-1-7 |<BR> | 0-1-5-8 |<BR> | 0-1-5-9 |<BR> | 0-1-5-8-10 |<BR> | 0-1-5-8-11 |<BR> +---------------------+<BR> 11 rows in set<br><br> mysql> select concat(path,"-",id) from tb_kind;<BR> +---------------------+<BR> | concat(path,"-",id) |<BR> +---------------------+<BR> | 0-1 |<BR> | 0-2 |<BR> | 0-3 |<BR> | 0-4 |<BR> | 0-1-5 |<BR> | 0-1-6 |<BR> | 0-1-7 |<BR> | 0-1-5-8 |<BR> | 0-1-5-9 |<BR> | 0-1-5-8-10 |<BR> | 0-1-5-8-11 |<BR> +---------------------+<BR> 11 rows in set<br><br> mysql> select concat(path,"-",id) as abs from tb_kind order by abs.path;<BR> ERROR 1054 : Unknown column 'abs.path' in 'order clause'<BR> mysql> select concat(path,"-",id) as abs from tb_kind order by abs<br><br> +------------+<BR> | abs |<BR> +------------+<BR> | 0-1 |<BR> | 0-1-5 |<BR> | 0-1-5-8 |<BR> | 0-1-5-8-10 |<BR> | 0-1-5-8-11 |<BR> | 0-1-5-9 |<BR> | 0-1-6 |<BR> | 0-1-7 |<BR> | 0-2 |<BR> | 0-3 |<BR> | 0-4 |<BR> +------------+<BR> 11 rows in set<BR> mysql> select concat(path,"-",id) as,id,name,path abs from tb_kind order by abs;<BR> 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<BR> mysql> select concat(path,"-",id) as abs,<BR> id,pname,path abs from tb_kind order by abs;<BR> +------------+----+----------+---------+<BR> | abs | id | pname | abs |<BR> +------------+----+----------+---------+<BR> | 0-1 | 1 | 新闻 | 0 |<BR> | 0-1-5 | 5 | 体育新闻 | 0-1 |<BR> | 0-1-5-8 | 8 | 篮球新闻 | 0-1-5 |<BR> | 0-1-5-8-10 | 10 | NBA | 0-1-5-8 |<BR> | 0-1-5-8-11 | 11 | CBA | 0-1-5-8 |<BR> | 0-1-5-9 | 9 | 足球新闻 | 0-1-5 |<BR> | 0-1-6 | 6 | 娱乐新闻 | 0-1 |<BR> | 0-1-7 | 7 | 财经新闻 | 0-1 |<BR> | 0-2 | 2 | 视频 | 0 |<BR> | 0-3 | 3 | 图片 | 0 |<BR> | 0-4 | 4 | 博客 | 0 |<BR> +------------+----+----------+---------+<BR> 11 rows in set<BR> mysql><BR>
下面是php源文件:
<BR> <BR> <BR> <BR><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <BR><title>无标题文档</title> <BR> <BR><body> <BR><!---ecms -ecms -ecms 显示结果 <BR>新闻 <BR>体育新闻 <BR>篮球新闻 <BR>NBA <BR>CBA <BR>足球新闻 <BR>娱乐新闻 <BR>财经新闻 <BR>视频 <BR>图片 <BR>博客 <BR>--> <BR><? <BR>$conn=mysql_connect("localhost","root","root"); <BR>mysql_select_db("db_kind"); <BR>mysql_query("set names utf8"); <BR>$sql="select concat(path,'-',id) as abspath,id,pname,path from tb_kind order by abspath"; <BR>$rs=mysql_query($sql); <BR>while($result=mysql_fetch_assoc($rs)){ <BR>$num=count(explode("-",$result[path]))-1; <BR>$new_str=str_repeat("---",$num); <BR>echo $new_str.$result[pname]; <BR>echo "<br>"; <BR>} <BR>$str=str_repeat("=",10); <BR>echo $str; <BR>$num=count(explode("-","0-1-5-8"))-1; <BR>echo $num; <BR>?> <BR> <BR> <BR>
上面的代码中其实中间有空格的输入效果还是非常不错的,请大家本地测试。因编辑器问题导致排版混乱。