程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php 無限分類程序

php 無限分類程序

編輯:關於PHP編程

以前也經常介紹關於無限分類這個問題,大多數情況我們都是利用php遞歸來實現,今天介紹的這篇無限分類不需要遞歸哦,方法很簡單有需要的朋友可以參考一下。

 

 代碼如下 復制代碼

//////////////
//////無限分類的數據庫設計及樣例
//////////////
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>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved