Skip to content

Instantly share code, notes, and snippets.

@imhoffd
Created November 11, 2014 18:22
Show Gist options
  • Save imhoffd/b0bd375ee0165bfb7370 to your computer and use it in GitHub Desktop.
Save imhoffd/b0bd375ee0165bfb7370 to your computer and use it in GitHub Desktop.
Pagination class
<?php
class Pagination {
private $config;
private $base_url;
private $limit;
private $num_items;
private $page;
private $is_paginated = false;
private $first_button;
private $previous_button;
private $previous_links;
private $current_page;
private $next_links;
private $next_button;
private $last_button;
public function __construct($config = array()) {
$this->setConfig($config);
}
public static function newInstance($config = array()) {
return new self($config);
}
public function setConfig($config = array()) {
$this->config = $config + array(
'page' => null, // You can pass in the page number externally.
'page_buffer' => 4, // The number of page links to show between the current page number and the dots.
'show_previous_button' => true, // Show the previous page button when not on first page.
'show_next_button' => true, // Show the next page button when not on last page.
'show_first_button' => true, // Show the first page button when not on first page.
'show_last_button' => true, // Show the last page button when not on last page.
'force_show_first_button' => false, // Always show the first page button if set to true.
'force_show_last_button' => false, // Always show the last page button if set to true.
);
return $this;
}
public function setNumItems($num_items) {
$this->num_items = $num_items;
return $this;
}
public function setLimit($limit) {
$this->limit = $limit;
return $this;
}
public function setBaseUrl($base_url) {
$this->base_url = $base_url;
return $this;
}
public function getConfig() {
return $this->config;
}
public function getNumItems() {
return $this->num_items;
}
public function getLimit() {
return $this->limit;
}
public function getBaseUrl() {
return $this->base_url;
}
public function getNumPages() {
return $this->num_pages;
}
public function getPagination($num_items = null, $limit = null, $base_url = null) {
if (isset($num_items))
$this->setNumItems($num_items);
if (isset($limit))
$this->setLimit($limit);
if (isset($base_url))
$this->setBaseUrl($base_url);
$this->generatePagination();
// Pagination not needed.
if ($this->limit >= $this->num_items)
return false;
$output = "<div class=\"pagination\">";
if ($this->config['show_first_button'] && ($this->page != 1 || $this->config['force_show_first_button']))
$output .= $this->getFirstButton();
if ($this->config['show_previous_button'])
$output .= $this->getPreviousButton();
$output .= "<span class=\"pagination-links\">";
$output .= $this->getPreviousLinks() .
$this->getCurrentPage() .
$this->getNextLinks();
$output .= "</span>";
if ($this->config['show_next_button'])
$output .= $this->getNextButton();
if ($this->config['show_last_button'] && ($this->page != $this->num_pages || $this->config['force_show_last_button']))
$output .= $this->getLastButton();
$output .= "</div>";
return $output;
}
public function getFirstButton() {
$this->paginate();
return $this->first_button;
}
public function getPreviousButton() {
$this->paginate();
return $this->previous_button;
}
public function getPreviousLinks() {
$this->paginate();
return $this->previous_links;
}
public function getCurrentPage() {
$this->paginate();
return $this->current_page;
}
public function getNextLinks() {
$this->paginate();
return $this->next_links;
}
public function getNextButton() {
$this->paginate();
return $this->next_button;
}
public function getLastButton() {
$this->paginate();
return $this->last_button;
}
public function paginate() {
if(!$this->is_paginated)
$this->generatePagination();
return $this;
}
public function repaginate() {
$this->generatePagination();
return $this;
}
private function generatePagination() {
$this->page = isset($this->config['page']) ? $this->config['page'] : (isset($_REQUEST['page']) ? $_REQUEST['page'] : 1);
$this->base_url = isset($this->base_url) ? $this->base_url : $_SERVER['REQUEST_URI'];
$this->limit = isset($this->limit) ? $this->limit : 20;
if (!isset($this->num_items))
throw new Exception('The total number of items is required to generate pagination.');
$this->num_pages = ceil($this->num_items / $this->limit);
$parsed_url = parse_url($this->base_url);
$parsed_url_query = isset($parsed_url['query']) ? $this->parseUrlQuery($parsed_url['query']) : array();
if (isset($parsed_url_query['page']))
unset($parsed_url_query['page']);
$query = '';
foreach ($parsed_url_query as $param => $value) {
$query .= '&' . $param . '=' . $value;
}
$this->first_button = " <a href=\"" . $parsed_url['path'] . '?page=1' . $query . "\" class=\"pagination-first\">&laquo; First</a> ";
$this->last_button = " <a href=\"" . $parsed_url['path'] . '?page=' . $this->num_pages . $query . "\" class=\"pagination-last\">Last &raquo;</a> ";
if($this->page != 1) $this->previous_button = " <a href=\"" . $parsed_url['path'] . '?page=' . ($this->page - 1) . $query . "\" class=\"pagination-previous\">&lsaquo; Previous</a> ";
if($this->page != $this->num_pages) $this->next_button = " <a href=\"" . $parsed_url['path'] . '?page=' . ($this->page + 1) . $query . "\" class=\"pagination-next\">Next &rsaquo;</a> ";
$this->previous_links = '';
$this->next_links = '';
$pre_dots_added = $post_dots_added = $add_post_dots = false;
for ($i = 1; $i <= $this->num_pages; ++$i) {
if (($i != $this->page && $i == 1) || ($i < $this->page && $i > $this->page - ($this->config['page_buffer'] + 1))) {
$this->previous_links .= " <a href=\"" . $parsed_url['path'] . '?page=' . $i . $query . "\" class=\"pagination-page-" . $i . "\">" . $i . "</a> ";
}
if ($i < $this->page - ($this->config['page_buffer'] + 1) && !$pre_dots_added) {
$this->previous_links .= "<span class=\"pagination-dots pagination-pre-dots\"> ... </span>";
$pre_dots_added = true;
}
if ($i == $this->page) {
$this->current_page = " <strong class=\"pagination-current-page pagination-page-" . $i . "\">" . $i . "</strong> ";
}
if ($this->page < $this->num_pages - ($this->config['page_buffer'] + 1)) {
$add_post_dots = true;
}
if (($i != $this->page && $i == $this->num_pages) || ($i > $this->page && $i < $this->page + ($this->config['page_buffer'] + 1))) {
$this->next_links .= " <a href=\"" . $parsed_url['path'] . '?page=' . $i . $query . "\" class=\"pagination-page-" . $i . "\">" . $i . "</a> ";
}
if ($i == $this->num_pages - 1 && $add_post_dots) {
$this->next_links .= "<span class=\"pagination-dots pagination-post-dots\"> ... </span>";
}
}
}
private function parseUrlQuery($query) {
$array = array();
$exploded_query = explode('&', $query);
foreach ($exploded_query as $param) {
if ($param) {
$exploded_param = explode('=', $param);
$array[$exploded_param[0]] = $exploded_param[1];
}
}
return $array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment