如何使用 PHP & 显示类别和无限子类别MySQL? [英] How to display categories and end-less sub categories using PHP & MySQL?
问题描述
我想知道使用 PHP & 显示 100 个类别的最佳方式是什么?MySQL?
例如,为每个类别或子类别创建一个文件夹是不明智的.
我将如何创建我的 php 页面来显示类别和无限子类别?
如果有多个子类别,我的网址会是什么样子?
http://www.example.com/a/b/c/d/e
它看起来像什么吗?
http://www.example.com/cat?
我的数据库会是什么样子?
这是我的 MySQL 数据库的样子,我应该添加或删除什么?
创建表类别(id INT UNSIGNED NOT NULL AUTO_INCREMENT,parent_id INT UNSIGNED NOT NULL DEFAULT 0,类别文本非空,网址文本不为空,主键 (id),索引父 (parent_id));
一个好的教程或详细的例子会很有帮助.
抱歉所有问题.
输出数据.
1. 行政支持2. 艺术与艺术娱乐1. 娱乐&主题公园2. 艺术鉴赏3. 艺术家1. A1. a12.a22.乙3. C4. D3. 汽车&运输4. 网络管理5. 服务器管理6. 网页设计1.CSS2. HTML
数据库存储.
id parent_id 类别网址1 0 艺术 &娱乐文娱/2 1 娱乐 &主题公园游乐园和主题公园/3 1 艺术鉴赏 art-appreciation/4 1 艺术家艺术家/5 4 A 艺术家/a/6 4 B 艺术家/b/7 4 C 艺术家/c/8 4D 艺术家/d/9 0 汽车 &交通运输 汽车与运输/10 5 a1 艺术家/a/a1/11 5 a2 艺术家/a/a2/12 0 网页设计 web-design/13 12 HTML 网页设计/html/14 12 CSS 网页设计/css/15 0 网络管理 network-administration/16 0 服务器管理 server-administration/17 0 行政支持 行政支持/
我希望有效:
function category_tree( $parent = 0, $parent_url = "www.example.com/" ){echo "";$sql = sprintf("SELECT id, category, url FROM Categories where parent_id = %d order by category asc", $parent);$r = mysql_query( $sql );while( $rs = mysql_fetch_assoc( $r ) ){$url = $parent_url .$rs['url'];$item = sprintf("<li> <a href = '%s' >%s</a> </li>", $url , $rs['category']);回声 $item;category_tree( $rs['id'], $url );}mysql_free_result( $r );echo "</ol>";}
I was wondering what is the best way to display 100's of categories using PHP & MySQL?
For example, it would not be wise to create a folder for each category or sub category.
And how would I create my php page to display categories and end-less sub categories?
And how would my url look like exactly for example with multiple sub categories?
http://www.example.com/a/b/c/d/e
Would it look like something like?
http://www.example.com/cat?
How would my database look like?
Here is how my MySQL databse looks like what should i add or remove?
CREATE TABLE categories (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
parent_id INT UNSIGNED NOT NULL DEFAULT 0,
category TEXT NOT NULL,
url TEXT NOT NULL,
PRIMARY KEY (id),
INDEX parent (parent_id)
);
a good tutorial or a detail example would help out a lot.
Sorry for all the question.
output data.
1. Administrative Support
2. Arts & Entertainment
1. Amusement & Theme Parks
2. Art Appreciation
3. Artists
1. A
1. a1
2. a2
2. B
3. C
4. D
3. Automotive & Transportation
4. Network Administration
5. Server Administration
6. Web Design
1. CSS
2. HTML
database storage.
id parent_id category url
1 0 Arts & Entertainment arts-and-entertainment/
2 1 Amusement & Theme Parks amusement-and-theme-parks/
3 1 Art Appreciation art-appreciation/
4 1 Artists artists/
5 4 A artists/a/
6 4 B artists/b/
7 4 C artists/c/
8 4 D artists/d/
9 0 Automotive & Transportation automotive-and-transportation/
10 5 a1 artisits/a/a1/
11 5 a2 artisits/a/a2/
12 0 Web Design web-design/
13 12 HTML web-design/html/
14 12 CSS web-design/css/
15 0 Network Administration network-administration/
16 0 Server Administration server-administration/
17 0 Administrative Support administrative-support/
I hope works:
function category_tree( $parent = 0, $parent_url = "www.example.com/" ){
echo "<ol>";
$sql = sprintf("SELECT id, category, url FROM categories where parent_id = %d order by category asc", $parent);
$r = mysql_query( $sql );
while( $rs = mysql_fetch_assoc( $r ) ){
$url = $parent_url . $rs['url'];
$item = sprintf("<li> <a href = '%s' >%s</a> </li>", $url , $rs['category']);
echo $item;
category_tree( $rs['id'], $url );
}
mysql_free_result( $r );
echo "</ol>";
}
这篇关于如何使用 PHP & 显示类别和无限子类别MySQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!