Displaying results by year
Posted: Sat Feb 03, 2007 2:08 pm
For my website I would like to give the visitors the possibility to view events by year. For example, if a user clicks on the link "2006" he will get a list with all the events we did in 2006.
This is what I already have now:
Now my questions:
This is what I already have now:
Code: Select all
<?php
require_once('../private/open_testdb.inc');
$db = OpenDb();
$query1 = mysql_query("SELECT DISTINCT DATE_FORMAT(FROM_UNIXTIME(date), '%Y') AS year FROM web_press ORDER BY year DESC");
while ($result1 = mysql_fetch_object($query1))
{
$years[] = $result1->year; //building an array of the distinct years, we'll use this array for the pagination
}
//print_r($years); //output array for debugging
echo '<div class="pagination">' . implode(" | ", $years). "</div>\n";
$year=$_GET["year"];
if(!isset($year))
$year = $years[0];
$sql = "SELECT pressId,header,page FROM web_press WHERE page='events' AND DATE_FORMAT(FROM_UNIXTIME(date), '%Y')=$year ORDER BY date DESC";
$result = mysql_query($sql) or die("Invalid query: " . mysql_error());
$max = mysql_num_rows($result);
$i = 0;
while($row = mysql_fetch_array($result))
{
if($i < $max-1) {
echo "\t<div class=\"event\">\n";
$i++;
}
else
echo "\t<div class=\"event noborder_bottom\">\n";
echo "\t<h4 id=\"p".$row["pressId"]."\">".$row["header"]."</h4>\n\t";
include sprintf("./press/%s_%04d.php", $row["page"], $row["pressId"]);
echo "\n\t<div class=\"clearer\"> </div>";
echo "\n\t</div>\n";
}
echo '<div class="pagination">' . implode(" | ", $years). "</div>\n";
?>- I want to add hyperlinks to the years listed in my pagination so peaple can jump from one year to another one. What would be the best way to achieve this (maybe certain PHP functions I can use?)?
- The current year in my pagination should be unlinked, all the other should have a link. How to do this?
- What do you think of my code? Where can things be improved? I'm a newbie but I love to hear how I can write things better
