Page 1 of 1

Do I use 'foreach' loop to do this?

Posted: Sat Sep 13, 2008 8:54 pm
by michlcamp
I know I've seen other posts about this - thanks for your time.

I've got a data table for user comments and want to produce a report by 'category'.

example table:
category | name | date | comment
Cars| bob jones | 09-13-08 | this is my comment today
Boats | dave smith | 09-12-08 | wish I had a boat
Trucks | tom carpenter | 09-12-08 | need a big truck

I need a query that produces a report by 'category', with category heading, then items listed below it.
ex:
Cars
bob jones
Boats
dave smith
Trucks
tom carpenter


Do I need a 'FOREACH' loop to pull the categories as UNIQUE, then a WHILE loop to pull data 'WHERE category = $category'

looking 'round the web for answers, but always get the best ones here.
thanks.

Re: Do I use 'foreach' loop to do this?

Posted: Sat Sep 13, 2008 9:14 pm
by s.dot
Nope, just a group by clause in your SQL.

Assuming MySQL:

Code: Select all

SELECT `name`, `category` FROM `table` GROUP BY `category`