比较简单实用的PHP无限分类源码分享(思路不错)


Posted in PHP onOctober 13, 2011

下面一段代码是创建相应数据库的sql代码:

 //////////////
   //////无限分类的数据库设计及样例
   //////////////
   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,"新闻",0,0);
 Query OK, 1 row affected
 mysql> insert into tb_kind values(null,"视频",0,0);
 Query OK, 1 row affected
 mysql> insert into tb_kind values(null,"图片",0,0);
 Query OK, 1 row affected
 mysql> insert into tb_kind values(null,"博客",0,0);
 Query OK, 1 row affected
 mysql> insert into tb_kind values(null,"体育新闻",1,"0-1");
 Query OK, 1 row affected
 mysql> insert into tb_kind values(null,"娱乐新闻",1,"0-1");
 Query OK, 1 row affected
 mysql> insert into tb_kind values(null,"财经新闻",1,"0-1");
 Query OK, 1 row affected
 mysql> select * from db_kind;
 ERROR 1146 : Table 'db_kind.db_kind' doesnot exist
 mysql> select * from tb
 _kind;
 +----+----------+-----+------+
 | id | pname    | pid | path |
 +----+----------+-----+------+
 |  1 | 新闻     |   0 | 0    |
 |  2 | 视频     |   0 | 0    |
 |  3 | 图片     |   0 | 0    |
 |  4 | 博客     |   0 | 0    |
 |  5 | 体育新闻 |   1 | 0-1  |
 |  6 | 娱乐新闻 |   1 | 0-1  |
 |  7 | 财经新闻 |   1 | 0-1  |
 +----+----------+-----+------+
 7 rows in set
 mysql> insert into tb_kind values(null,"篮球新闻",5,"0-1-5");
 Query OK, 1 row affected
 mysql> insert into tb_kind values(null,"足球新闻",5,"0-1-5");
 Query OK, 1 row affected
 mysql> select * from tb_kind;
 +----+----------+-----+-------+
 | id | pname    | pid | path  |
 +----+----------+-----+-------+
 |  1 | 新闻     |   0 | 0     |
 |  2 | 视频     |   0 | 0     |
 |  3 | 图片     |   0 | 0     |
 |  4 | 博客     |   0 | 0     |
 |  5 | 体育新闻 |   1 | 0-1   |
 |  6 | 娱乐新闻 |   1 | 0-1   |
 |  7 | 财经新闻 |   1 | 0-1   |
 |  8 | 篮球新闻 |   5 | 0-1-5 |
 |  9 | 足球新闻 |   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 | 新闻     |   0 | 0       |
 |  2 | 视频     |   0 | 0       |
 |  3 | 图片     |   0 | 0       |
 |  4 | 博客     |   0 | 0       |
 |  5 | 体育新闻 |   1 | 0-1     |
 |  6 | 娱乐新闻 |   1 | 0-1     |
 |  7 | 财经新闻 |   1 | 0-1     |
 |  8 | 篮球新闻 |   5 | 0-1-5   |
 |  9 | 足球新闻 |   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源文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>无标题文档</title> 
</head> 
<body> 
<!--显示结果 
新闻 
体育新闻 
篮球新闻 
NBA 
CBA 
足球新闻 
娱乐新闻 
财经新闻 
视频 
图片 
博客 
--> 
<? 
$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 "<br>"; 
} 
$str=str_repeat("=",10); 
echo $str; 
$num=count(explode("-","0-1-5-8"))-1; 
echo $num; 
?> 
</body> 
</html>

