比较简单实用的PHP无限分类源码分享(思路不错)
2023-06-13 09:14:30 时间
下面一段代码是创建相应数据库的sql代码:
复制代码代码如下:
//////////////
//////无限分类的数据库设计及样例
//////////////
mysql>createdatabasedb_kind;
QueryOK,1rowaffected
mysql>usedb_kind;
Databasechanged
mysql>createtabletb_kind(
->idintnotnullauto_incrementprimarykey,
->pidint,
->pathvarchar(200)
->);
QueryOK,0rowsaffected
mysql>insertintotb_kindvalues(null,"新闻",0,0);
QueryOK,1rowaffected
mysql>insertintotb_kindvalues(null,"视频",0,0);
QueryOK,1rowaffected
mysql>insertintotb_kindvalues(null,"图片",0,0);
QueryOK,1rowaffected
mysql>insertintotb_kindvalues(null,"博客",0,0);
QueryOK,1rowaffected
mysql>insertintotb_kindvalues(null,"体育新闻",1,"0-1");
QueryOK,1rowaffected
mysql>insertintotb_kindvalues(null,"娱乐新闻",1,"0-1");
QueryOK,1rowaffected
mysql>insertintotb_kindvalues(null,"财经新闻",1,"0-1");
QueryOK,1rowaffected
mysql>select*fromdb_kind;
ERROR1146:Table"db_kind.db_kind"doesnotexist
mysql>select*fromtb
_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 |
+----+----------+-----+------+
7rowsinset
mysql>insertintotb_kindvalues(null,"篮球新闻",5,"0-1-5");
QueryOK,1rowaffected
mysql>insertintotb_kindvalues(null,"足球新闻",5,"0-1-5");
QueryOK,1rowaffected
mysql>select*fromtb_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|
+----+----------+-----+-------+
9rowsinset
mysql>insertintotb_kindvalues(null,"NBA",8,"0-1-5-8");
QueryOK,1rowaffected
mysql>insertintotb_kindvalues(null,"CBA",8,"0-1-5-8");
QueryOK,1rowaffected
mysql>select*fromtb_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|
+----+----------+-----+---------+
11rowsinset
mysql>selectconcat(path,"-",id)fromtb_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 |
+---------------------+
11rowsinset
mysql>selectconcat(path,"-",id)fromtb_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 |
+---------------------+
11rowsinset
mysql>selectconcat(path,"-",id)asabsfromtb_kindorderbyabs.path;
ERROR1054:Unknowncolumn"abs.path"in"orderclause"
mysql>selectconcat(path,"-",id)asabsfromtb_kindorderbyabs
+------------+
|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 |
+------------+
11rowsinset
mysql>selectconcat(path,"-",id)as,id,name,pathabsfromtb_kindorderbyabs;
ERROR1064:YouhaveanerrorinyourSQLsyntax;checkthemanualthatcorrespondstoyourMySQLserverversionfortherightsyntaxtousenear"id,name,pathabsfromtb_kindorderbyabs"atline1
mysql>selectconcat(path,"-",id)asabs,
id,pname,pathabsfromtb_kindorderbyabs;
+------------+----+----------+---------+
|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 |
+------------+----+----------+---------+
11rowsinset
mysql>
下面是php源文件:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-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("setnamesutf8");
$sql="selectconcat(path,"-",id)asabspath,id,pname,pathfromtb_kindorderbyabspath";
$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连接ldap服务器,使用PHP连接LDAP服务器[通俗易懂]
- 微信授权网页扫码登录php,PHP实现微信开放平台扫码登录源码
- 十天学会PHP_十天学会php教程
- MySQL与PHP的基础与应用专题之索引
- 『抛弃MySQL,转用PHP新时代』(php废弃mysql)
- Linux系统下搜索PHP文件的技巧(linux查找php文件)
- MySQL封装,PHP解决方案(php封装mysql)
- PHP与MySQL结合使用实现关联查询的方法(php关联mysql查询)
- PHP操作MySQL数据库的完整指南(php读写mysql)
- PHP添加MySQL扩展:快速实现数据库连接(php添加mysql扩展)
- 深入理解PHP操作MySQL类库的精髓(php操作mysql类库)
- MSSQL 数据库在PHP中的登录实现(mssql登录php)
- 用Redis和PHP构建强大的商城(商城redis php)
- php的正则表达式完全手册
- php中常用的预定义变量小结
- PHP对MongoDB[NoSQL]数据库的操作
- php获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法
- php实现事件监听与触发的方法
- PHP不用递归遍历目录下所有文件的代码
- Linux+php+apache+oracle环境搭建之CentOS下源码编译安装PHP