total_pages = intval($value); } #how many items to show per page public function limit($value) { $this->limit = intval($value); } #Page to sent the page value public function target($value) { $this->target = $value; } #Current page public function currentPage($value) { $this->page = intval($value); } #How many adjacent pages should be shown on each side of the current page? public function adjacents($value) { $this->adjacents = intval($value); } #show counter? public function showCounter($value="") { $this->showCounter=($value===true)?true:false; } #to change the class name of the pagination div public function changeClass($value="") { $this->className=$value; } public function nextLabel($value) { $this->nextT = $value; } public function nextIcon($value) { $this->nextI = $value; } public function prevLabel($value) { $this->prevT = $value; } public function prevIcon($value) { $this->prevI = $value; } #to change the class name of the pagination div public function parameterName($value="") { $this->parameterName=$value; } #to change urlFriendly public function urlFriendly($value="%") { if (preg_match('/^ *$/i', $value)) { $this->urlF=false; return false; } $this->urlF=$value; } public $pagination; public function __construct() { } public function show() { if (!$this->calculate) { if ($this->calculate()) { echo "
className\">$this->pagination
"; } } } public function getPagination() { if (!$this->calculate) { if ($this->calculate()) { return "
className\">$this->pagination
"; } } } public function get_pagenum_link($id) { //if(strpos($this->target,'?')===false) if ($this->urlF) { return str_replace($this->urlF, $id, $this->target); } else { return "$this->target?$this->parameterName=$id"; } //else //return "$this->target&$this->parameterName=$id"; } public function calculate() { $this->pagination = ""; $this->calculate == true; $error = false; if ($this->urlF and $this->urlF != '%' and strpos($this->target, $this->urlF)===false) { //Es necesario especificar el comodin para sustituir echo "Especificaste un wildcard para sustituir, pero no existe en el target
"; $error = true; } elseif ($this->urlF and $this->urlF == '%' and strpos($this->target, $this->urlF)===false) { echo "Es necesario especificar en el target el comodin % para sustituir el n�mero de p�gina
"; $error = true; } if ($this->total_pages == null) { echo "It is necessary to specify the number of pages (\$class->items(1000))
"; $error = true; } if ($this->limit == null) { echo "It is necessary to specify the limit of items to show per page (\$class->limit(10))
"; $error = true; } if ($error) { return false; } $n = trim($this->nextT.' '.$this->nextI); $p = trim($this->prevI.' '.$this->prevT); /* Setup vars for query. */ if ($this->page) { $start = ($this->page - 1) * $this->limit; } //first item to display on this page else { $start = 0; } //if no page var is given, set start to 0 /* Setup page vars for display. */ if ($this->page == 0) { $this->page = 1; } //if no page var is given, default to 1. $prev = $this->page - 1; //previous page is page - 1 $next = $this->page + 1; //next page is page + 1 $lastpage = ceil($this->total_pages/$this->limit); //lastpage is = total pages / items per page, rounded up. $lpm1 = $lastpage - 1; //last page minus 1 /* Now we apply our rules and draw the pagination object. We're actually saving the code to a variable in case we want to draw it more than once. */ if ($lastpage > 1) { //anterior button if ($this->page > 1) { $this->pagination .= "get_pagenum_link($prev)."\">$p"; } else { $this->pagination .= "$p"; } //pages if ($lastpage < 7 + ($this->adjacents * 2)) {//not enough pages to bother breaking it up for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $this->page) { $this->pagination .= "$counter"; } else { $this->pagination .= "get_pagenum_link($counter)."\">$counter"; } } } elseif ($lastpage > 5 + ($this->adjacents * 2)) {//enough pages to hide some //close to beginning; only hide later pages if ($this->page < 1 + ($this->adjacents * 2)) { for ($counter = 1; $counter < 4 + ($this->adjacents * 2); $counter++) { if ($counter == $this->page) { $this->pagination .= "$counter"; } else { $this->pagination .= "get_pagenum_link($counter)."\">$counter"; } } $this->pagination .= "..."; $this->pagination .= "get_pagenum_link($lpm1)."\">$lpm1"; $this->pagination .= "get_pagenum_link($lastpage)."\">$lastpage"; } //in middle; hide some front and some back elseif ($lastpage - ($this->adjacents * 2) > $this->page && $this->page > ($this->adjacents * 2)) { $this->pagination .= "get_pagenum_link(1)."\">1"; $this->pagination .= "get_pagenum_link(2)."\">2"; $this->pagination .= "..."; for ($counter = $this->page - $this->adjacents; $counter <= $this->page + $this->adjacents; $counter++) { if ($counter == $this->page) { $this->pagination .= "$counter"; } else { $this->pagination .= "get_pagenum_link($counter)."\">$counter"; } } $this->pagination .= "..."; $this->pagination .= "get_pagenum_link($lpm1)."\">$lpm1"; $this->pagination .= "get_pagenum_link($lastpage)."\">$lastpage"; } //close to end; only hide early pages else { $this->pagination .= "get_pagenum_link(1)."\">1"; $this->pagination .= "get_pagenum_link(2)."\">2"; $this->pagination .= "..."; for ($counter = $lastpage - (2 + ($this->adjacents * 2)); $counter <= $lastpage; $counter++) { if ($counter == $this->page) { $this->pagination .= "$counter"; } else { $this->pagination .= "get_pagenum_link($counter)."\">$counter"; } } } } //siguiente button if ($this->page < $counter - 1) { $this->pagination .= "get_pagenum_link($next)."\">$n"; } else { $this->pagination .= "$n"; } if ($this->showCounter) { $this->pagination .= "
($this->total_pages Pages)
"; } } return true; } }