EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP Pagination

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP Pagination

PHP Pagination

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 on 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 the pagination and its uses in the PHP language.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

There is nothing to deal with the pagination syntax. There are a number of ways we can handle the 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, 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:

PHP Pagination-1.1

If we click on page numbers (1,2 or 3) will get the different records on the web page.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,694 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

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:

PHP Pagination-1.2

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:

PHP Pagination-1.3

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 the 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 and how does php pagination work? along with the example. You may also have a look at the following articles to learn more –

  1. PHP Final Class
  2. preg_match in PHP
  3. PHP Tag in HTML
  4. PHP unset()

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More