Jump to content

categories and sub categories


coolphpdude

Recommended Posts

hi there,

 

Im creating a school database system and i will use a number of categories, sub categories and sub sub categories. wot i am wondering is, what is the best way to do this? Should i have a table of main gategories then a table for each sub category like the following... i'll use a car database as an example...

 

[b]Main_cat_table[/b]
Ford
Vauxhall
Vauxwagon
etc...


                      [b]ford_sub_cat_table[/b]
                      Focus
                      Mondeo
                      Fiesta
                      etc...

                      [b]vauxhall_sub_cat_table[/b]
                      astra
                      corsa
                      vectra
                      etc...

                      [b]vauxwagon_sub_cat_table[/b]
                      golf
                      polo
                      passatt
                      etc...

                                                                 [b]golf_sub_cat_table[/b]
                                                                 tdi
                                                                 gt tdi
                                                                 gti

 

Is this the best way to do it or am i making things difficult for me by making loads of tables???

 

 

Link to comment
Share on other sites

If you are dealing with a finite number of sub-categories such as your example above, then separate tables make the most sense. Especially since each "category" may need different supporting information.

 

However, if your subcategories will be infinite (such as a geneology solution) you will use one table. If this is the route you need to go, look for a tutorial - her is one: http://www.codeassembly.com/How-to-display-infinite-depth-expandable-categories-using-php-and-javascript/

 

Link to comment
Share on other sites

ooh that luks gud!!! it looks like exactly what i need. could you give me some further advice...

 

if i use the method on that page id have something along the following

 


Id     Cat             Parent
1      ford            0
2      vauxhall       0
3      vauxwagon        0
4      focus          1
5      fiesta          1
6      mondeo       1
7      astra          2
8      corsa          2
9      vectra        2
10    golf            3
11    polo            3
12    passatt        3 
13    st               4
14    tdi               4
15    tdci             4

 

is that right?? so basically i am saying that id 13,14 and 15 (st, tdi, tdci) belong to id 4 (focus)?

 

So would you recommend this way of doing things or do you think each category should have its own table?

Link to comment
Share on other sites

is that right?? so basically i am saying that id 13,14 and 15 (st, tdi, tdci) belong to id 4 (focus)?

Yes.

 

So would you recommend this way of doing things or do you think each category should have its own table?

I think I already answered that. If you have a finite number of categories like you showed in your example, then three separate tables would be better (e.g. Make, Model, Sub-Model?). Because in that example each category is a unique type of entity (make = manufacturer of the car, model = type of car, etc.)

 

however, with something like a geneology database youhave many parent/child combinations similar to many categories, but each entity is stil the same type of object (a person). Since you did not explain the type of records you plan to record I can't really provide any concrete advice.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.