Jump to content

creating flexible categories and unlimited subcategories


johnmerlino

Recommended Posts

Hey all,

 

If I want to have a category which can have subcategories, which in turn can have its own subcategories or belong to the parent category, and the subcategories can have more than one parent categories, is the most effective way to design this is to create a categories table and categories_join table where the categories table has a foreign key categories_join_id linking it to the categories_join table and the categories_join table has a field called parent_id which associates with the primary key of the categories table, therefore allowing me to have multiple subcategories to one category:

 

categories_join table

id parent_id

1 2

1 3

 

categories table

id categories_join_id

1 1

2 2

3 3

 

So from the example above, the first category has two parents, category 2 and category 3.

 

As an alternative option, in this post:

 

http://stackoverflow.com/questions/5384183/database-design-question-categories-subcategories

 

The second answer down mentions  to use recursive programming? But he doesn't give an example of what he means. Is he saying you have a function call itself passing it parameters as to what the parent should be:

 

function getParent($category,$parents = array()){

}

 

Thanks for response.

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.