<?php
class Pager {
// Total pages in constructed Pager object.
var $total_pages = 1;
// Elements of target array per page.
var $elem_per_page = 10;
// Total number of target array's elements.
var $count_elements = 0;
// Do not use it.
var $arr = array();
// Class' constructor. Creates object.
// Usage:
// $pager = new Pager($target_array,$elems_per_page);
//
function Pager($arr=array(),$per_page=10) {
$this->elem_per_page = $per_page;
$this->count_elements = count($arr);
if ( $this->count_elements % $per_page == 0 ) {
$this->total_pages = floor($this->count_elements/$per_page);
}
else {
$this->total_pages = floor($this->count_elements/$per_page)+1;
}
$this->arr = $arr;
}
// Method Page. Returns target array's page object number $pageno.
// Usage:
// $page = $pager->page($pageno);
//
function page($pageno) {
$page = new Page($pageno);
$from = $this->elem_per_page * ($pageno-1)+1;
$to = $from + $this->elem_per_page-1;
if ($to > $this->count_elements) {
$to = $this->count_elements;
}
$res = array();
reset($this->arr);
for ($i = ($from-1); $i < ($to); $i++) {
$res[]=$this->arr[$i];
}
$page->setFrom($from);
$page->setTo($to);
$page->setResult($res);
return $page;
}
// Prints page numbers
// Usage:
// $url = "$PHP_SELF?myvar=test";
// $pager->printPageNumbers($pageno,'series','numbers',$url,'pageno',$seriesrange);
//
// $pageno is current page number.
// 'all' or 'series' prints all page numbers or just $seriesrange pages backward and forward from current page.
// 'numbers' prints '[1] [2]' type page numbers, 'from' prints '[1] [13] [25]' type
// page numbers, 'fromto' prints '[1-12] [13-24] [25-36]' type page numbers.
// $url prints numbers with given URL plus 'pageno' parameter as page numbers variable.
// If you print 'all' page numbers, $seriesrange variable doesn't mean nothing.
//
// Simplest way to use:
// $pager->printPageNumbers($pageno);
//
function printPageNumbers($cp=1,$range='all',$type='numbers',$url='',$paramName='pageno',$seriesrange=2) {
global $PHP_SELF;
$range = (empty($range))?'all':$range;
$type = (empty($types))?'numbers':$type;
if (empty($url)) {
$url = $PHP_SELF.'?pageno=';
}
else {
if (preg_match('/\?/',$url)) {
$url = $url."&$paramName=";
}
else {
$url = $url."?$paramName=";
}
}
switch ($range) {
case 'all':
for ($i=1; $i <= $this->total_pages; $i++) {
$page = $this->page($i);
switch ($type) {
case 'numbers':
if ($i != $cp) {
echo "[<a href=\"$url$i\">$i</a>] ";
}
else {
echo "[$i] ";
}
break;
case 'from':
if ($i != $cp) {
echo "[<a href=\"$url$i\">$page->from</a>] ";
}
else {
echo "[$page->from] ";
}
break;
case 'fromto':
if ($i != $cp) {
echo "[<a href=\"$url$i\">$page->from-$page->to</a>] ";
}
else {
echo "[$page->from-$page->to] ";
}
break;
}
}
break;
case 'series':
if ($cp == $this->total_pages) {
$seriesrange = $seriesrange*2;
}
else {
if ($cp == 1) {
$seriesrange = $seriesrange*2;
}
}
$from = ($cp-$seriesrange < 1)? 1 : $cp-$seriesrange ;
$to = ($cp+$seriesrange > $this->total_pages)? $this->total_pages : $cp+$seriesrange ;
$prev = ($cp > 1)? $cp-1 : 1 ;
$next = ($cp < $this->total_pages)? $cp+1 : $this->total_pages ;
echo "<a href=\"${url}1\"><<</a> ";
echo "<a href=\"$url$prev\"><</a> ";
for ($i=$from; $i <= $to; $i++) {
$page = $this->page($i);
switch ($type) {
case 'numbers':
if ($i != $cp) {
echo "[<a href=\"$url$i\">$i</a>] ";
}
else {
echo "[$i] ";
}
break;
case 'from':
if ($i != $cp) {
echo "[<a href=\"$url$i\">$page->from</a>] ";
}
else {
echo "[$page->from] ";
}
break;
case 'fromto':
if ($i != $cp) {
echo "[<a href=\"$url$i\">$page->from-$page->to</a>] ";
}
else {
echo "[$page->from-$page->to] ";
}
break;
}
}
echo "<a href=\"$url$next\">></a> ";
echo "<a href=\"$url$this->total_pages\">>></a> ";
break;
}
}
}
?>
|