Updated March 31, 2023
Introduction to PHP Pagination
In any web application, the listing of the records is very common. This list of records could be sometimes huge. Loading of these records on a single page can take a longer time. To deal with these records, we have the pagination features in the PHP programming language. The moment we talk about a web application and the listing of the records, pagination comes into existence. Pagination is all about listing the whole record on multiple pages rather than displaying it on a single page. As we all know, everyone wants the information loaded with a single click in very minimal time. We need to use this pagination feature smartly to reduce the page loading time. In the coming section, we will explore something more about pagination and its uses in the PHP language.
Syntax:
There is nothing to deal with the pagination syntax. There are a number of ways we can handle pagination. To make the pagination functional in the PHP, we would have to take the help of the HTML, CSS, JavaScript, JQuery, etc as well.
In the example section, we will see how to get the number of the pages, the current page, and the records of those pages.
How does it Work?
To put the pagination at work we must have the number of records. If there is no possibility of more records, then pagination will not be helpful for us. Here is the list of key things we will have to work on:
- Get the Current Page: This is one of the key things a developer should focus on – how to get the current page. Once we have the current page, we can get the number of records using that page number or we can perform other operations as per the business requirements.
- Get the Total Number of The Pages: Once we will have the total number of the pages then we can create the pagination buttons.
- Pagination Button: Once we calculate the number of pages, we can create buttons like 1, 2 3, 4 till the last page. We can also use the CSS code to make the user interface beautiful.
- Next and Previous Button: If we have more than 2 pages then we should have Next and the Previous button. We can say this is an addition to the basics of the pagination features.
We will be using the database for selecting the records then we will display it on the web page with the pagination.
Examples of PHP Pagination
Following are the examples of PHP pagination:
Example #1
In this example, we will select the records from the database. After selecting the data from a database table we will list the 5 records on each page.
Code:
Filename: index.php
<?php
// Database Connection
$hostname='localhost';
$user='root';
$pass='';
$databaseName = "employees";
$conn=mysqli_connect($hostname,$user,$pass,"$databaseName");
if(!$conn){
die('Database Connection issue.<br>' .mysql_error());
}
$numberOfRecordsPerPage = 5; // number of records page page to be displayed.
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else{
$page=1;
}
$start_page = ($page-1) * $numberOfRecordsPerPage;
$result = mysqli_query($conn,"SELECT * FROM employees ORDER BY emp_no ASC LIMIT $start_page, $numberOfRecordsPerPage");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Pagination in PHP</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<?php
$records = mysqli_query($conn,"SELECT count(*) FROM employees ORDER BY emp_no ASC");
$row_db = mysqli_fetch_row($records);
$total_records = $row_db[0];
?>
<table class="table table-bordered table-striped">
<tr>
<td><h1>Total Number of records </h1></td>
<td><h1><?php echo $total_records; ?></h1></td>
</tr>
</table>
<table class="table" border="1" cellpadding="4px">
<thead>
<tr>
<th>Emp No</th>
<th>Birth Date</th>
<th>First Name</th>
<th>Last Name</th>
<th>Gender</th>
<th>hire_date</th>
</tr>
<thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["emp_no"]; ?></td>
<td><?php echo $row["birth_date"]; ?></td>
<td><?php echo $row["first_name"]; ?></td>
<td><?php echo $row["last_name"]; ?></td>
<td><?php echo $row["gender"]; ?></td>
<td><?php echo $row["hire_date"]; ?></td>
</tr>
<?php
};
?>
</tbody>
</table>
<?php
//Generating pagination page number
$total_pages = ceil($total_records / $numberOfRecordsPerPage);
$pagLink = "<ul class='pagination'>";
for ($i=1; $i<=$total_pages; $i++) {
$pagLink .= "<li class='page-item'><a class='page-link' href='index.php?page=".$i."'>".$i."</a></li>";
}
echo $pagLink . "</ul>";
?>
</body>
</html>
<style>
.pagination {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
padding-left: 0;
list-style: none;
border-radius: .25rem;
padding: 10px;
}
.pagination .page-item{
padding: 10px;
}
</style>
Output:
If we click on page numbers (1,2 or 3) will get the different records on the web page.
Example #2
Continuing with the same code as above let’s try to make the pagination button more beautiful. After adding the below code we can see our pagination button is looking nice.
Code:
<style>
.pagination {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
padding-left: 0;
list-style: none;
border-radius: .25rem;
}
.pagination .page-item{
padding: 8px;
border: #259dc7 1px solid;
margin: 11px;
background: lightblue;
color: white;
}
.page-item a{
text-decoration: none;
font-weight: bold;
color: red;
font-size: 20px;
}
</style>
Output:
Example #3
Let’s make the whole page more beautiful
After making the page numbers more beautiful we have to make the table better. Now, our CSS code will look like below mentioned.
Code:
<style>
.pagination {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
padding-left: 0;
list-style: none;
border-radius: .25rem;
}
.pagination .page-item{
padding: 8px;
border: #259dc7 1px solid;
margin: 11px;
background: lightblue;
color: white;
}
.page-item a{
text-decoration: none;
font-weight: bold;
color: red;
font-size: 20px;
}
.table{
background: #333030;
color: white;
margin-bottom: 10px;
border: green 2px solid;
}
.table th{
background: #31316f;
padding: 5px;
font-size: 22px;
}
</style>
The output will remain the same as we see in the first example, but the output screen will be changed.
Output:
Conclusion
Pagination is really a good thing to move ahead with while we deal with the huge number of records. It can present the page to the user in a very short time as we will be loading the part of the results rather than loading the whole results. We should use HTML and CSS to make our page design better, this way we can make our web page more readable.
Recommended Articles
This is a guide to PHP Pagination. Here we also discuss the introduction, syntax, and working of Pagination in PHP along with different examples and code implementation. You may also have a look at the following articles to learn more –