corillo181 Posted August 13, 2007 Share Posted August 13, 2007 so i got some data on a mysql table that i want to group by date i was thinking of SELECT song_name,DATE_FORMAT(date, '%m - %d - %y') as dates FROM tra_artist_song GROUP BY DATE how do make every data under the same date appear under that date Quote Link to comment https://forums.phpfreaks.com/topic/64601-solved-group-by-date/ Share on other sites More sharing options...
corillo181 Posted August 13, 2007 Author Share Posted August 13, 2007 i came up with this, if any one know a better way please let me know. function newSongs($limit){ $select="SELECT DATE_FORMAT(date,'%a, %M %d, %y') as dates FROM tra_artist_song GROUP BY dates LIMIT $limit"; $query=mysql_query($select)or die(mysql_error()); while($name=mysql_fetch_array($query)){ $dates=$name['dates']; echo '<h3>'.$dates.'</h3>'; $select2="SELECT song_name FROM tra_artist_song WHERE DATE_FORMAT(date,'%a, %M %d, %y')='$dates'"; $query2=mysql_query($select2)or die(mysql_error()); while($songNames=mysql_fetch_array($query2)){ echo $songNames['song_name'].'<br />'; } } } Quote Link to comment https://forums.phpfreaks.com/topic/64601-solved-group-by-date/#findComment-322175 Share on other sites More sharing options...
Barand Posted August 13, 2007 Share Posted August 13, 2007 try <?php $select="SELECT DATE_FORMAT(date,'%a, %M %d, %y') as dates, song_name FROM tra_artist_song ORDER BY dates LIMIT $limit"; $query=mysql_query($select)or die(mysql_error()); $prevdate = ''; while (list($dt, $song) = mysql_fetch_row($query)) { if ($prevdate != $dt) { echo "<h3>$dt</h3>"; // output date when its value changes $prevdate = $dt ; } echo "$song<br/>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/64601-solved-group-by-date/#findComment-322214 Share on other sites More sharing options...
corillo181 Posted August 13, 2007 Author Share Posted August 13, 2007 works the same, but lest code thankx. Quote Link to comment https://forums.phpfreaks.com/topic/64601-solved-group-by-date/#findComment-322763 Share on other sites More sharing options...
Barand Posted August 13, 2007 Share Posted August 13, 2007 and less query traffic. You could also do this <?php $select="SELECT DATE_FORMAT(date,'%a, %M %d, %y') as dates, GROUP_CONCAT (song_name SEPARATOR '<br/>') FROM tra_artist_song GROUP BY dates LIMIT $limit"; $query=mysql_query($select)or die(mysql_error()); while (list($dt, $songs) = mysql_fetch_row($query)) { echo "<h3>$date</h3>$songs"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/64601-solved-group-by-date/#findComment-322777 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.