PHP Pagination


A lightweight PHP pagination class to output pagination links.

This class is written to be chain-able so to create a logically fluent and easily readable way to create a set of pagination links.

It simplifies the paging of results and outputs Bootstrap 4 compatible navigation HTML.

It has been fully tested to work with PHP 5.5+, including PHP 7+

Installation via Composer

You can now install this class via composer:

composer require benhall14/php-pagination

**Remember** to add the composer autoloader before using the class and use the correct namespace.

require 'vendor/autoload.php';
use benhall14\PHPPagination\Pagination as Pagination;


Please make sure you have added the required classes.

In its simplest form, you can use the following to set up the paginator.

$pagination = new Pagination();

You can use the following chainable methods to customise the final pagination links.

# sets the total number of items in the collection - e.g. 100

# sets the current page. By default, the class looks for the page value in the GET query string.

# sets the number of items to show per page. Default = 20

# sets the separator (if using). Default '...'

# sets the screen reader class. Default 'true', but you may need to set it to false if you are using your own custom css.

# sets the Bootstrap 4 pagination link class to small

# sets the Bootstrap 4 pagination link class to medium

# sets the Bootstrap 4 pagination link class to large

# sets the Bootstrap 4 alignment class to left

# sets the Bootstrap 4 alignment class to center

# sets the Bootstrap 4 alignment class to right

# sets the flag to show the separator

# sets the flag to hide the separator

# sets the next text string - Default: 'Next'

# sets the previous text string - Default: 'Previous'

# hides the 'Next' link

# shows the 'Next' link

# hides the 'Previous' link

# shows the 'Previous' link

# sets a prefix text for each page link: {prefix} {page number} {suffix}

# sets a suffix text for each page link: {prefix} {page number} {suffix}

# sets the flag to retain the query string for each page link.

# sets the flag to ignore the query string when building the links

# sets the number of pages BEFORE the separator

# sets the number of pages AROUND the active page

# sets the URL pattern - Default $pattern: ?page=(:num)- $replacement: (:num)
$pagination->pattern($pattern, $replacement);

# The class will replace the $replacement token in the $pattern with the actual page number


Works with PHP 5.5, PHP 5.6, and PHP 7+<


Copyright (c) Benjamin Hall, -

Licensed under the MIT license


If you find this project helpful or useful in any way, please consider getting me a cup of coffee - It's really appreciated :)


More Projects?
View Code Showcase
Benjamin, is brilliant at what he does, he really knows what he is at. Gives real useful advice and guidance. He is fast and timely provides updates and always provides solutions and listens well. Trust me Benjamin is a breath of fresh air.
- Alexander T.
Ben had a great idea for my project and completed the project to our requirements very quickly. He also helped me tweak a few issues and answered my questions quickly. We'll work with Ben again!
- Gordon S.
I was impressed: a freelancer who could give me inputs and details on how he'd implement the solution, kept me updated in the meanwhile, understood my requirements and proposed pretty nice tweaks to make things even better. Ben, you are amazing. I will need no more time to find any other dev's, and will surely deal with you again if I need something related to these things in the future. Full stop.
- Marco F.
Absolutely superb, a great work ethic, outstanding knowledge and a great eye for detail. I thoroughly recommend him!
- Gary J.
Benjamin is a talented developer who can tackle technically challenging projects with great service and communication. Thoroughly recommended.
- Steven M.
Ben truly went above and beyond. He's fantastic at communication, coding, getting round problems, fixing issues, WordPress, custom plugins and much more. I will be using Ben again for other projects.
- Rachael R.
Ben went the extra mile and not only resolved the problem but provided a more suitable solution for the long term. Cheers!
- William R.
Ben is an amazing developer. He took the project and delivered it on time and within budget, whilst maintaining excellent communication. He also worked beyond what was asked to make sure the project was functioning correctly. Would highly recommend A++++!
- David B.
Fantastic work on our JS script. Ben worked beyond the goals of the project and overcame extra issues that were not defined in the project. He is very quick to respond and patent when waiting for our response. One of our top 5 Developers, We had to deal with many Interfaces including server firewalls/Java-scripting/JSON/PHP/PHP APIs/Remote Databases and automation.
- Kevin J.
Ben is just great to work with. Efficient, clearly communicating, understanding very quickly, and also giving great advice. I will continue working with Ben on any other WordPress stuff. Sorry for all those other PPH'ers :-)
- Bruno V.
Absolutely amazing experience. Ben is an expert in this field, I was amazed at how quickly he jumped on our platform and decoded the code, db etc. He will go over and above to find solutions for clients. Highly recommended.
- Harrus A.
Interested in working together to make something great? Hire CONOBE