DAVID_87 Posted August 5, 2009 Share Posted August 5, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/168914-page-not-updating/ Share on other sites More sharing options...
taquitosensei Posted August 5, 2009 Share Posted August 5, 2009 you're overwriting $year with results from your query. while ($row=mysql_fetch_array($result)) { $year=$row["year"]; $options.=" <a href=\"news?id=$year\">".$year."</a> |"; }?> since it's descending and 2006 is the earliest year that's what it ends up as Quote Link to comment https://forums.phpfreaks.com/topic/168914-page-not-updating/#findComment-891293 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.