Home  • Programming • PHP

Basic pagination with PHP and MySQL

Pagination
fielddata typeAttributes
idint(10)auto_increment primary key
namevarchar(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>&nbsp;&nbsp;";		// Current page is not clickable and different from other pages
	} else {
		echo "<a href='?page=$i'>$i</a>&nbsp;&nbsp;";
	}
}
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


I made it as simple as possible for the beginners with no CSS style and JavaScript.
Sir kivabe bujhlen aj ami pagination khujhc...
This post is for Md. Hassan Ali as he requested today.
You are all students in the same school. Smaile
And  I can customize the code and use may website...successfully....
Thank you sir and spacial thanks for my dear friend Hasan
thanku sir, i'm try many time to make it using php but con't do. now i'm very happy. thanku very much sir and thank hassan brother.
Thank u sir..It was needed to me..
nice....thanks
Thanks A lot Sir, It's Really Dynamic & Easy to use
thanks sir
thank you sir
very useful for all
Thank you sir
Copyright © 2025. Powered by Intellect Software Ltd