You can use just the one table with a parent for each sub-category:

alter table category add parent int(11);

id    parent    name
1    NULL    fruit
2    1        apple
3    1        banana
4    1        coconut

Now, suppose you need the sub-categories of the record with ID #1
(IE, all the fruits)

select id, name from cateogory where parent = 1

You could even generate *ALL* the relationships with a "SELF-JOIN"
This is where you kinda "fake out" SQL to think of the table from two
view-points at once:

Your query would be:
select,,, sub,name
from category as super, category as sub
where sub.parent =
order by,

WARNING [EMAIL PROTECTED] address is an endangered species -- Use
Wanna help me out?  Like Music?  Buy a CD:
Volunteer a little time:
----- Original Message -----
From: Jeff Lewis <[EMAIL PROTECTED]>
Newsgroups: php.general
Sent: Wednesday, September 12, 2001 9:07 AM
Subject: Question about how to handle something....

We have categories set up and I was using PHP to make searches on this.  It
was working nicely and the categories were auto incremented as new ones were

Now we're adding sub categories and the categories are all labelled the same

When I load one page I use PHP to generate a drop down box of the categories
and this is easy but now I need to do sub categries as well...  Anyone have
a way to do this in mind?  Like how can I do both.

Should I create a new table to hold these sub categories and enter them in
but somehow link them to the main categories?  So when building thedrop down
boxes it wouldbe like:

Computers - Programming (value of this would be something like 1/8) I would
then split the two values to do searches?


PHP General Mailing List (
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to