How to make PHP Pagination With Example and Demo?

In this dynamic pagination calculation in php demo example We are going to step by step learn how to create paginations with next and previous or Advance Level feature using PHP and MySQLi best Example with Full Source code download.

PHP Code : simple paginations in php

<?php  
$servername='localhost'
$username='root';
$password='';
$dbname = "gandhi_shops";
$conn=mysqli_connect($servername,$username,$password,"$dbname");
if(!$conn){
die('Could not Connect My Sql:' .mysql_error());
}
$limit = 10;  
if (isset($_GET["page"])) {
	$page  = $_GET["page"]; 
	} 
	else{ 
	$page=1;
	};  
$start_from = ($page-1) * $limit;  
$productsData = mysqli_query($conn,"SELECT * FROM user_table ORDER BY userid ASC LIMIT $start_from, $limit");
?>

Also Read: php paginations example with code download

HTML Code with Paginations Link : paginations in php

<!DOCTYPE html>
<html lang="en">
<head>
  <title>paginations in php demo - www.pakainfo.com</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>
<body>
<table class="table table-bordered table-striped">  
<thead>  
<tr>  
<th>productid</th>  
<th>Product name</th>
<th>Product Desc</th>
 <th>Category name</th>
<th>Product Code</th> 
</tr>  
<thead>  
<tbody>  
<?php  
while ($row = mysqli_fetch_array($productsData)) {  
?>  
            <tr>  
				<td><?php echo $row["productid"]; ?></td>  
				<td><?php echo $row["product_name"]; ?></td>
				<td><?php echo $row["product_desc"]; ?></td>
				<td><?php echo $row["category"]; ?></td>
				<td><?php echo $row["pcode"]; ?></td>			
            </tr>  
<?php  
};  
?>  
</tbody>  
</table>  
<?php  

$productsData_db = mysqli_query($conn,"SELECT COUNT(id) FROM product_table"); 
$row_db = mysqli_fetch_row($productsData_db);  
$total_products = $row_db[0];  
$all_pages_count = ceil($total_products / $limit); 
/* echo  $all_pages_count; */
$htmlbindPageinationUrl = "<ul class='pagination'>";  
for ($i=1; $i<=$all_pages_count; $i++) {
              $htmlbindPageinationUrl .= "<li class='page-item'><a class='page-link' href='pagination.php?page=".$i."'>".$i."</a></li>";	
}
echo $htmlbindPageinationUrl . "</ul>";  
?>

</body>
</html>

pagination in php demo with Complete Example

index.php

<?php  
$servername='localhost'
$username='root';
$password='';
$dbname = "gandhi_shops";
$conn=mysqli_connect($servername,$username,$password,"$dbname");
if(!$conn){
die('Sorry, Could not Connect My Sql:' .mysql_error());
}
$limit = 10;  
if (isset($_GET["page"])) {
	$page  = $_GET["page"]; 
	} 
	else{ 
	$page=1;
	};  
$start_from = ($page-1) * $limit;  
$productsData = mysqli_query($conn,"SELECT * FROM product_table ORDER BY productid ASC LIMIT $start_from, $limit");
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>paginations in php demo - www.pakainfo.com</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>
<body>
<table class="table table-bordered table-striped">  
<thead>  
<tr>  
<th>productid</th>  
<th>Product name</th>
<th>Product Desc</th>
 <th>Category</th>
<th>Product Code</th> 
</tr>  
<thead>  
<tbody>  
<?php  
while ($row = mysqli_fetch_array($productsData)) {  
?>  
            <tr>  
				<td><?php echo $row["productid"]; ?></td>  
				<td><?php echo $row["product_name"]; ?></td>
				<td><?php echo $row["product_desc"]; ?></td>
				<td><?php echo $row["category"]; ?></td>
				<td><?php echo $row["pcode"]; ?></td>			
            </tr>  
<?php  
};  
?>  
</tbody>  
</table>  
<?php  

$productsData_db = mysqli_query($conn,"SELECT COUNT(id) FROM product_table"); 
$row_db = mysqli_fetch_row($productsData_db);  
$total_products = $row_db[0];  
$all_pages_count = ceil($total_products / $limit); 
/* echo  $all_pages_count; */
$htmlbindPageinationUrl = "<ul class='pagination'>";  
for ($i=1; $i<=$all_pages_count; $i++) {
              $htmlbindPageinationUrl .= "<li class='page-item'><a class='page-link' href='pagination.php?page=".$i."'>".$i."</a></li>";	
}
echo $htmlbindPageinationUrl . "</ul>";  
?>

</body>
</html>

Free Live Chat for Any Issue

Also Read: dynamic paginations-code-in-php-with-next-and-previous-demo in php with mysql example

Rate this post