比较简单实用的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 相关文章推荐
smtp邮件发送一例
Oct 09 PHP
PHP计数器的实现代码
Jun 08 PHP
PHP实现生成透明背景的PNG缩略图函数分享
Jul 08 PHP
php实现Linux服务器木马排查及加固功能
Dec 29 PHP
PHP动态规划解决0-1背包问题实例分析
Mar 23 PHP
PHP Smarty模版简单使用方法
Mar 30 PHP
thinkPHP实现将excel导入到数据库中的方法
Apr 22 PHP
PHP获取客户端及服务器端IP的封装类
Jul 21 PHP
PHP 中常量的知识整理
Apr 14 PHP
windows下的WAMP环境搭建图文教程(推荐)
Jul 27 PHP
Laravel学习教程之model validation的使用示例
Oct 23 PHP
PHP基于swoole多进程操作示例
Aug 12 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
PHP 开源框架22个简单简介
2009/08/24 PHP
php pki加密技术(openssl)详解
2013/07/01 PHP
PHP的Yii框架中Model模型的学习教程
2016/03/29 PHP
php实现base64图片上传方式实例代码
2017/02/22 PHP
js类后台管理菜单类-MenuSwitch
2007/09/12 Javascript
一个JS小玩意 几个属性相加不能超过一个特定值.
2009/09/29 Javascript
简单时间提示DEMO从0开始一直进行计时
2013/11/19 Javascript
Jquery结合HTML5实现文件上传
2015/06/25 Javascript
基于jquery实现无限级树形菜单
2016/03/22 Javascript
node-http-proxy修改响应结果实例代码
2016/06/06 Javascript
jQuery中的insertBefore(),insertAfter(),after(),before()区别介绍
2016/09/01 Javascript
getElementById().innerHTML与getElementById().value的区别
2016/10/27 Javascript
jquery封装插件时匿名函数形参和实参的写法解释
2017/02/14 Javascript
NodeJS、NPM安装配置步骤(windows版本) 以及环境变量详解
2017/05/13 NodeJs
基于node下的http小爬虫的示例代码
2018/01/11 Javascript
详谈js的变量提升以及使用方法
2018/10/06 Javascript
JS 封装父页面子页面交互接口的实例代码
2019/06/25 Javascript
Elementui表格组件+sortablejs实现行拖拽排序的示例代码
2019/08/28 Javascript
JS代码触发事件代码实例
2020/01/02 Javascript
vue 导航锚点_点击平滑滚动,导航栏对应变化详解
2020/08/10 Javascript
为什么推荐使用JSX开发Vue3
2020/12/28 Vue.js
Python列表解析配合if else的方法
2018/06/23 Python
pytorch 实现删除tensor中的指定行列
2020/01/13 Python
Python关于__name__属性的含义和作用详解
2020/02/19 Python
python opencv pytesseract 验证码识别的实现
2020/08/28 Python
css3 column实现卡片瀑布流布局的示例代码
2018/06/22 HTML / CSS
使用canvas对多图片拼合并导出图片的方法
2018/08/28 HTML / CSS
英国亚马逊官方网站:Amazon.co.uk
2019/08/09 全球购物
如何设定的weblogic的热启动模式(开发模式)与产品发布模式
2012/09/08 面试题
如何写你的创业计划书
2014/01/07 职场文书
四个太阳教学反思
2014/02/01 职场文书
《黄山奇石》教学反思
2014/04/19 职场文书
奥林匹克运动会口号
2014/06/19 职场文书
交通工程专业推荐信
2014/09/06 职场文书
2014年教师党员自我评议
2014/09/19 职场文书
SQL语句多表联合查询的方法示例
2022/04/18 MySQL