View Full Version : rookie in need of help with archive code

05-06-2009, 07:18 PM
Hi, I have a code someone made me a while ago, it works fine on the site I was making back then, but for some reason it doesn't work on the one I'm doing now.

Its main function is to list all months from now and then when you click a month, show all entries in a database from that month.

For some reason it is just showing "May 2009" twice and when I click on that it's bringing up nothing (there are entries made in May 2009)

//get desired month and year
if (isset($_GET['month']))
$desiredMonth = $_GET['month'];
} else {
$desiredMonth = '';
if (isset($_GET['year']))
$desiredYear = $_GET['year'];
} else {
$desiredYear = '';

if($desiredMonth && $desiredYear)
$db=mysql_connect("*****","*****","*****") or die ('cant connect');
mysql_select_db("*****",$db) or die ("cant change");
$news=mysql_query("SELECT * FROM news WHERE 1 AND date LIKE '%".$desiredMonth."%".$desiredYear."%' ORDER BY id DESC") or die ("cant get table");


echo "<h3>$title</h3>
} else {

$time1 = strtotime('2009-05-06');
$time2 = strtotime("now");
$my = date('mY', $time2);

$months = array(date('F Y', $time1));

while($time1 < $time2) {
$time1 = strtotime(date('Y-m-d', $time1).' +1 month');
if(date('mY', $time1) != $my && ($time1 < $time2))
$months[] = date('F Y', $time1);

$months[] = date('F Y', $time2);

foreach ($months as $value){
echo "<a href='archive.php?month=".substr($value, 0, 3)."&year=".substr($value, -4)."'>".$value."</a>";

it could be something really simple but I dont actually know php and I cant ask the person who made it originally, I would be very grateful for any help


05-06-2009, 07:32 PM
This script is expecting a query string containing a month and year value. Are you sending a query string to this script? (A query string is the stuff in a URL that begins with a question mark; for example the URL for the page I'm on right now is "http://codingforums.com/newreply.php?do=newreply&noquote=1&p=813657", so the query string is "?do=newreply&noquote=1&p=813657".)

05-06-2009, 07:54 PM
I'm sorry, I don't really understand this stuff at all, really I'm normally just a graphics person. I thought that the code at the bottom generated links which include the query string?

the link to May is being generated by the page, but it is being generated twice (no idea why)

the link points to archive.php?month=May&year=2009

but the resulting page is blank (despite there being entries in the database dated 2009-05-06)

05-06-2009, 08:09 PM
Ok... Well... I looked at the script a little more closely and it would seem the dates in your table are stored entirely the wrong way, which probably explains why you're not getting any results back. Without changing the script you'll have to store your dates in a ""MM...YYYY"" format. The query is really horrible though and should be changed (along with the way you are storing dates).

05-06-2009, 08:13 PM
Well erm.. the original code is really old (as in, years) and I didn't write it. Thanks anyway at least I know where the problem lies.