How to create pagination in PHP and MySQL
To handle the large number of records in database pagination is most important part to view records.
This article will teach you how to create pagination in PHP with mysql database results.
I have created a pagination class which holds the mysql results and html for generated links.
This is very simple and secure to use.
Follow these steps to set pagination with your DB results.
1. Download a fresh copy of pagination class from github.
2. Extract the zip and put Pagination folder in your project directory.
3. Include the autoload file in your file
require_once __DIR__."/Pagination/autoload.php";
Now the pagination class is fully access in your project.
4. Now first create an object of pagination class and pass the limit for display the no of result on a page.
$pagination = new \Pagination\Pagination(10);
Here 10 is the limit of per page results.
Pagination class has been initialized.
5. To get results from database call `sql()` and put your sql query without limit.
$pagination->sql("SELECT * FROM dummy"); $pagination->results();
6. And in last for generate pagination links call –
$pagination->links();
There are so many other functions for different links;
echo "Start: ".$pagination->start(); echo "End : ".$pagination->end(); echo "CurrentPage : ".$pagination->currentPage(); echo "Current Page URL: ".$pagination->url(); echo "Total Page: ".$pagination->totalPage; echo "Total Result: ".$pagination->total();
Here an example