Hi All,
I have a news page on my website that is connected to a MySQL database.
At the top of my page I have this code which selects the year:
<?php
if (isset($_GET['id'])) { $year = "$_GET[id]"; } else { $year = date("Y"); }
?>
Further on my page I have a list that users can select the year from in order to view that years news:
<?php
include 'dbc.php';
$sql="SELECT DISTINCT year FROM news ORDER BY year DESC";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$year=$row["year"];
$options.=" <a href=\"news?id=$year\">".$year."</a> |";
}?>
<?php echo $options ?>
Then the main code to display the selected years news.
<?php
include 'dbc.php';
$data ="SELECT *, UNIX_TIMESTAMP(date) AS datestamp FROM news WHERE year = '$year' ORDER BY date DESC";
$result=mysql_query($data);
$color="1";
while($rows=mysql_fetch_array($result)){
$dd = date("jS F Y", strtotime($rows['date']));
if($color==1){
echo "<p class='floatright alignright'><b>".$dd."</b></p>
<h2>".$rows['title']."</h2>
<p>".$rows['article']."</p>";
$color="2";
}else{
echo "<p class='floatright alignright'><b>".$dd."</b></p>
<h2>".$rows['title']."</h2>
<p>".$rows['article']."</p>";
$color="1";
}
}
echo '</table>';
mysql_close();
?>
My problem is that when the original news page is displayed it should show the current years news (2009). Now I know my first snippet of code is working fine because I also have an "echo $year" statement on my page and this shows perfect and changes when the different options are selected. However I have 4 news years in my database (2006, 2007, 2008 and 2009) but the actual news articales do not show as the correct year. Only 2006 is displayed. This happens on each id page that should be created.
Now if I remove the statement that looks for the id at the start and replace it with a static "$year = "year";" snippet the correct years news shows.
Any one see where I am going wrong or can offer any help?