hutch Posted September 10, 2008 Share Posted September 10, 2008 Can any one tell me how to take data extracted from mySQL and make it display results that have the same date grouped together? Typically I used a "while ($row = mysql_fetch_array($data))" to display data but can't figure out the easiest way to do this. Example: Sept. 10 2008 ----------------------- row1 row4 Sept. 11 2008 ----------------------- row2 row3 Sept. 18 2008 ----------------------- row5 row6 row7 row8 etc.. Link to comment https://forums.phpfreaks.com/topic/123690-solved-php-mysql-display-results-grouped-by-date/ Share on other sites More sharing options...
php_dave Posted September 10, 2008 Share Posted September 10, 2008 Hi, Pull your results sorting by date order so adding a order by <date_field> DESC to your query if you dont have it already. Then you can compare the last date row against the current if its the same just print the row if its different print the header first. Example: $sql = "SELECT * from TABLE order by <date_field> DESC" $old_date = False; $result = mysql_query($sql); while ($row = dbarray($result)) { if ($old_date != $row['date_field']) { echo $row['date_field']."<BR />"; } echo $row."<BR />"; $old_date = $row['date_field']; } Not Tested but should work. Dave Link to comment https://forums.phpfreaks.com/topic/123690-solved-php-mysql-display-results-grouped-by-date/#findComment-638713 Share on other sites More sharing options...
hutch Posted September 11, 2008 Author Share Posted September 11, 2008 Worked like a charm and it was so easy! Thanks so much! Link to comment https://forums.phpfreaks.com/topic/123690-solved-php-mysql-display-results-grouped-by-date/#findComment-638824 Share on other sites More sharing options...
hutch Posted September 11, 2008 Author Share Posted September 11, 2008 One more stupid question.. My date field in mySQL is setup exactly as that, a "date" column. When my results are being output it shows as: 2008-09-10 .. 2008-09-11 .. 2008-09-12 .. 2008-09-21 .. 2008-09-31 .. 2008-09-01 The lower dates (01, 02, 03, etc) are being displayed at the bottom.. What have I done wrong? mySQL SELECT * FROM table ORDER BY startDate PHP while ($row = mysql_fetch_array($result)) { if ($old_date != $row['StartDate']) { echo $row['StartDate'].'<br />'; } echo $row['Title'].'<br />'; $old_date = $row['StartDate']; } Link to comment https://forums.phpfreaks.com/topic/123690-solved-php-mysql-display-results-grouped-by-date/#findComment-638850 Share on other sites More sharing options...
Zane Posted September 11, 2008 Share Posted September 11, 2008 try ORDER BY UNIX_TIMESTAMP(startDate) Link to comment https://forums.phpfreaks.com/topic/123690-solved-php-mysql-display-results-grouped-by-date/#findComment-638852 Share on other sites More sharing options...
hutch Posted September 11, 2008 Author Share Posted September 11, 2008 bingo, thank you very much Link to comment https://forums.phpfreaks.com/topic/123690-solved-php-mysql-display-results-grouped-by-date/#findComment-638882 Share on other sites More sharing options...
Zane Posted September 11, 2008 Share Posted September 11, 2008 your welcome remember to mark your posts as solved bottom left area Link to comment https://forums.phpfreaks.com/topic/123690-solved-php-mysql-display-results-grouped-by-date/#findComment-638890 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.