Pagination is a very common task for a PHP developer. With the power of OOP coding style, it is nature to have a reusable pagination class, which we can use it for various projects. Because for an advanced developer, we should never repeat the same coding process.(Don't Repeat Yourself).
In this tutorial, I will go through the process of writing your very own PHP paginator class.

Table Of Contxent

  1. Introduction
  2. Code Explaination
  3. How To Use
  4. Download
  5. The End

Do You Want To Become a Modern PHP Developer? Check out our premium content Modern PHP Developer.

Content


1. Introduction

During a normal PHP coding task, pagination is always associated with a database (MySql in most cases). However in this tutorial, we are going to separate pagination from data source in order to achieve the goal of a reusable pagination class.
So you do not have to worry about the data source (database) part. All of our concerns will be the pagination numbers as well as navigation.

Firstly, let me show you the completed class, and then I will explain it in details:

2. Code Explaination

The class name is "Paginator". Firstly let us take a look at its member variables:

  • public $itemsPerPage This tells Paginator class how many items per page.
  • public $range: This variable set the range between current page and two ending page numbers. For example, if current page is 5, and range is 2, pagination numbers will be showed as 3,4,5,6,7. Current page will be the middle page number.
  • public $currentPage:This variable stores current page number.
  • public $total:This variable stores total number of pages.
  • public $textNav:This variable decides if to show text navigation(Pre,Next) links. This approach makes it flexiable to hide/show the text navigation.
  • public $itemSelect:This variable is an array of numbers, which is used to form the items per page select box.
  • private $_navigation:This variable stores all the text strings include 'Pre','Next' as well as 'Item per pages'. This approach makes it easy to change the text.
  • private $_link:This variable stores the link value, which is the sanitized $_SERVER['PHP_SELF'] value.
  • private $_pageNumHtml:This variable stores the html content for paginations. We store it here to avoid repeated formatting process.
  • private $_itemHtml:This variable stores the html content for items per page select box. We store it here to avoid repeated formatting process.

All the variables are initialized in the class constructor. Take a look at it to have an idea of their default values:


First public function is paginate(), this function is the main function used to process the pagination:

  1. We use $_GET['current'] to get the current page number from URL and set it to $currentPage if it is set.
  2. We use $_GET['item'] to get number of items per page from URL and set it to $itemsPerPage if it is set.
  3. We call a private function $_getPageNumbers() to process the pagination numbers and store it to variable $_pageNumHtml.
  4. We call a private function $_getItemSelect() to process the items per page select box and store it to variable $_itemHtml.

As we have seen from last function, two private functions ($_getPageNumbers() and $_getItemSelect()) are actually doing the job of generating the pagination html content. So let us take a look at these two functions:

  1. Set pre text link when $textNav is set to true and $currentPage is greater than 1.
  2. This process is to figure out the $start and $end paging numbers.
    IF (total page > range) {
    	start page = (current page <= range) ?  1 :  current page - range;
    	end page   = (total page - current page >= range) ? current page +  range: total;
    }ELSE{
    	start page = 1;
    	end page   = total page;
    } 
    
  3. Loop using the $start and $end value to form the paging numbers as well as its links.
  4. Set next text link when $textNav is set to true and $currentPage is less than total page.

This function is very straightforward. It simply uses a foreach loop to form a select box.


Lastly, let us take a look at two public functions, which will be actually called by developers to print out the pagination.

As you can see, besides from returning the pagination html, this function always provides a debug message, reminding people to call paginate() function first, This will be discussed on How To Use section. The reason, we have a separate function to return the html content is that it allows developers to print out the pagination multiple times without reprocessing.

public function itemsPerPage() is almost identical to pageNumbers().

3.How To Use

To use this class, there are something you will need to do with your data source (MySql possibly, but can be anything). Firstly you will need to tell Paginator total number of records by setting variable $total. Secondly you will need to set your data record using combination of $currentPage and $itemsPerPage from Paginator class.
For example, I am using MySql to store my data. What I need to do is firstly find total number of records (SELECT COUNT) using MySql and set it to $total:

After that, you will typically use "LIMIT" query to get data from database:

Finally, you can call pageNumbers() function to print the pagination, and itemsPerPage() to print the items per page select box. Even more, with the way this class is built. You can print both of them multiple times, and it will not cause any performance issue:

Since this class does not come with any style sheet, you should provide some basic style sheet for it to look better.

4.Download

You can download this script from my github account.

5.The End

Thank you for reading this article, and if you have any encountered anything different, have a different solution or think our solution is wrong, do let us know in the comment section.
We will be very happy to hear that.

If you like our tutorial, please follow us on Twitter and help spread the word. We need your support to continue.

Do You Want To Become a Modern PHP Developer? Check out our premium content Modern PHP Developer.