Jul 2, 2017

Pagination

In the beginning of my career, every technical interview has this question; "How do you write pagination script in PHP?". I think you too faced the same issue. It was very important to answer this because every application is developed to list set of information and off-course you can't show every thing in a single page. It will irritate the user to scroll to the bottom of page to read.

So, the pagination. Divide the list of items in number of pages and navigate to each by clicking on each page number displayed. At the beginning, the pagination was simple was limited to 10-15 pages, but after some time in real time the requirement has grown to 10 multiple pages. Solution was to show first few page numbers, last few page numbers and in middle show the ellipsis(...). As you navigate to any page it would show definite page numbers to both the sides.

/**
 * Pagination file
 * 
 * This file provides the google like pagination solution
 * 
 * @category Saral
 * @package Pager
 * @version  0.1
 * @since  0.1
 */

/**
 * Pager class
 *
 * Class is used generate google like pagination links
 *
 * @category Saral
 * @package Pager
 * @version Release: 0.1
 * @since 26.Nov.2013
 * @author Sailesh Jaiswal
 */
class Pager
{

    /**
     *
     * css class name for links container
     *
     * @var string
     */
    private $class_pagination = 'pagination';

    /**
     *
     * css class name for default style for links
     *
     * @var string
     */
    private $class_default = 'page';

    /**
     *
     * css class name for disabled style for links
     *
     * @var string
     */
    private $class_disabled = 'disabled';

    /**
     *
     * css class name for current/selected link style
     *
     * @var string
     */
    private $class_current = 'current';

    /**
     *
     * used to set the css classes for links
     *
     * @param string $pagination            
     * @param string $default            
     * @param string $disabled            
     * @param string $current            
     */
    function setStyles($pagination = '', $default = '', $disabled = '', $current = '')
    {
        if ($pagination != '') {
            $this->class_pagination = $pagination;
        }
        if ($default != '') {
            $this->class_default = $default;
        }
        if ($disabled != '') {
            $this->class_disabled = $disabled;
        }
        if ($current != '') {
            $this->class_current = $current;
        }
    }

    /**
     *
     * generates pagination links
     *
     * @param string $url   url of the page         
     * @param integer $current_page   current page         
     * @param integer $total_records   total records         
     * @param integer $rpp            records per page
     * @param integer $adjacents      how many pages to show both the side of the current page in case if there are plenty of pages      
     * @return string
     */
    function showLinks($url, $current_page, $total_records, $rpp = 5, $adjacents = 2)
    {
        $class_pagination = $this->class_pagination;
        $page = $this->class_default;
        $current = $this->class_current;
        $disabled = $this->class_disabled;
        
        $pagination = "";
        $prev = $current_page - 1; // previous page is page - 1
        $next = $current_page + 1; // next page is page + 1
        $lastpage = ceil($total_records / $rpp); // lastpage is = total pages / items per page, rounded up.
        $lpm1 = $lastpage - 1; // last page minus 1
        $prev = $url . '?page=' . $prev;
        if ($lastpage > 1) {
            $pagination .= "
    "; // previous button if ($current_page > 1) $pagination .= "
  • << previous
  • "; else $pagination .= "
  • << previous
  • "; // pages if ($lastpage < 7 + ($adjacents * 2)) // not enough pages to bother breaking it up { for ($counter = 1; $counter <= $lastpage; $counter ++) { $i = $url . '?page=' . $counter; if ($counter == $current_page) $pagination .= "
  • $counter
  • "; else $pagination .= "
  • $counter
  • "; } } elseif ($lastpage > 5 + ($adjacents * 2)) // enough pages to hide some { // close to beginning; only hide later pages if ($current_page < 1 + ($adjacents * 2)) { for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter ++) { $i = $url . '?page=' . $counter; if ($counter == $current_page) $pagination .= "
  • $counter
  • "; else $pagination .= "
  • $counter
  • "; } $lpm11 = $url . '?page=' . $lpm1; $lastpage1 = $url . '?page=' . $lastpage; $pagination .= "
  • ...
  • "; $pagination .= "
  • $lpm1
  • "; $pagination .= "
  • $lastpage
  • "; } // in middle; hide some front and some back elseif ($lastpage - ($adjacents * 2) > $current_page && $current_page > ($adjacents * 2)) { $pagination .= "
  • 1
  • "; $pagination .= "
  • 2
  • "; $pagination .= "
  • ...
  • "; for ($counter = $current_page - $adjacents; $counter <= $current_page + $adjacents; $counter ++) { $i = $url . '?page=' . $counter; if ($counter == $current_page) $pagination .= "
  • $counter
  • "; else $pagination .= "
  • $counter
  • "; } $lpm11 = $url . '?page=' . $lpm1; $lastpage1 = $url . '?page=' . $lastpage; $pagination .= "
  • ...
  • "; $pagination .= "
  • $lpm1
  • "; $pagination .= "
  • $lastpage
  • "; } // close to end; only hide early pages else { $pagination .= "
  • 1
  • "; $pagination .= "
  • 2
  • "; $pagination .= "
  • ...
  • "; for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter ++) { $i = $url . '?page=' . $counter; if ($counter == $current_page) $pagination .= "
  • $counter
  • "; else $pagination .= "
  • $counter
  • "; } } } // next button if ($current_page < $counter - 1) { $next = $url . '?page=' . $next; $pagination .= "
  • next >>
  • "; } else { $pagination .= "
  • next >>
  • "; } $pagination .= "
\n"; } return $pagination; } } $total_records = 100; //total records that you want to show as paginated $page = $_GET['page']; $pager = new Pager; echo $pager->showLinks("http://example.com/posts.php", $page, $total_records, 10, 2);