group mysql results by year, list months

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

group mysql results by year, list months

Post by invisibled »

hey everybody,

i am trying to build a query and just can't quite get my head around it. I need to display press releases from a database, but, They need to be grouped by year, then display all the months. The data will be one press release per month, the date will be a datetime column called "created_at" and here is a visual example of how i want the output. e.g:

2010
january
february
march
etc..

2009
january
february
etc..

any help would be greatly appreciated!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: group mysql results by year, list months

Post by Christopher »

ORDER BY the date descending. Then as you loop through the results -- when the year or month changes -- insert the proper heading.
(#10850)
Post Reply