Pagination in PHP using class.

@mit $ingh. January 28, 2017 Comments

Class :-

<?php

class Paginator {

    private $_conn;
    private $_limit;
    private $_page;
    private $_query;
    private $_total;

    public function __construct($conn, $query, $totalQuery) {

        $this->_conn = $conn;
        $this->_query = $query;

        $rs = $this->_conn->query($totalQuery);
        $this->_total = $rs->fetch_assoc()['count'];
    }

    public function getData($limit = 10, $page = 1) {

        $this->_limit = $limit;
        $this->_page = $page;

        if ($this->_limit == 'all') {
            $query = $this->_query;
        } else {
            $query = $this->_query . " LIMIT " . ( ( $this->_page - 1 ) * $this->_limit ) . ", $this->_limit";
        }
        $rs = $this->_conn->query($query);

        while ($row = $rs->fetch_assoc()) {
            $results[] = $row;
        }

        $result = new stdClass();
        $result->page = $this->_page;
        $result->limit = $this->_limit;
        $result->total = $this->_total;
        $result->data = $results;

        return $result;
    }

    public function createLinks($no_of_links, $link_url) {
        if ($this->_limit == 'all') {
            return '';
        }

        ######################## PAGING LOGIC START ##########################
        $last_page = ceil($this->_total / $this->_limit);
        $middle_page_no = ceil($no_of_links / 2);
        $left_right_side_pages_no = $middle_page_no - 1;

        //calculating start
        $start = 1;
        if ($this->_page > $middle_page_no)
            $start = $this->_page - $left_right_side_pages_no;

        //calculating end
        $end = $this->_page + $left_right_side_pages_no;
        if ($this->_page < $middle_page_no) {
            $tmp = $middle_page_no - $this->_page;
            $end = $end + $tmp;
        }
        if ($end > $last_page)
            $end = $last_page;

        //Re-calculating start according to end.
        $tmp = $last_page - $this->_page;
        if ($tmp == 0)
            $start = $start - $left_right_side_pages_no;
        elseif ($tmp < $left_right_side_pages_no)
            $start = $start - ($left_right_side_pages_no - $tmp);
        $start = ($start < 1) ? 1 : $start;
        ######################## PAGING LOGIC END ##########################

        $html = '<ul class = "pagi-ul">';

        ####----FIRST & PREV----####
        if ($this->_page == 1) {
            $html .= '<a href="javascript:void(0)"><li class="pagi-disabled"> << </li></a>';
            $html .= '<a href="javascript:void(0)"><li class="pagi-disabled"> < </li></a>';
        } else {
            $html .= '<a href="' . $link_url . '1"><li title="First"> << </li>';
            $html .= '<a href="' . $link_url . ($this->_page - 1) . '">'
                    . '<li title="Previous"> < </li></a>';
        }

        ####----ACTIVE LINKS----####
        for ($i = $start; $i <= $end; $i++) {
            $url = ($this->_page == $i) ? 'javascript:void(0)' : $link_url . $i;

            $html .= '<a href="' . $url . '">';
            $html .= '<li class="' . ($this->_page == $i ? 'pagi-active' : '') . '">' . $i . '</li>';
            $html .= '</a>';
        }

        ####----INaCTIVE LINKS----####
        if ($end < $no_of_links) {
            for ($i = ($end + 1); $i <= $no_of_links; $i++) {

                $html .= '<a href="javascript:void(0)">';
                $html .= '<li class="pagi-disabled">' . $i . '</li>';
                $html .= '</a>';
            }
        }

        ####----LAST & NEXT----####
        if ($this->_page == $last_page) {
            $html .= '<a href = "javascript:void(0)"><li class = "pagi-disabled"> > </li>';
            $html .= '<a href = "javascript:void(0)"><li class = "pagi-disabled"> >> </li></a>';
        } else {
            $html .= '<a href = "' . $link_url . ($this->_page + 1) . '"><li title = "Next"> > </li></a>';
            $html .= '<a href = "' . $link_url . $last_page . '"><li title = "Last"> >> </li></a>';
        }

        $html .= '</ul>';

        return $html;
    }

}
?>

Usage in HTML :-

#To fetch the data
<?php
$totalQuery = 'SELECT count(id) as count FROM students WHERE status = "Active"';
$query = 'SELECT fname, name, class, rollno FROM students WHERE status = "Active" ORDER BY id DESC';
$Paginator = new Paginator($mysqliConn, $query, $totalQuery);
$p = empty($_REQUEST['page']) ? 1 : $_REQUEST['page'];
$students = (array) $Paginator->getData(7, $p);
--------------------------------------------------------------------------------
#To create paging links in HTML
$no_of_links = 13; #No of links to show including active and inactive
$link_url = '/students/?page=';
echo $Paginator->createLinks($no_of_links, $link_url);

CSS for paging :-

<style>
    .pagi-ul li{list-style: none; float: left; margin-right: 10px; border: 1px #000 solid; padding: 0px 10px; background-color: #C1C682; }
    .pagi-ul li.pagi-disabled{background-color: #F3EEEA !important; cursor: not-allowed; opacity: 0.3;}
    .pagi-ul li.pagi-active{background-color: #E37722 !important; cursor: cell;}
</style>

 

  • PHP
  • pagination