Jump to content

[SOLVED] How to list items


timmah1

Recommended Posts

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

<?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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.