AJAX pagination using PHP and MySQL with Demo

AJAX pagination using PHP and MySQL with Demo

This tutorial will be on AJAX pagination using PHP and MySQL. We know Pagination is one of the beautiful tweaks for presenting your site to your visitors. People feel that everything is placed on its exact location. It is also called the website management. Today, we will learn about that beautiful task; pagination. We can use any type of pagination like server based or client based. So, the server based pagination will send request to the server, it will refresh the page, response will be generated and the desired results will be loaded. The issue is that, the page refresh loses the visitor attraction. In terms of client based pagination, we use jQuery or AJAX for this purpose to avoid the page refresh. The problem with jquery is that it will load all the data at the same time on the page but it is hidden by CSS rules. So, I prefer to use the AJAX pagination. Let’s start it.


Prerequisites for AJAX pagination using PHP and MySQL:

  • PHP
  • MySQL
  • AJAX
  • CSS

AJAX pagination flow:

As AJAX is used to avoid the page refresh and is processes all the data in the backend and displays the result. Same thing is used here. Look at the following HTML code.

Here, I’ve the CSS id target-content. When page loads it will populate only two the results because the I’ve limited it to 2 in the file pagination.php as $limit = 2; It is fetching all the records but with limit 2.

The CSS id append-pagination starts the AJAX magic for pagination. It will append the pagination in this page. It generates an unordered list with anchor tags. So, when I click on any number it sends the request to the pagination.php and loads the next two records. You will observe that rest of code is same as we use in simple PHP pagination. Here paginate_function function controls this process.

paginate_function function has four parameters as

  • $item_per_page,
  • $current_page,
  • $total_records,
  • $total_pages

The parameters names describes their tasks. So, that is simple code for AJAX pagination using PHP and MySQL. I hope it will helpful for you. The Whole code can be downloaded from HERE.