jcjst21 Posted July 17, 2012 Share Posted July 17, 2012 I have been searching the forums to find how to group a table by ID and list each record associated with it. All I have been finding is how to group by and using it to do a sum or average or count. What I want to do is, Group By ID, and list each associated row... Example: Group A: -Person A -Person B Group B -Person C -Person D I tried multiple variations of a while loop without success... any advice? Quote Link to comment https://forums.phpfreaks.com/topic/265847-creating-a-report-with-group-by-id/ Share on other sites More sharing options...
ManiacDan Posted July 17, 2012 Share Posted July 17, 2012 You could use the group_concat function. You could also simply order by the group ID, and only print it when it changes. Quote Link to comment https://forums.phpfreaks.com/topic/265847-creating-a-report-with-group-by-id/#findComment-1362239 Share on other sites More sharing options...
jcjst21 Posted July 17, 2012 Author Share Posted July 17, 2012 I'll try group concat but also, how could i indicate when the groupID changes in the while loop? Quote Link to comment https://forums.phpfreaks.com/topic/265847-creating-a-report-with-group-by-id/#findComment-1362248 Share on other sites More sharing options...
ManiacDan Posted July 17, 2012 Share Posted July 17, 2012 Keep $oldGroupName in a variable. Check it with every iteration of the loop. Quote Link to comment https://forums.phpfreaks.com/topic/265847-creating-a-report-with-group-by-id/#findComment-1362265 Share on other sites More sharing options...
jcjst21 Posted July 17, 2012 Author Share Posted July 17, 2012 If i did it this way where it only prints if the group id changes then how can i make a header for the group name? Quote Link to comment https://forums.phpfreaks.com/topic/265847-creating-a-report-with-group-by-id/#findComment-1362268 Share on other sites More sharing options...
Barand Posted July 17, 2012 Share Posted July 17, 2012 $prev = null; while ( whatever ) { if ($current != $prev) { echo "Heading"; $prev = $current; } echo "Details"; } Quote Link to comment https://forums.phpfreaks.com/topic/265847-creating-a-report-with-group-by-id/#findComment-1362275 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.