Page 1 of 1

Sort list into groups & break between different groups

Posted: Tue Mar 15, 2005 8:53 am
by toonarmy
I have a list of links in MySQL, as ID, category, Description & URL,

Q. How can i generate a list in PHP which breaks the list down by category & which enters the category as the header. i.e.

LINKS

Search Pages
Google.com
MSN.com

PHP Sites
devnetwork.com
PHP.net

etc.

I only have around 4 categories so could easily have 4 connections on my page (where category = 1, 2, 3 or 4) & list them that way.

Is this the easiest way to do it? what if i have 40 categories???

Posted: Tue Mar 15, 2005 10:28 am
by pickle
You could do a search for all links and categories, and order it by category. Then, when looping through the result set rows, store the current category. If the category for the current row != the current category, output a header.

You could also just do 4 different queries.

Posted: Tue Mar 15, 2005 10:55 am
by John Cartwright
research GROUP BY or ORDER BY

and as pickle mentioned, loop through your single query, and check to see if the category has changed since the last one, if it has, output the category name