Previous and Next page navigation links in Pagination Code
Posted: Mon Jul 30, 2007 7:34 am
Hello All,
I have written a code for pagination in php and mysql but i need to display previous and next page links as i have records exceeding 500.
And also i would like you all to help me optimize the code as i am just a beginner in PHP.
Any help would be highly appreciated.
Thanks and Regards,
Dream2rule
I have written a code for pagination in php and mysql but i need to display previous and next page links as i have records exceeding 500.
And also i would like you all to help me optimize the code as i am just a beginner in PHP.
Code: Select all
<?php
//connecting to the database
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("live_search") or die(mysql_error());
//retrieving query string values
$lower_bound = $_GET['lb'];
$upper_bound = $_GET['ub'];
$page_no = $_GET['page'];
if((isset($upper_bound)) && (isset($lower_bound)))
{
$bound = ($upper_bound - $lower_bound) + 1;
//echo "Bound - ".$bound."<br>";
$sql_data = "SELECT * FROM ajax_live_search ORDER BY area LIMIT $lower_bound,$bound";
//echo $sql_data."<br>";
$rs_data = mysql_query($sql_data);
while($row = mysql_fetch_array($rs_data))
{
echo "<table cellpadding='0' cellspacing='0' border='0' width='50%'><tr>$row[1]</tr></table>";
}
}
//retrieving the total number of records from db
$sql = "SELECT area FROM ajax_live_search ORDER BY area";
$rs = mysql_query($sql);
$total_records = mysql_num_rows($rs);
$limit_per_page = 20;
$number_of_pages = ceil($total_records/$limit_per_page);
//calculating the lower bound and upper bound for the page
$lb=0; //initial value
for($i=1; $i <= $number_of_pages; $i++)
{
$ub = $lb + ($limit_per_page - 1);
if($i < $number_of_pages)
{
//if viewing the current page, link does not appear else the link to that page appears
if($i == $page_no)
echo " ".$i." |";
else
echo " "."<a href=pagination.php?page=$i&lb=$lb&ub=$ub>".$i."</a>"." |";
}
else
{
if($i == $page_no)
echo " ".$i." ";
else
echo " "."<a href=pagination.php?page=$i&lb=$lb&ub=$total_records>".$i."</a>"." ";
}
$lb = $ub + 1;
}
?>Thanks and Regards,
Dream2rule