Grouping Table Results Dynamicly

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
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Grouping Table Results Dynamicly

Post by romeo »

I have a table with several fields that need to be displayed per record (there are thousands), the table has a "category" column with "data1" data2" "bob3" "imis4" and a dozen other things, is there a way (sql statement) that would grab each of the GROUP BY and display the CATAGORY name befor eit listed off the people in the catagory... i.e

catagory BOY
- Jim
- Farnk
- Sam
- Wally

category Grl
- Sue
- Beth

category DOG
- Ruger
- Fido


and so on and so forth?
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

Do you mean like this?

select * from table where category = 'boy';
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Post by romeo »

No, at least I dont think so...

I want a generic pull that displays all the data but groups it and before it displays a certain group, it displays what the group is

better?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

ORDER BY category, name ( - or whatever col Jim, Beth & Fido are in)

As you loop through the result with mysql_fetch_array or whatever, store the current category in a $var.

In each new loop, check $result['category'] against $var: if they are different, echo $result['category'] . '<br />'; and reset $var = $result['category'].

If they are the same, do nothing.

Initialise $var = ''; before the while loop.
Post Reply