Pagination using Classes in PHP

What is Pagination?

Pagination is the process of dividing a document into discrete pages. On a website, it is used for such things as displaying a limited number of search engine results page, or viewing a limited number of images in the gallery on a page. Pagination is used in many web applications to divide the data and display it on multiple pages instead of one. It can be handled on the client side (using javascript) or on the server end (PHP). This tutorial is about achieving Pagination using Classes in PHP.

If you have no experience about Classes in PHP, you must first read this article : Classes in PHP

Example:

WordPress uses Pagination to limit the number of posts on a page.
Capture

In the above image, you can observe that the URL has /page/2/ which means that the current page is displaying the next 10 entries from the database.

In this tutorial, we will make an image gallery of 10 pictures and make a web page to display those 10 pictures in multiple pages using Pagination.

Step 1: Pagination Class

<?php
    class Pagination {
    public $current_page;
    public $per_page;     //$per_page is the number of items shown per page
    public $total_count;     //$total_count is the total number of items to be displayed

    public function __construct($page=1, $per_page=20, $total_count=0) {
        $this->current_page = (int)$page;
        $this->per_page = (int)$per_page;
        $this->total_count = (int)$total_count;
    }

    public function offset() {
        return ($this->current_page - 1) * $this->per_page;
    }

    public function total_pages() {
        return ceil($this->total_count/$this->per_page);
    }

    public function previous_page() {
        return $this->current_page - 1;
    } 

    public function next_page() {
        return $this->current_page + 1;
    }

    public function has_previous_page() {
        return $this->previous_page() >= 1 ? true : false;
    }

    public function has_next_page() {
        return $this->next_page() <= $this->total_pages() ? true : false;
    }
}
?>

The above code snippet creates a class Pagination with $current_page, $per_page and $total_count as public member variables and offset(), total_pages(), previous_page(), next_page(), has_previous_page(), has_next_page() as public member functions. The functions are self-explainatory.

Step 2: Initialization

<?php
    include('class.pagination.php');

    $page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
    $per_page = 3;
    $total_count = 10;
    $pagination = new Pagination($page, $per_page, $total_count);
?>

The above code snippet includes the class created above. Here we have defined 3 variables:

  • $page: This variable gets the value of the current page displayed. If there is no GET Parameter passed, it assumes that the first page has to be displayed.
  • $per_page: This variable is user defined. If we want three items per page, then $per_page should be set to 3.
  • $total_count: This is the total number of items that we need to display. Since, I have 10 pictures to be displayed. If you are making pagination to display limited number of rows from a table, then $total_count should get the total number of rows from the table.

After initializing the variables, now we create a $pagination object of the Class.

Step 3: Navigation Bar

<html>
<body>

    <ul>
        <?php
        /*************block 1****************/

            if($pagination->has_previous_page()) {
                echo '<li style="display:inline"><a href="index.php?page='.$pagination->previous_page().'">«</a></li>';
            } else {
                echo '<li style="display:inline" class="disabled"><a href="#">«</a></li>';
            }
        ?>
        <?php
        /*************block 2****************/

            for($i=1; $i<=$pagination->total_pages(); $i++) {
                echo '<a href="index.php?page='.$i.'"><li style="display:inline; margin-left:5px; margin-right:5px">'.$i.'</li></a>';
            }
        ?>
        <?php
        /***************block 3***************/

            if($pagination->has_next_page()) {
                echo '<li style="display:inline"><a href="index.php?page='.$pagination->next_page().'">»</a></li>';
            } else {
                echo '<li style="display:inline" class="disabled"><a href="#">»</a></li>';
            }
        ?>
    </ul>
</body>
</html>

The above code snippet, creates a navigation bar like this:
Capture

As you can observe, the above code is divided into 3 blocks. Block 2 prints the page numbers on the screen. Now for example, if we have to display total 10 images with 3 images per page, we need total 4 pages. Block 1 and Block 3 are optional, they just print ” if the user wants to go to one page next or previous of the current page. If for example, the user is on page 3, then ‘>’ will take him to page 4. However, when the user is at page 4, then ‘>’ will be disabled because there is no page 5!

Step 4: Displaying Images

Inside your HTML and above the navigation bar, copy paste the following code. You can download the sample images from the source code available at the end of this article.

<div>
    <?php
        $i = $pagination->offset() + 1;
        $limit = $pagination->per_page;
        while($i<=$pagination->total_count && $limit>0) {
            echo '<img src="img/product'.$i.'.jpg" style="display:inline; margin-left:10px">';
            $i++;
            $limit--;
        }
    ?>
</div>

This code snippet displays the images on the screen based on the current page number. For example, if the user is on page 2, then images 4, 5 and 6 will be displayed.

Conclusion

Capture 1

Capture

The above screenshots are the final output. Feel free to download the source code for reference.

View Demo Download source

This content has been helpful to you?

Thanks for contributing!

Yes No