上面的代码中其实中间有空格的输入效果还是非常不错的,请大家本地测试。因编辑器问题导致排版混乱。
PHP 相关文章推荐
杏林同学录(五)
Oct 09 PHP
如何从一个php文件向另一个地址post数据,不用表单和隐藏的变量的
Mar 06 PHP
Dwz与thinkphp整合下的数据导出到Excel实例
Dec 04 PHP
php内存缓存实现方法
Jan 24 PHP
分享PHP-pcntl 实现多进程代码
Sep 30 PHP
ecshop适应在PHP7的修改方法解决报错的实现
Nov 01 PHP
PHP重定向与伪静态区别
Feb 19 PHP
浅谈PHP中pack、unpack的详细用法
Mar 12 PHP
PHP消息队列实现及应用详解【队列处理订单系统和配送系统】
May 20 PHP
解决在laravel中leftjoin带条件查询没有返回右表为NULL的问题
Oct 15 PHP
laravel 解决crontab不执行的问题
Oct 22 PHP
基于PHP+Mysql简单实现了图书购物车系统的实例详解
Aug 06 PHP
php 错误处理经验分享
Oct 11 #PHP
php购物车实现代码
Oct 10 #PHP
使用PHP实现密保卡功能实现代码&amp;lt;打包下载直接运行&amp;gt;
Oct 09 #PHP
PHP实现时间轴函数代码
Oct 08 #PHP
PHP+Mysql+jQuery实现动态展示信息
Oct 08 #PHP
PHP+Mysql+jQuery实现发布微博程序 jQuery篇
Oct 08 #PHP
php实现用户在线时间统计详解
Oct 08 #PHP
You might like
使用bcompiler对PHP文件进行加密的代码
2010/08/29 PHP
PHP生成唯一订单号
2015/07/05 PHP
PHP多文件上传实例
2015/07/09 PHP
ThinkPHP3.2.1图片验证码实现方法
2016/08/19 PHP
实例讲解PHP验证邮箱是否合格
2019/01/28 PHP
网页上的Javascript编辑器和代码格式化
2010/04/25 Javascript
不同的jQuery API来处理不同的浏览器事件
2012/12/09 Javascript
Jquery实现点击切换图片并隐藏显示内容(2种方法实现)
2013/04/11 Javascript
Javascript访问器属性实例分析
2014/12/30 Javascript
简单实现限制uploadify上传个数
2015/11/16 Javascript
AngularJS 作用域详解及示例代码
2016/08/17 Javascript
Vue.js每天必学之数据双向绑定
2016/09/05 Javascript
基于jQuery实现Tabs选项卡自定义插件
2016/11/21 Javascript
javascript高级模块化require.js的具体使用方法
2017/10/31 Javascript
使用JS模拟锚点跳转的实例
2018/02/01 Javascript
jQuery常见的遍历DOM操作详解
2018/09/05 jQuery
Angular(5.2-&gt;6.1)升级小结
2018/12/27 Javascript
基于layui的table插件进行复选框联动功能的实现方法
2019/09/19 Javascript
如何优雅地在Node应用中进行错误异常处理
2019/11/25 Javascript
在NodeJs中使用node-schedule增加定时器任务的方法
2020/06/08 NodeJs
Python中使用装饰器和元编程实现结构体类实例
2015/01/28 Python
在Django同1个页面中的多表单处理详解
2017/01/25 Python
python非递归全排列实现方法
2017/04/10 Python
python访问抓取网页常用命令总结
2017/04/11 Python
django传值给模板, 再用JS接收并进行操作的实例
2018/05/28 Python
全球领先的美容用品专卖店:Beauty Plus Salon
2018/09/04 全球购物
JD Sports西班牙:英国领先的运动服装公司
2020/01/06 全球购物
什么是serialVersionUID
2016/03/04 面试题
厨师长岗位职责
2014/03/02 职场文书
母亲节感恩活动记录
2014/03/16 职场文书
社区服务活动小结
2014/07/08 职场文书
化工实习心得体会
2014/09/09 职场文书
民主生活会剖析材料
2014/09/30 职场文书
优秀班主任先进事迹材料
2014/12/16 职场文书
投诉书格式范本
2015/07/02 职场文书
python使用matplotlib绘制图片时x轴的刻度处理
2021/08/30 Python