Simple HTML Pagination using PHP & MYSQL

Pagination helps you in navigating through pages on your site and has proven to be useful in sites having many articles and/or items (usually more than 30). Basically, it helps you to distribute your articles among pages by grouping them (ordered by date), and making your pages short enough for convenience of users with slow connections. Here we are going to create a Simple HTML Pagination by using PHP and MYSQL. Let’s start with creating a configuration file and then we will be proceeding with selecting records from database and rendering our pagination in footer.

Simple HTML Pagination using PHP & MYSQL

Configuration File (config.php):

We will now set our configuration parameters which we will be using later at different places in our code. To do this, we are putting the following code in a new file config.php. Below is its code:

We are done with setting configurations related to our database. Let’s proceed with Creating Pagination Function.

Functions.php (Includes Pagination Function):

Above code should be placed in a new file named functions.php. Now let’s have a look on its usage:

Create Pagination.php file:

Let’s create another file pagination.php and put some code in it. We will obviously be including both of our previously created files in this one. Have a look below:

 Pagination.php Explained:

In start we included our necessary files (config and functions) and then we moved towards getting page number from query string (in browser URL). After that we have defined range for using in our database query (for selecting records with specific starting and ending points). For example if we are on page 2 and are using 10 items per page, then our range will be from 11 to 20. Then comes our SQL query for counting total number of items from our items table in database. After getting items we stored them in another variable for later use. Now in our final query we select items for displaying in page (this query used starting and ending limits defined previously). And finally we have our records inserted into an array ready for use. And then we displayed items in their place. In the last, we used our Pagination Function which returned Pagination Output in HTML.

Till now, all of our main work has been completed regarding Pagination. Let’s give it some styling using CSS:

In above PHP code, I assigned some CSS classes and IDs to HTML Tags. So after adding the above styling code (into your page), your Pagination will start looking like in the Demo. You may change this CSS  as you like.

Try it yourself. Also get a copy of this script’s files downloaded to your computer.

Simple-html-pagination-using-php-mysql
Simple-html-pagination-using-php-mysql
simple-html-pagination-using-php-mysql.zip
3.9 KiB
282 Downloads
Details

9 Replies to “Simple HTML Pagination using PHP & MYSQL”

  1. mnm says:

    not working

    1. Umar Hassan says:

      Please can you tell me what issue or error is occurring.

  2. Kally says:

    The $current_file_path = ”; is not working meaning it will not give you page two, three and so on. I enter the supposed url of the current file correctly. http://127.0.0.1/testpage.php

    1. David says:

      I’m having exactly the same issue.

  3. api says:

    Thanks 🙂 for the code!. 🙂

  4. Marcin says:

    I doesn’t work for me too. I entered path to main file in $current_file_path but after clicking another page it display page without css and with incorrect records.

    1. Marcin says:

      Im using Wamp.

  5. sk sharma says:

    not working in other pages. I also try your code but this also not working. I change your code
    as $current_file_path = ‘http://localhost/pages/pagination.php/’;

  6. ceasar says:

    You forgot the $prev function

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.

scroll to top