Basic pagination with PHP and MySQL
field | data type | Attributes |
id | int(10) | auto_increment primary key |
name | varchar(50) | not null |
//----items.php-----//
<?php
$db=new mysqli("localhost","root","","my_db");
$perPage = 10;
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$startAt = $perPage * ($page - 1);
$rs=$db->query("SELECT COUNT(*) FROM user");
list($total)= $rs->fetch_row();
$totalPages = ceil($total / $perPage);
$next=($page+1)<$totalPages?($page+1):$totalPages;
$pre=($page-1)>0?($page-1):1;
$links = "<a href='items.php?page=1'>First</a> ";
$links .= "<a href='items.php?page=$pre'>Previous</a> ";
for ($i = $page-5; $i <= 10+$page-5; $i++) {
if($i>0 && $i<=$totalPages){
$links .= ($i != $page ) ? "<a href='items.php?page=$i'> $i</a> " : "$page ";
}
}
$links.= "<a href='items.php?page=$next'>Next</a> ";
$links.= "<a href='items.php?page=$totalPages'>Last</a> ";
// display page content
$r = $db->query("SELECT * FROM user LIMIT $startAt, $perPage");
while($row=$r->fetch_array()){
echo $row[0].". ".$row[1]."<br/>";
}
// print pagination
echo $links;
echo "<form action='#' method='get'>";
echo "<input type='text' size='1' name='page' />";
echo "<input type='submit' value='go' />";
echo "</form>";
?>
Improved version
demo.php
<?php
require_once("pagination.php");
$db=new mysqli("localhost","root","","my_db");
$perPage = 10;
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$startAt = $perPage * ($page - 1);
$rs=$db->query("SELECT COUNT(*) FROM user_master");
list($total)= $rs->fetch_row();
$totalPages = ceil($total / $perPage);
//display page content
$r = $db->query("SELECT * FROM user_master LIMIT $startAt, $perPage");
while($row=$r->fetch_array()){
echo $row[0].". ".$row[1]."<br/>";
}
//print pagination
echo print_pagination($page,$totalPages);
?>
pagination.php
<?php
function print_pagination($page,$totalPages){
$next=($page+1)<$totalPages?($page+1):$totalPages;
$pre=($page-1)>0?($page-1):1;
$links = "<a href='?page=1'>First</a> ";
$links .= "<a href='?page=$pre'>Previous</a> ";
for ($i = $page-5;$i<=$page+5;$i++) {
if($i>0 && $i<=$totalPages){
$links .= ($i != $page ) ? "<a href='?page=$i'> $i</a> " : "$page ";
}
}
$links.= "<a href='?page=$next'>Next</a> ";
$links.= "<a href='?page=$totalPages'>Last</a> ";
$links.="<form method='get'>";
$links.= "<input type='text' size='1' name='page' />";
$links.="<input type='submit' value='go' />";
$links.= "</form>";
return $links;
}
?>
Another PHP pagination Example
<?
/*
$current_page=$_REQUEST['page'];
$total_pages=120;
*/
grace=5; // 5 pages on the left and 5 pages on the right of current page
$range=$grace*2;
$start = ($current_page - $grace) > 0 ? ($current_page - $grace) : 1;
$end=$start + $range;
if($end > $total_pages){ //make sure $end doesn't go beyond total pages
$end=$total_pages;
$start= ($end - $range) > 0 ? ($end - $range) : 1; //if there is a change in $end, adjust $start again
}
if($start>1){
echo "<a href='?page=1'>1</a> ...";
}
for($i=$start;$i<=$end;$i++){
if($i==$current_page){
echo "<span>$i</span> "; // Current page is not clickable and different from other pages
} else {
echo "<a href='?page=$i'>$i</a> ";
}
}
if($end < $total_pages){
echo "... <a href='?page=$total_pages'>$total_pages</a>"; // If $end is away from total pages, add a link of the last page
}
?>
Comments 15