php pagination with ajax example

32

In this tutorial, we are going to see an example PHP code to add pagination to a list of records retrieved from database. I am using AJAX to fetch records for each page from database with jQuery. In a previous tutorial, we have seen already about AJAX pagination. In this example, I have added an option to choose pagination format and also I have changed the look and feel to enrich the per-page design.

In this example, we have two options based on which pagination format will be changed. One is displaying only Prev and Next links to navigate back and forth to the previous and next pages. The other format is displaying all page links and also with the links to navigate to the first, last, previous and next pages.

HTML Code to Choose Pagination Format

The following code shows a dropdown with two options for the pagination format. The default format is to display all per-page links. We call jQuery to change pagination format on the change event of the dropdown options.

Show Pagination Results via AJAX

We have a jQuery function to send an AJAX call to PHP to get pagination results. We call this function in two places. One is on changing pagination settings option to update pagination format. The other call is on clicking pagination links to show current page result and to highlight currently selected page.

Process AJAX Pagination Request in PHP

The following code reads all parameters like the current page, pagination-setting that are sent by an AJAX call. Based on these parameters PHP script calls pagination function to form AJAX response.

 

Leave A Reply

Your email address will not be published.