PHP Pagination with timestamp / timeline display

On February 3, 2010, in How-to, Scripting, by Cubert aka (Cube Dweller)

Pagination with a timestamp as the center value

We provide a way to scroll page to page of data and show the time stamp of when the data was collected as part of the page navigations.

Here we setup the first query and findout how many pages we have.


$sql = "SELECT COUNT(*) FROM table where name = '" . $name . "' and type = '" . $type . "'";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
$rowsperpage = 1;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
$currentpage = (int) $_GET['currentpage'];
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if ($currentpage < 1) {
$currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;

Now we need to make the second query for the actual data so we can place our scroll links and then display our data.


$sql = "SELECT * FROM table where name = '" . $name . "' and type = '" . $type . "' ORDER BY data_time DESC LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
while($row = mysql_fetch_array($result)){
$data = $row['data'];
$data_time = $row['data_time']; // expected in epoch
}
$range = 8;
echo "Reported data";
if ($currentpage > 1) {
echo " << ";
$prevpage = $currentpage - 1;
echo " < ";
}

for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " [";
$rectime = date(DATE_RFC822, $data_time);
echo $rectime;
echo "]
";
} else {
echo " $x ";
}
}
}
if ($currentpage != $totalpages) {
$nextpage = $currentpage + 1;
echo " > ";
echo " >> ";
}

echo "


";
echo $data;
echo "";

 

You should have a database that use epoch time as its time field then set $data_time to that field. You will get a nice output with links to pages forward and previous and the time stamp in local time in the center as the current page you are on. Great for scrolling throught time dlimited data in a time line.

Tagged with:
 

Leave a Reply

*



%d bloggers like this: