How to implement Pagination in php

15/04/2017   04:13:17 PM

Hello, In this tutorial, we will use PHP and MySQL to create simple pagination script. Pagination lets you to paging your data into a number of pages. When we get huge data from the database instead of showing all of it at once we will choose different ways to display that data. One such technique is PHP pagination.

Pagination in php is very simple concepts. Just follow the below steps.

We will create these php pages. -config.php (Database Configuration) -index.php, function.php

1) Create Database & Table

  • Create a database and table in mysql.Here database name is sampledb & table name is employee

  • You also need to insert some values in your table as you can see above.

2) Create config file page

  • Create config.php page in your project folder and add the following code

config.php

<?php
$db = @mysql_connect('localhost', 'root', '') or die(mysql_error());
@mysql_select_db('sampledb', $db) or die(mysql_error());
?>

 

3) Create function.php page

In this function we first get the total count of row in the mysql table. Then we calculate the number of page according to total count.

<?php
function pagination($query, $per_page = 10,$page = 1, $url = '?')
{
$query = "SELECT COUNT(*) as `num` FROM {$query}";
$row = mysql_fetch_array(mysql_query($query));
$total = $row['num'];
$adjacents = "2";
 
$page = ($page == 0 ? 1 : $page);
$start = ($page - 1) * $per_page;
 
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total/$per_page);
$lpm1 = $lastpage - 1;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<ul class='pagination'>";
$pagination .= "<li class='details'>Page $page of $lastpage</li>";
if ($lastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class='current'>$counter</a></li>";
else
$pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";
}
}
elseif($lastpage > 5 + ($adjacents * 2))
{
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class='current'>$counter</a></li>";
else
$pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";
}
$pagination.= "<li class='dot'>...</li>";
$pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>";
$pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";
}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<li><a href='{$url}page=1'>1</a></li>";
$pagination.= "<li><a href='{$url}page=2'>2</a></li>";
$pagination.= "<li class='dot'>...</li>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class='current'>$counter</a></li>";
else
$pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";
}
$pagination.= "<li class='dot'>..</li>";
$pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>";
$pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";
}
else
{
$pagination.= "<li><a href='{$url}page=1'>1</a></li>";
$pagination.= "<li><a href='{$url}page=2'>2</a></li>";
$pagination.= "<li class='dot'>..</li>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<li><a class='current'>$counter</a></li>";
else
$pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";
}
}
}
if ($page < $counter - 1){
$pagination.= "<li><a href='{$url}page=$next'>Next</a></li>";
$pagination.= "<li><a href='{$url}page=$lastpage'>Last</a></li>";
}
Else
{
$pagination.= "<li><a class='current'>Next</a></li>";
$pagination.= "<li><a class='current'>Last</a></li>";
}
$pagination.= "</ul>\n";
}
return $pagination;
}
?>

 

4) Create pagination.css page  

ul.pagination{
margin:0px;
padding:0px;
height:100%;
overflow:hidden;
font:12px 'Tahoma';
list-style-type:none;
}
 
ul.pagination li.details{
padding:7px 10px 7px 10px;
font-size:14px;
}
 
ul.pagination li.dot{padding: 3px 0;}
 
ul.pagination li{
float:left;
margin:0px;
padding:0px;
margin-left:5px;
}

ul.pagination li:first-child{
margin-left:0px;
}
 
ul.pagination li a{
color:black;
display:block;
text-decoration:none;
padding:7px 10px 7px 10px;
}
 
ul.pagination li a img{
border:none;
}

 

5) You can use the another following css code to create your page. Copy and save it as grey.css in your project folder.

Grey.css

ul.pagination li.details{
color:#888888;
}
 
ul.pagination li a
{
color:#FFFFFF;
border-radius:3px;
-moz-border-radius:3px;
-webkit-border-radius:3px;
}
 
ul.pagination li a
{
color:#474747;
border:solid 1px #B6B6B6;
padding:6px 9px 6px 9px;
background:#E6E6E6;
background:-moz-linear-gradient(top,#FFFFFF 1px,#F3F3F3 1px,#E6E6E6);
background:-webkit-gradient(linear,0 0,0 100%,color-stop(0.02,#FFFFFF),color-stop(0.02,#F3F3F3),color-stop(1,#E6E6E6));
}
 
ul.pagination li a:hover,
ul.pagination li a.current
{
background:#FFFFFF;

 

6) Create index.php page 

Here you can set the page limit $limit = 5.

In this page we first set a LIMIT to display the page. Then make a query to display all the content with initial page limit and then after every click of page we will get a page number and according to that page the previous query again execute. This process will running until the page end.

<?php

    include_once ('config.php');

    //get the function

    include_once ('function.php');

    $page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);

    $limit = 5;

    $startpoint = ($page * $limit) - $limit;

    //to make pagination

    $statement = "`employee`";

?>

<table>

<tr><th>First Name</th><th>Last Name</th><th>Salary</th></tr>

        <?php

            //show records

            $query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint} , {$limit}");

                     while ($row = mysql_fetch_assoc($query)) {

        ?>

            <div class="record round"><?php echo "<tr><td>{$row['firstname']}</td><td>{$row['lastname']}</td><td>{$row['salary']}</td></tr>";?></div>

        <?php   

            }

        ?></table>

<br />

<br />

<?php

           echo pagination($statement,$limit,$page);

?>


Output

                   

Support us by Sharing