Need case study of categories/subcategories

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
egturnkey
Forum Commoner
Posts: 34
Joined: Sun Jul 26, 2009 7:35 pm

Need case study of categories/subcategories

Post by egturnkey »

Hello friends,

I've coded a simple php script which is

catinput.php > i can add categories
db.sql > categories are stored in database
config.php > connect with database
index.php > shows the categories as menu


and it did works fine.

now getting more complex

Can anyone give me an free open source script has a categories and subcategories

cause i faild to add subcategories into mine, so i'd like to see a code so i can study it how it does works.


thanks in advance it will really helps me in my school to study how it works.


example

i wanna add categories ( X1 ) then add subcategories ( sub X1 )
stored in database
at index it can connect the db and get
X1 ( on click or mousemove ) it shows ( sub X1 )

that what i ment.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Need case study of categories/subcategories

Post by flying_circus »

OsCommerce was the first open source app that I used, and it has this type of functionality. Pretty much any shopping cart, blog, or cms should do something like this.

What I've found, create a table in your database and do something like this:

cat_id | parent_id | cat_name
-------------------------------
1 | 0 | 'X1'
2 | 1 | 'Sub X1'
3 | 2 | 'Sub Sub X1'
4 | 0 | 'X2'
5 | 4 | 'Sub X2'


Assume that any category with a parent_id of 0 is in the menu root. Fetch all the rows from the database into an array and loop through it, testing if children exist.
Post Reply