如何使用PHP显示类别和无限子类别; MySQL的?

时间:2010-08-14 19:51:52

标签: php mysql

我想知道使用PHP& amp;显示100种类别的最佳方式是什么? MySQL的?

例如,为每个类别或子类别创建一个文件夹是不明智的。

我如何创建我的php页面来显示类别和无末尾子类别?

我的网址如何与多个子类别完全相同?

http://www.example.com/a/b/c/d/e

它看起来像什么?

http://www.example.com/cat?

我的数据库怎么样?

以下是我的MySQL数据库看起来应该添加或删除的内容?

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)
);

一个好的教程或一个详细的例子会有很多帮助。

对不起所有问题。

输出数据。

   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

数据库存储。

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/

1 个答案:

答案 0 :(得分:0)

我希望有效:

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>";
}