Simple Pagination Script in PHP & MySQL

Simple Pagination Script in PHP & MySQL, this script will explain you how pagination system is created in PHP & MySQL. Pagination is a sequence of pages to break number of results into small bytes in each page. We can limit these results based on our requirement.

Here I’m not working with a huge list, I’m working with simple data from the database. I’m going to display these results in table, which is styled with bootstrap styles. And also I’m using bootstrap pagination styles to display our pagination system.

First of all, we need to create a database table. I’m using crud table from crud application & also I’m working on view.php file from crud application in PHP & MySQL. If you don’t know, you can revise it from the URL. But, still I’ll provide necessary code here.

1. Create Database table with dummy data

2. Connecting to database using PHP

If you don’t know connecting to database in PHP, you can learn from this post -> Connect to MySQL database using PHP

3.view.php file from our CRUD Application

Save this file as pagination.php, we are going to modify this code by adding pagination system in PHP & MySQL. Here I’m not going to explain this view.php file. This is simple HTML table fetching data from database and displaying it in HTML page.

4. Adding HTML pagination buttons in Bootstrap

Here is the HTML code to add pagination buttons in bootstrap. Add this code below the table. If you reload this page in browser, you should see pagination button after table.

Now we have to make these pagination buttons dynamic.

5. Writing Code to make Pagination work

First of all, we have to define some variables. Create variable to display number of results.

Next, if the GET superglobal is set then assign this value to a variable. Otherwise, create a variable with default value.

Then modify ReadSql query with LIMIT clauses, here is the updated SQL

After using this above query, we will get 2 results. We will make these numbers dynamic using variabels. For that we have to create some more variables.

Here I’m calculating starting value that is used LIMIT clause.

To get number of results in database, we have to use this below code. This is to calculate number of pages in pagination

Next, we have to calculate start page, end page, next page, previous page.

Modify ReadSql query with variables to pass data.

6. Making pagination links dynamic

Here, in pagination buttons, I’ve five buttons.
First page button, Last page button. Previous page button, Current page button, Next page button.

First page pagination button.
If the current page is not equal to first page then only we are going to display this first page button.

Last page pagination button.
If the last page is not equal to current page then only we are going to dispaly this last page button.

Previous page pagination button
If the current page is greater than or equal to 2, then only we will display this previous page pagination button.

Current page pagination button
I’m not going to add any conditions here. Because, we will always display this current page link.

Next page pagination button

We can combine this code to both next page pagination button and last page pagination button. You can customize number of results on per page.

I hope now you understand the Pagination system in PHP & MySQL. If you have any problem in implementing the let me know through the comment form below.

Complete Code

If you have any problem arranging above pieces of code, you can use this complete code below.

If you have any problem in above code, let me know through comment form below.

Incoming search terms:

  • php - Next and previous( codingcyber com)
Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply:

x

Join My Facebook Group to Receive Coding Tutorial for Free

email-subsribe