Pagination Code in PHP

Pagination is the basic feature which is required almost in all the web application and websites. When we have huge number of records to display than we must implement pagination.

Pagination is the the process of breaking your website search result into some pages and showing it one by one.

Advantages of Pagination:

Faster Access of Data:

If we display large number of records in one page only than it may slow down loading of our webpage.  So it is the basic necessity to improve speed of loading of our webpage.

Convenience of Use:

It makes more convenient for users to read the data and accessing it.

Below is the basic code for writing and showing records by pagination: You can use below code and modify as per your need of development.


.pagination {
display: inline-block;
padding-left: 0;
margin: 20px 0;
border-radius: 4px;
.pagination li {
display: inline;
margin-left: 0;
border-top-left-radius: 4px;
border-bottom-left-radius: 4px;
.pagination > .active > a, .pagination > .active > a:hover{
z-index: 3;
color: #fff;
cursor: default;
background-color: #337ab7;
border-color: #337ab7;
.pagination > li > a, .pagination > li > span {
position: relative;
float: left;
padding: 5px;
text-decoration: none;


// database variables
$host = "localhost";  //database location
$user = "root";  //database username
$pass = "";  //database password
$db_name = "countries_db";  //database name
$link = mysql_connect($host, $user, $pass);

function get_transactions_details($limit=10, $offset=0){
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$start_from = ($page-1) * $limit;

$trans_all = array();
$query = "SELECT * FROM countries WHERE 1 ORDER BY countries_name LIMIT $start_from, $limit";
$sql = mysql_query($query) or die ("MYsql Error" . mysql_error());
$trans_all[] = $fetch;
return $trans_all;

function paging($table_name, $link ='#', $where = '1', $limit=4, $offset=0) {
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $limit;
$sql = "SELECT COUNT(countries_id) FROM $table_name where $where ORDER BY countries_name";

// $sql = get_search_query($data, $checklist, $cat, $limit, $offset=0);

$rs_result = mysql_query($sql) or die ('MySQL Error'.mysql_error());
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];   // mysql_num_rows($rs_result)
$total_pages = ceil($total_records / $limit);
$pagLink = "<ul class='pagination'>";
$active = '';
for ($i=1; $i<=$total_pages; $i++) {
if(@$_GET['page'] == $i)
$active = 'active';
$active = '';
if(($active == '') && ($i ==1) && (!@$_GET['page']))
$active = 'active';
$pagLink .= "<li class='$active'><a href='$link?page=".$i."'>" . $i. "</a></li>";
$pagLink .= "</ul>";
return $pagLink;


<h3 class="text-danger text-left" style="margin-top:0;"> Countries List </h3>
<table width="100%">
<tr class="danger">
<td class="text-center"> S.No </td>
<td class="text-center"> Country Name </td>
<td class="text-center"> Country ISO Code </td>
<td class="text-center"> Country ISD Code </td>

$i = 0;
$i = ($limit * ($_GET['page']-1));

$trans_list = get_transactions_details($limit, $offset=0);

$paging = paging($table_name ='countries’, $link ='pagination.php', $where = "1", $limit, $offset=0);

// p($trans_list);

foreach($trans_list as $trans_details){
echo '<tr>
<td width="8%" class="text-center">' .++$i. '</td>
<td class="text-center">' .$trans_details['countries_name']. '</td>
<td class="text-center">' .$trans_details['countries_iso_code']. '</td>
<td class="text-center">' .$trans_details['countries_isd_code']. '</td>';
echo '</tr>';


<div class="text-center">

<?php echo $paging; ?>





PHP Pagination

PHP Pagination

Leave a Reply

Notify of