|
-
Aug 9th, 2007, 10:13 AM
#1
Thread Starter
Addicted Member
Categories and sub-categories, best practice
I have an application which has a database table called Category. Its very simple, with fields id and categoryname.
My client now wants functionality for sub categories. Whats the best way to do this. My initial thought was to keep the same database table but have a 3rd column called ParentID which contains the ID of the parent category.
Is this the best way? Or something else.
Thanks
-
Aug 9th, 2007, 04:13 PM
#2
Fanatic Member
Re: Categories and sub-categories, best practice
FORUM_CATS: cat_id, subcat_id, cat_title
then something like FORUM_POST: POST, CAT_ID, ect., ect.
-
Aug 14th, 2007, 09:58 AM
#3
Re: Categories and sub-categories, best practice
 Originally Posted by MondeoST24
I have an application which has a database table called Category. Its very simple, with fields id and categoryname.
My client now wants functionality for sub categories. Whats the best way to do this. My initial thought was to keep the same database table but have a 3rd column called ParentID which contains the ID of the parent category.
Is this the best way? Or something else.
Thanks
As the previous poster so cryptically explained, you should use a second table for the subcategories that relates back to the category table. That's the normal way to design a relational database.
Your original table remains unchanged:
tblCategory
CategoryID (Long) <== Primary key
Descrip (String)
tblSubCategory
SubCategoryID (Long) <== Primary key
CategoryID (Long) <== Foreign key; relates back to tblCategory
Descrip (String)
-
Aug 14th, 2007, 12:06 PM
#4
Fanatic Member
Re: Categories and sub-categories, best practice
my way is perfectly normal. Many php forums do it the way i do it.
-
Aug 14th, 2007, 12:09 PM
#5
Re: Categories and sub-categories, best practice
 Originally Posted by psychotomus
my way is perfectly normal. Many php forums do it the way i do it.
As far as I can tell, we are both recommending the same approach. I was just offering more detail.
EDIT: On re-reading, I now see that you recommended a flat-file approach. I'm not a fan of that, no matter how many php forumns do it.
Last edited by Ellis Dee; Aug 14th, 2007 at 12:12 PM.
-
Aug 14th, 2007, 03:28 PM
#6
Fanatic Member
Re: Categories and sub-categories, best practice
I didnt recommend a flat-file approach. i recommend 1 table instead of 2.
-
Aug 15th, 2007, 07:42 AM
#7
Re: Categories and sub-categories, best practice
That's what flat-file means.
-
Aug 24th, 2007, 11:39 AM
#8
Re: Categories and sub-categories, best practice
And it's not normalized, which is bad coding practice.
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Sep 15th, 2007, 06:09 PM
#9
Hyperactive Member
Re: Categories and sub-categories, best practice
If you will only have one level of sub category then a second table is best. If you plan to have sub-sub categories and so on I think the parentID idea works better as it will alow and amount of sub levels.
Structure - Keep your html in html files.
Presentation - Keep you css in .css files.
Behaviour - Keep your javascript in .js files.
Try to be unobtrusive.
-
Sep 28th, 2007, 06:36 PM
#10
Re: Categories and sub-categories, best practice
I try to avoid recursive structures. I would use the two entities approach.
HoraShadow
I do like the reward system. If you find that my post was useful, rate it.
-
Sep 28th, 2007, 10:13 PM
#11
Hyperactive Member
Re: Categories and sub-categories, best practice
I don't think I was suggesting you do otherwise. If you want to have an unlimited number of sub categories though you have no choice.
Structure - Keep your html in html files.
Presentation - Keep you css in .css files.
Behaviour - Keep your javascript in .js files.
Try to be unobtrusive.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|