Normally when I group stuff together it is for the obvious use to group together duplicates, but I am making a winners page that shows winners for a bunch of jackpots, some jackpots have 1 winner while other have 3, using GROUP BY date will work if there is only 1 winner but will only show 1 winner when there should be 3.
Here is my code
$result = $db->query("SELECT * FROM r_winners GROUP BY date ORDER BY date DESC");
while($rows = $result->fetch_assoc()){
print"<table class='cashout' style='width:100%;'>
<th colspan='3'>".$rows['jackpot_name']." Winners</th>
<tr>
<td style='width:33%;'>".$rows['winner']."</td><td style='width:33%;'>$".$rows['amount']."</td><td style='width:33%;'>".date("m/d/Y", $rows['date'])."</td></tr>
</tr></table>";
}
It prints out a table like such
---------------------------------------
Daily Jackpot Winners
Username| $5.00 | 12/5/2012 // This table is right, because there is only 1 winner
---------------------------------------
Because there is only 1 winner then GROUP BY really has no affect here
Here is a table for multiple winners
---------------------------------------
Monthly Jackpot Winners
Username | $5.00 | 12/5/2012 // This table is wrong, because there should be 3 winners
---------------------------------------
It needs to look like this
---------------------------------------
Monthly Jackpot Winners
Username | $5.00 | 12/5/2012
Username2 | $2.00 | 12/5/2012
Username3 | $1.00 | 12/5/2012
---------------------------------------
How can I accomplish this?
EDIT: This can maybe explain this better https://gist.github.com/4221167
Change your query to just use
ORDER BYand reformat your loop, placing the table elements outside, and only printing the header once