ssailer Posted September 10, 2009 Share Posted September 10, 2009 I have a query which is using a date function to format an article date by month/year. My query is: SELECT `ID`, `title`, `content`, `articleDate`, `pdfDoc`, date_format(articleDate, '%M %Y') as monthYear from articles ORDER BY articleDate DESC"; The query is working fine, but I cannot figure out how to output it on screen grouped by month/year. I need it to look something like the following: August 2009 - Article Title - Article Title September 2009 - Article Title - Article Title I am a ColdFusion programmer who is trying to learn PHP and this is beyond my skill level right now... Thank you! Link to comment https://forums.phpfreaks.com/topic/173791-i-need-to-outputgroup-a-query-by-monthyear/ Share on other sites More sharing options...
ignace Posted September 10, 2009 Share Posted September 10, 2009 SELECT `ID`, `title`, `content`, `articleDate`, `pdfDoc`, date_format(articleDate, '%M %Y') as monthYear from articles ORDER BY monthYear, articleDate DESC $current = null; while ($row = mysql_fetch_assoc($result)) { if ($current !== $row['monthYear']) { $current = $row['monthYear']; echo '<dt>', $row['monthYear'], '</dt>'; } echo '<dd>', $row['title'], '</dd>'; } I modified your query so that it orders by monthYear first and articleDate afterwards (thus all monthYear will be grouped together). Afterwards I added a $current which will hold the current monthYear (August 2009 first, September 2009 second, ..) and print it once. Link to comment https://forums.phpfreaks.com/topic/173791-i-need-to-outputgroup-a-query-by-monthyear/#findComment-916133 Share on other sites More sharing options...
ssailer Posted September 10, 2009 Author Share Posted September 10, 2009 Ignace, thank you. It worked perfectly! I swear - I've been having trouble finding good working examples of some fairly common things with PHP Link to comment https://forums.phpfreaks.com/topic/173791-i-need-to-outputgroup-a-query-by-monthyear/#findComment-916233 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.