Sort list into groups & break between different groups

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
toonarmy
Forum Newbie
Posts: 1
Joined: Tue Mar 15, 2005 8:38 am

Sort list into groups & break between different groups

Post 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???
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
Post Reply