Simple tutorial of pagination in php with Demo

In this PHP tutorial, we’ll learn how to make pagination with PHP and MySql. The pagination can be ajax-based, but in this case, I’m creating pagination without ajax. To create dynamic pagination, I’m using bootstrap3 and PHP5. I’m assuming you’ve got a table and some data.

Normally, SQL SELECT query may return results containing thousands or millions of records. displaying all of those records on a single page is not a good idea. So, the output can be divided into multiple pages.

What is Pagination?

Paging allows you to display all of your retrieved results on multiple pages rather than all of them on a single page.

I am following the simple steps outlined below:

  • Step 1: I will connect a MySQL database with table.
  • Step 2: Data is retrieved from a MySQL table and displayed in table.
  • Step 3: I’ll add a pagination link at the bottom of the table.

Checkout other tutorials of pagination,

Let’s make an index.php file and paste the code below into it. This code will assist you in creating pagination in PHP. To create pagination at the bottom of the table, I’ll use the bootstrap code>pagination/code> component.

Let’s create a table and iterate rows data on table row using a while loop.

Now, we will count all records and create pagination –

Watch on Youtube

Result:

pagination in php

You can check live demo, YouTube video and download source code

15 thoughts on “Simple tutorial of pagination in php with Demo

  1. Hello.
    I’ve tried some scripts to make the active state on navigation, but with no success. Can you provide me some directions for how to generate the active state nav on active page? Thanks.

    • Ya sure,
      You can use jQuery live event on nav link like below:
      $(‘.navlink’).live(‘click’, function({
      $(‘.navlink’).removeClass(‘active’);
      $(this).addClass(‘active’);

      });

      This code fired on your navigation links which has ‘navlink’ class.We first remove active class and then add active class on current clicked navigation.
      I hope its help.

  2. Hi!
    Thanks for this useful code!
    What if I’ve hundreds of pages?
    Any way to shrink it to few pages and “next” and “previous” buttons?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.