There are two fields of thought, one is to toss the queries at the database that give you the data in a format that's almost ready to display, then you just loop through them, the other is to use one big query that gets you all the data, but with repeats in it, and then process that with PHP.
I tend towards the second. i.e. if I get a table returning data like:
group | name
1 | Scott
1 | Dan
1 | Steve
2 | Mark
2 | Jan
2 | Laurie
Then I'd just loop through the whole thing, checking to see if the group had changed and print a new heading if it had.