Results 1 to 11 of 11

Thread: Categories and sub-categories, best practice

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    180

    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

  2. #2
    Fanatic Member
    Join Date
    Aug 2003
    Posts
    601

    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.

  3. #3
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Categories and sub-categories, best practice

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

  4. #4
    Fanatic Member
    Join Date
    Aug 2003
    Posts
    601

    Re: Categories and sub-categories, best practice

    my way is perfectly normal. Many php forums do it the way i do it.

  5. #5
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Categories and sub-categories, best practice

    Quote 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.

  6. #6
    Fanatic Member
    Join Date
    Aug 2003
    Posts
    601

    Re: Categories and sub-categories, best practice

    I didnt recommend a flat-file approach. i recommend 1 table instead of 2.

  7. #7
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Categories and sub-categories, best practice

    That's what flat-file means.

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    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

  9. #9
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262

    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.

  10. #10
    Fanatic Member
    Join Date
    May 2005
    Posts
    608

    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.

  11. #11
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262

    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
  •  



Click Here to Expand Forum to Full Width