tags:

views:

30

answers:

1

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/
A: 

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>";
}
isola009
Thanks I fixed some of your code to make it work with mine.
monkeyEYES
That's perfect!!! ;-)
isola009