<?php
//Global Variable:
$page_name = “http://localhost/pagination.php”;
//REMEMBER TO CONNECT TO DATABASE!
$link = mysql_connect(‘localhost’,'root’,'rootpassword’)
or die(“Error connecting to Database: ” . mysql_error());
mysql_select_db(‘sakila’) or
die(“Error Selecting Database: ” . mysql_error());
//**EDIT TO YOUR TABLE NAME, ETC.
$t = mysql_query(“SELECT count(*) as cnt FROM customer”);
if(!$t){
die(“Error quering the Database: ” . mysql_error());
}
$a = mysql_fetch_object($t);
$total_items = $a->cnt;
echo “Total Number of records in Database: “.$total_items;
echo “<br/>”;
$limit= (isset($_GET["limit"])) ? $_GET["limit"] : 10;
$page= (isset($_GET["page"]))? $_GET["page"] : 1;
echo “This is Page Number: ” . $page . “<br/>”;
echo “Current Limit: “. $limit. “<br/>”;
//Set defaults if: $limit is empty, non numerical,
//less than 10, greater than 50
if((!$limit) || (is_numeric($limit) == false)
|| ($limit < 10) || ($limit > 50)) {
$limit = 10; //default
}
//Set defaults if: $page is empty, non numerical,
//less than zero, greater than total available
if((!$page) || (is_numeric($page) == false)
|| ($page < 0) || ($page > $total_items)) {
$page = 1; //default
}
//calculate total pages
$total_pages = ceil($total_items / $limit);
$set_limit = ($page * $limit) – $limit;
echo “Total Pages: $total_pages <br/>”;
//query: **EDIT TO YOUR TABLE NAME, ETC.
$q = mysql_query(“SELECT * FROM customer LIMIT $set_limit, $limit“);
if(!$q) die(mysql_error());
$no_row = mysql_num_rows($q);
if($no_row == 0) die(“No matches met your criteria.”);
//Results per page:
echo(“<br/>Records Per Page:
<a href=$page_name?limit=10&page=1>
10</a> |
<a href=$page_name?limit=25&page=1>
25</a> |
<a href=$page_name?limit=50&page=1>
50</a><br/>
“);
//show data matching query:
while($object = mysql_fetch_object($q)) {
//print your data here.
echo(“<br/>————————————–<br/>”);
echo(“First Name: “.$object->first_name.”<BR/>”);
echo(“Last Name: “.$object->last_name.”<BR/>”);
echo(“Email: “.$object->email.”<BR/>”);
echo(“————————————–<br/>”);
}
//Results Per Page: Same as earlier one
echo(“<br/>Records Per Page:
<a href=$page_name?limit=10&page=1>
10</a> |
<a href=$page_name?limit=25&page=1>
25</a> |
<a href=$page_name?limit=50&page=1>
50</a><br/>
“);
//prev. page
$prev_page = $page – 1;
if($prev_page >= 1) {
echo(“<b><<</b> <a href=$page_name?limit=$limit&page=$prev_page>
<b>Prev.</b></a>”);
}
//Display middle pages:
for($a = 1; $a <= $total_pages; $a++)
{
if($a == $page) {
echo(“<b> $a</b> | “); //no link
}
else {
echo(“
<a href=$page_name?limit=$limit&page=$a> $a
</a> | “);
}
}
//Next page:
$next_page = $page + 1;
if($next_page <= $total_pages) {
echo(“
<a href=$page_name?limit=$limit
&page=$next_page><b>Next</b></a> > >”);
}
//all done
?> |