Last active
September 27, 2015 03:57
-
-
Save wGEric/1207476 to your computer and use it in GitHub Desktop.
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
This file contains hidden or 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 | |
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"><< First</a> '; | |
} | |
// 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) . '">< Prev</a> '; | |
} | |
// 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 .= '... '; | |
$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 .= ' ...'; | |
} | |
// if number is less than total pages minus one then show next link | |
if ($this->current_number < $total_pages - 2) { | |
$after .= ' <a href="' . $url . (($this->current_number + 1) * $this->per_page) . '">Next ></a>'; | |
} | |
// if number is less than the total number of pages then show last link | |
if ($this->current_number < $total_pages - 1) { | |
$after .= ' <a href="' . $url . (($total_pages - 1) * $this->per_page) . '">Last >></a>'; | |
} | |
return 'Page: ' . $before . implode(', ', $output) . $after; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment