Skip to content

Instantly share code, notes, and snippets.

@wGEric wGEric/Pagination
Last active Sep 27, 2015

Embed
What would you like to do?
Pagination class. Was written for a specific system so it could/should be updated to use a constructor instead of setVars or switched to static methods
<?php
class Pagination {
private $total_items = 0;
private $current_number = 0;
private $per_page = 25;
/**
* sets up the variables
*/
public function setVars($total, $current = 0, $per_page = 25) {
$this->total_items = $total;
$this->current_number = $current/$per_page;
$this->per_page = $per_page;
}
/**
* display pagination
*/
public function display($url, $total = 0, $current = 0, $per_page = 25) {
if (!empty($total) || !empty($current)) {
$this->setVars($total, $current, $per_page);
}
echo $this->generate($url);
return;
}
/**
* generates pagination. returns a string
*/
public function generate($url, $total = 0, $current = 0, $per_page = 25) {
if (!empty($total) || !empty($current) || !empty($per_page)) {
$this->setVars($total, $current, $per_page);
}
// do we need a ? or & for the url
$url .= (strpos($url, '?') === false) ? '?start=' : '&start=';
// calculate how many pages
$total_pages = ceil($this->total_items / $this->per_page);
$output = array();
$before = $after = '';
// if number is greater than 0 then show first link
if ($this->current_number > 0) {
$before .= '<a href="' . $url . '0">&lt;&lt; First</a>&nbsp;&nbsp;';
}
// if number is great than 1 then show first and prev link
if ($this->current_number > 1) {
$before .= '<a href="' . $url . (($this->current_number - 1) * $this->per_page) . '">&lt; Prev</a>&nbsp;&nbsp;';
}
// if the number is great than 3 then it is cutting some off so show ...
// also set the starting point for looping through the pages
if ($this->current_number > 3) {
$before .= '...&nbsp;&nbsp;';
$i = $this->current_number - 3;
} else {
$i = 0;
}
// loop through the pages
for(; $i < $total_pages; $i++) {
$output[] = '<a href="' . $url . ($i * $this->per_page) . '"' . (($i == $this->current_number) ? ' class="selected"' : '') . '>' . ($i + 1) . '</a>';
// if the current number + 2 is less than the current number then break out of the loop. Enough pages have been shown.
if ($this->current_number + 2 < $i) {
break;
}
}
// if the current page plus 4 is less than the total pages then show ...
if ($this->current_number + 4 < $total_pages) {
$after .= '&nbsp;&nbsp;...';
}
// if number is less than total pages minus one then show next link
if ($this->current_number < $total_pages - 2) {
$after .= '&nbsp;&nbsp;<a href="' . $url . (($this->current_number + 1) * $this->per_page) . '">Next &gt;</a>';
}
// if number is less than the total number of pages then show last link
if ($this->current_number < $total_pages - 1) {
$after .= '&nbsp;&nbsp;<a href="' . $url . (($total_pages - 1) * $this->per_page) . '">Last &gt;&gt;</a>';
}
return 'Page: ' . $before . implode(',&nbsp;&nbsp;', $output) . $after;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.