I have come up with the following code, that limits the amounts of data displayed per page to only 2 records. Depending on the number of records on a page, a NEXT and PREVIOUS link is displayed.
How do I add page number links to the code, to enable the user to navigate directly to a particular page, without the need of scanner through endless amounts of page until the required page is displayed?
The image at the following URL, is what I'm after!
http://notoon.free.fr/images/forum/20070103_pagination.gif
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die(mysql_error());
}
mysql_select_db("db", $con);
$perpage = 2;
$start = (isset($_GET['id'])) ? $_GET['id'] : 0;
$TotalRec = mysql_result(mysql_query("SELECT COUNT(*) FROM person"), 0);
$select = "SELECT * FROM person LIMIT $start, $perpage";
$result = mysql_query($select) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['FirstName']." ";
echo $row['LastName'].'<br />';
}
if($start == 0)
{
echo "PREVIOUS";
}
else
{
echo '<a href="pagination.php?id=' . ($start - $perpage) . '">'."PREVIOUS".'</a>';
}
$page = ($_GET['id'] / $perpage) + 1;
$total = ceil($TotalRec / $perpage);
if($start + $perpage >= $TotalRec)
{
echo "NEXT";
}
else
{
echo '<a href="pagination.php?id=' . ($start + $perpage) . '">'."NEXT".'</a>';
}
?>