Forum Moderators: open

Message Too Old, No Replies

MySQL Grouping Categories

getting categories with parents grouped

         

fatwombat

2:03 am on Aug 30, 2009 (gmt 0)

10+ Year Member



Hey everyone,

Bit of a newbie here trying to work something out.

If you can give me direction on what to do, not necessarily code that would be a massive help.

I have a table called `categories`. Not its setup like so:

ID int(3)
category varchar(50)
parent int(3)

Now for instance I have a category called `CD` and the id will be 1.

I then have categories called `Singles` and `Albums` with IDs of 2 and 3 respectively and parent equals 1 and 1 respectively.

`categories`
ID category parent
1 CD 0
2 Singles 1
3 Albums 1

Now I want to get those out in the below format:

Categories:
CD
->Singles
->Albums
DVD
->Musical
->Comedy

I just added DVD so you can see what other categories could be added to such a table to explain how the output should be done.

Cheers for the help :)

Fatwombat.

eelixduppy

8:17 pm on Aug 31, 2009 (gmt 0)



Order by the category name, which will "group them", then when you output them to the browser you have to check to see when the category name changes. Are you using PHP?

fatwombat

8:58 am on Sep 1, 2009 (gmt 0)

10+ Year Member



Yeah I am using PHP. Ordering by category name will just put them in alphabetical order won't it?