timmah1 Posted December 5, 2008 Share Posted December 5, 2008 I'm not sure how to go about doing this. I have a database of newsletters I'm trying to select all of them, then list them by the year and month. I would like it to look like 2007 jan february march 2008 june july and so forth I've tried numerous different things, but I cannot grasp the concept of doing this Here is the code that I have $archive = mysql_query("SELECT * FROM newsletter GROUP BY year"); while($n = mysql_fetch_array($archive)){ $year = $n['year']; $archived = mysql_query("SELECT * FROM newsletter WHERE year = '$year'"); while($n1 = mysql_fetch_array($archived)){ $month = $n1['month']; echo $year; echo $month; } } Can anybody help me out with this simple thing? Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/135686-solved-how-to-list-items/ Share on other sites More sharing options...
gevans Posted December 5, 2008 Share Posted December 5, 2008 <?php $archive = mysql_query("SELECT DISTINCT year FROM newsletter ORDER BY year DESC"); while($n = mysql_fetch_array($archive)){ echo $year = $n['year']; $archived = mysql_query("SELECT month FROM newsletter WHERE year = '$year' ORDER BY month DESC"); while($n1 = mysql_fetch_array($archived)){ echo $n1['month']; } } ?> This should, though I'm confident there's a better way of doing this with a single query Link to comment https://forums.phpfreaks.com/topic/135686-solved-how-to-list-items/#findComment-706949 Share on other sites More sharing options...
timmah1 Posted December 5, 2008 Author Share Posted December 5, 2008 Thanks for the help All that does is list the items like this: 2009 October 2008 September 2008 October 2008 November 2008 March 2008 June/July 2008 June 2008 June 2008 August 2008 April 2008 April 2007 September 2007 October 2007 November 2007 May 2007 March 2007 June 2007 July 2007 August I'm trying to get all the months that are listed for 2007, to show under 2007, with 2007 just showing once, then 2008, then months, and so forth Link to comment https://forums.phpfreaks.com/topic/135686-solved-how-to-list-items/#findComment-706956 Share on other sites More sharing options...
timmah1 Posted December 5, 2008 Author Share Posted December 5, 2008 Nevermind. I put some of your code wrong. works like I needed it too Thank you Link to comment https://forums.phpfreaks.com/topic/135686-solved-how-to-list-items/#findComment-706958 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.