Do I use 'foreach' loop to do this?

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
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

Do I use 'foreach' loop to do this?

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

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

Post 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`
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply