Category/Sub-Category/Sub-Sub-Category

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Hyaku
Forum Newbie
Posts: 4
Joined: Tue Sep 12, 2006 12:10 pm

Category/Sub-Category/Sub-Sub-Category

Post by Hyaku »

Hi!
I want to create an image gallery, that may include more then 4 sub-categories inside each other. For example:
Arhitecture->Maryland->Baltimore->Downtown->Old Houses
What would be a good way, how to save it in the database? do I need to have seperated table for each sub-category or should I use one table? any other suggestions? Thanks!
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

You don't need separate table to do this. You can achieve this with the help of single table.

Make the table with the following attributes :

Code: Select all

cat_id
cat_name 
is_parent
is_parent if 0 then it is root. And if it is not 0 and contains a parent node id which will becomes a child of parent node with that value.

I guess that makes a concept of what you are trying to archive.
Hyaku
Forum Newbie
Posts: 4
Joined: Tue Sep 12, 2006 12:10 pm

Post by Hyaku »

Thanks alot man!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That appears to be a hierarchical structure.

There are two resources we have listed in our.. uh Useful Resources threads that talk directly of hierarchical structures. They may be a good read.
Post Reply