Skip to content

Instantly share code, notes, and snippets.

@geeksunny
Last active December 10, 2015 17:18
Show Gist options
  • Save geeksunny/4466855 to your computer and use it in GitHub Desktop.
Save geeksunny/4466855 to your computer and use it in GitHub Desktop.
Simple class for generating pagination links.
<?php
class pagination {
private $forward = true;
private $backward = true;
private $current_page;
private $last_page;
function __construct($current_page=false, $last_page=false, $generate=false) {
if ($current_page) {
$this->current_page = $current_page;
}
if ($last_page) {
$this->last_page = $last_page;
}
if ($generate) {
$this->process();
}
}
/* ~~~~~~~~~~~~~~~
~~~ Setters ~~~
~~~~~~~~~~~~~~~ */
public function set_current_page($current) {
$this->current_page = $current;
}
public function set_last_page($last) {
$this->last_page = $last;
}
/* ~~~~~~~~~~~~~~~
~~~ Getters ~~~
~~~~~~~~~~~~~~~ */
public function get_current_page() {
return $this->current_page;
}
public function get_last_page() {
return $this->last_page;
}
public function get_forward() {
return $this->forward;
}
public function get_backward() {
return $this->backward;
}
public function get_previous_page() {
return $this->current_page - 1;
}
public function get_next_page() {
return $this->current_page + 1;
}
public function get_pagerange($range = 5) {
$step = (int)($range / 2);
// Start and End points
if (($this->current_page - $step) < 1) {
$start = 1;
$end = ($range > $this->last_page) ? $this->last_page : $range;
} elseif (($this->current_page + $step) > $this->last_page) {
$end = $this->last_page;
$start = (($end-$range) < 1) ? 1 : ($end-$range);
} else {
$start = $this->current_page - $step;
$end = $this->current_page + $step;
}
// Generating range. Current page is false, all other spots are true.
$range = range($start,$end);
$return = array();
foreach ($range as $spot) {
if ($spot == $this->current_page) {
$return[$spot] = false;
} else {
$return[$spot] = true;
}
}
return $return;
}
/* ~~~~~~~~~~~~~~~
~~~ Helpers ~~~
~~~~~~~~~~~~~~~ */
public function calculate_current_and_last_page($start_index, $total_items, $items_per_page, $set=false, $process=true) {
$current_page = (int)($start_index / $items_per_page)+1;
$last_page = (int)($total_items / $items_per_page);
if (($total_items % $items_per_page) > 0) {
$last_page++;
}
if ($set) {
$this->set_current_page($current_page);
$this->set_last_page($last_page);
if ($process) {
$this->process();
}
}
}
public function calculate_last_page($total_items, $items_per_page, $set=false, $process=true) {
$last_page = (int)($total_items / $items_per_page);
if (($total_items % $items_per_page) > 0) {
$last_page++;
}
if ($set) {
$this->set_last_page($last_page);
if ($process) {
$this->process();
}
} else {
return $last_page;
}
}
public function process() {
if ($this->current_page == 1) {
$this->backward = false;
} elseif ($this->current_page == $this->last_page) {
$this->forward = false;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment