Last active
December 10, 2015 02:49
-
-
Save akhawaja/4370739 to your computer and use it in GitHub Desktop.
Pagination class for PHP that outputs HTML compatible with Twitter Bootstrap. It uses a sliding scale when displaying the pagination links.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Pagination class that outputs HTML compatible with Twitter Bootstrap. | |
* It uses a sliding scale when displaying the pagination links. | |
* | |
* @author Amir Khawaja <[email protected]> | |
*/ | |
class Pagination | |
{ | |
protected $page; | |
protected $split = 8; | |
/** | |
* @param int $currentPage | |
* @param int $totalRecords | |
* @param int $rowsPerPage | |
* @param string $baseUrl | |
* @param string $queryString | |
*/ | |
public function __construct($currentPage, $totalRecords, $rowsPerPage, $baseUrl = '', $queryString = '') | |
{ | |
$page['rows_per_page'] = $rowsPerPage; | |
$page['total_rows'] = $totalRecords; | |
$page['current_page'] = $currentPage; | |
$page['total_pages'] = ceil($totalRecords / $rowsPerPage); | |
$page['next_page'] = (($currentPage + 1) > $page['total_pages']) ? $page['total_pages'] : $currentPage + 1; | |
$page['previous_page'] = (($currentPage + 1) > $page['total_pages']) ? $page['total_pages'] : $currentPage - 1; | |
$page['base_url'] = $baseUrl; | |
$page['query_string'] = $queryString; | |
if ($page['total_pages'] == 0) { | |
$page['total_pages'] = 1; | |
} | |
$this->page = $page; | |
} | |
/** | |
* Get the pagination as HTML. | |
* @return string | |
*/ | |
public function getPaginationHtml() | |
{ | |
$html = '<ul>'; | |
$url = $this->page['base_url']; | |
$qs = $this->page['query_string']; | |
$cp = $this->page['current_page']; | |
$tp = $this->page['total_pages']; | |
$prevPage = ($cp == 1) ? 1 : $cp - 1; | |
$nextPage = ($cp == 1) ? 1 : $cp + 1; | |
// For Page Sliding | |
$padding = floor($this->split / 2); | |
// Determine the Page Start and Stop Index | |
if ($tp > $this->split) { | |
if ($cp >= ($padding + 1)) { | |
if ($cp > ($tp - $padding)) { | |
$startIndex = $tp - ($padding * 2); | |
$stopIndex = $tp; | |
} else { | |
$startIndex = $cp - $padding; | |
$stopIndex = $cp + $padding; | |
} | |
} else { | |
$startIndex = 1; | |
$stopIndex = ($padding * 2) + 1; | |
} | |
} else { | |
$startIndex = 1; | |
$stopIndex = $tp; | |
} | |
if ($this->isFirstPage()) { | |
$html .= '<li class="disabled"><span>First</span></li>' . PHP_EOL; | |
$html .= '<li class="disabled"><span>« Prev</span></li>' . PHP_EOL; | |
} else { | |
if (strpos($qs, '=') > 0) { | |
$html .= sprintf('<li><a href="%s?%s&p=%d">First</a></li>', $url, $qs, 1) . PHP_EOL; | |
$html .= sprintf('<li><a href="%s?%s&p=%d">« Prev</a></li>', $url, $qs, $prevPage) . PHP_EOL; | |
} else { | |
$html .= sprintf('<li><a href="%s?p=%d">First</a></li>', $url, 1) . PHP_EOL; | |
$html .= sprintf('<li><a href="%s?p=%d">« Prev</a></li>', $url, $prevPage) . PHP_EOL; | |
} | |
} | |
for ($i = $startIndex; $i <= $stopIndex; $i++) { | |
if ($cp == $i) { | |
$html .= sprintf('<li class="active"><span>%d</span></li>', $i) . PHP_EOL; | |
} else { | |
if (strpos($qs, '=') > 0) { | |
$html .= sprintf('<li><a href="%s?%s&p=%d">%d</a></li>', $url, $qs, $i, $i) . PHP_EOL; | |
} else { | |
$html .= sprintf('<li><a href="%s?p=%d">%d</a></li>', $url, $i, $i) . PHP_EOL; | |
} | |
} | |
} | |
if ($this->isLastPage()) { | |
$html .= '<li class="disabled"><span>Next »</span></li>' . PHP_EOL; | |
$html .= '<li class="disabled"><span>Last</span></li>' . PHP_EOL; | |
} else { | |
$html .= sprintf('<li><a href="%s?%s&p=%d">Next »</a></li>', $url, $qs, $nextPage) . PHP_EOL; | |
$html .= sprintf('<li><a href="%s?%s&p=%d">Last</a></li>', $url, $qs, $tp) . PHP_EOL; | |
} | |
$html .= '</ul>' . PHP_EOL; | |
return $html; | |
} | |
public function getPaginationSummary() | |
{ | |
$low = (($this->page['current_page'] - 1) * $this->page['rows_per_page']) + 1; | |
$high = ($this->page['total_rows'] <= $this->page['rows_per_page']) | |
? ($this->page['total_rows']) | |
: ($low + $this->page['rows_per_page']) - 1; | |
$html = sprintf('Displaying %d to %d of %d entries.', $low, $high, $this->page['total_rows']); | |
return $html; | |
} | |
/** | |
* Get the pagination data. | |
* @return array | |
*/ | |
public function getPaginationData() | |
{ | |
return $this->page; | |
} | |
public function isFirstPage() | |
{ | |
return $this->page['current_page'] == 1; | |
} | |
public function isLastPage() | |
{ | |
return $this->page['total_pages'] == $this->page['current_page']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment