<?php
// +———————————————————————-+
// | php version 4 |
// +———————————————————————-+
// | copyright (c) 1997-2002 the php group |
// +———————————————————————-+
// | this source file is subject to version 2.02 of the php license, |
// | that is bundled with this package in the file license, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | if you did not receive a copy of the php license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +———————————————————————-+
// | authors: richard heyes <richard@phpguru.org> |
// +———————————————————————-+
/**
* pager class
*
* handles paging a set of data. for usage see the example.php provided.
*
*/
class pager {
/**
* current page
* @var integer
*/
var $_currentpage;
/**
* items per page
* @var integer
*/
var $_perpage;
/**
* total number of pages
* @var integer
*/
var $_totalpages;
/**
* item data. numerically indexed array…
* @var array
*/
var $_itemdata;
/**
* total number of items in the data
* @var integer
*/
var $_totalitems;
/**
* page data generated by this class
* @var array
*/
var $_pagedata;
/**
* constructor
*
* sets up the object and calculates the total number of items.
*
* @param $params an associative array of parameters this can contain:
* currentpage current page number (optional)
* perpage items per page (optional)
* itemdata data to page
*/
function pager($params = array())
{
global $http_get_vars;
$this->_currentpage = max((int)@$http_get_vars[pageid], 1);
$this->_perpage = 8;
$this->_itemdata = array();
foreach ($params as $name => $value) {
$this->{_ . $name} = $value;
}
$this->_totalitems = count($this->_itemdata);
}
/**
* returns an array of current pages data
*
* @param $pageid desired page id (optional)
* @return array page data
*/
function getpagedata($pageid = null)
{
if (isset($pageid)) {
if (!empty($this->_pagedata[$pageid])) {
return $this->_pagedata[$pageid];
} else {
return false;
}
}
if (!isset($this->_pagedata)) {
$this->_generatepagedata();
}
return $this->getpagedata($this->_currentpage);
}
/**
* returns pageid for given offset
*
* @param $index offset to get pageid for
* @return int pageid for given offset
*/
function getpageidbyoffset($index)
{
if (!isset($this->_pagedata)) {
$this->_generatepagedata();
}
if (($index % $this->_perpage) > 0) {
$pageid = ceil((float)$index / (float)$this->_perpage);
} else {
$pageid = $index / $this->_perpage;
}
return $pageid;
}
/**
* returns offsets for given pageid. eg, if you
* pass it pageid one and your perpage limit is 10
* it will return you 1 and 10. pageid of 2 would
* give you 11 and 20.
*
* @params pageid pageid to get offsets for
* @return array first and last offsets
*/
function getoffsetbypageid($pageid = null)
{
$pageid = isset($pageid) ? $pageid : $this->_currentpage;
if (!isset($this->_pagedata)) {
$this->_generatepagedata();
}
if (isset($this->_pagedata[$pageid])) {
return array(($this->_perpage * ($pageid – 1)) + 1, min($this->_totalitems, $this->_perpage * $pageid));
} else {
return array(0,0);
}
}
/**
* returns back/next and page links
*
* @param $back_html html to put inside the back link
* @param $next_html html to put inside the next link
* @return array back/pages/next links
*/
function getlinks($back_html = << back, $next_html = next >>)
{
$url = $this->_getlinksurl();
$back = $this->_getbacklink($url, $back_html);
$pages = $this->_getpagelinks($url);
$next = $this->_getnextlink($url, $next_html);
return array($back, $pages, $next, back => $back, pages => $pages, next => $next);
}
/**
* returns number of pages
*
* @return int number of pages
*/
function numpages()
{
return $this->_totalpages;
}
/**
* returns whether current page is first page
*
* @return bool first page or not
*/
function isfirstpage()
{
return ($this->_currentpage == 1);
}
/**
* returns whether current page is last page
*
* @return bool last page or not
*/
function islastpage()
{
return ($this->_currentpage == $this->_totalpages);
}
/**
* returns whether last page is complete
*
* @return bool last age complete or not
*/
function islastpagecomplete()
{
return !($this->_totalitems % $this->_perpage);
}
/**
* calculates all page data
*/
function _generatepagedata()
{
$this->_totalitems = count($this->_itemdata);
$this->_totalpages = ceil((float)$this->_totalitems / (float)$this->_perpage);
$i = 1;
if (!empty($this->_itemdata)) {
foreach ($this->_itemdata as $value) {
$this->_pagedata[$i][] = $value;
if (count($this->_pagedata[$i]) >= $this->_perpage) {
$i++;
}
}
} else {
$this->_pagedata = array();
}
}
/**
* returns the correct link for the back/pages/next links
*
* @return string url
*/
function _getlinksurl()
{
global $http_server_vars;
// sort out query string to prevent messy urls
$querystring = array();
if (!empty($http_server_vars[query_string])) {
$qs = explode(&, $http_server_vars[query_string]);
for ($i = 0, $cnt = count($qs); $i < $cnt; $i++) {
list($name, $value) = explode(=, $qs[$i]);
if ($name != pageid) {
$qs[$name] = $value;
}
unset($qs[$i]);
}
}
if(is_array($qs)){
foreach ($qs as $name => $value) {
$querystring[] = $name . = . $value;
}
}
return $http_server_vars[script_name] . ? . implode(&, $querystring) . (!empty($querystring) ? & : ) . pageid=;
}
/**
* returns back link
*
* @param $url url to use in the link
* @param $link html to use as the link
* @return string the link
*/
function _getbacklink($url, $link = << back)
{
// back link
if ($this->_currentpage > 1) {
$back = <a href=" . $url . ($this->_currentpage – 1) . "> . $link . </a>;
} else {
$back = ;
}
return $back;
}
/**
* returns pages link
*
* @param $url url to use in the link
* @return string links
*/
function _getpagelinks($url)
{
// create the range
$params[itemdata] = range(1, max(1, $this->_totalpages));
$pager =& new pager($params);
$links = $pager->getpagedata($pager->getpageidbyoffset($this->_currentpage));
for ($i=0; $i<count($links); $i++) {
if ($links[$i] != $this->_currentpage) {
$links[$i] = <a href=" . $this->_getlinksurl() . $links[$i] . "> . $links[$i] . </a>;
}
}
return implode( , $links);
}
/**
* returns next link
*
* @param $url url to use in the link
* @param $link html to use as the link
* @return string the link
*/
function _getnextlink($url, $link = next >>)
{
if ($this->_currentpage < $this->_totalpages) {
$next = <a href=" . $url . ($this->_currentpage + 1) . "> . $link . </a>;
} else {
$next = ;
}
return $next;
}
}
?>
