<?php
# Version 1.0
/**
(# of pages to show to the left/right of current page, GET variable the script will look for to see what page number you are on)
$pag = new pagination(20,'page');
$message_count = $sql->numRows();
$pag->set_total($message_count);
**/
class pagination
{
var $page_current;
var $limit = 5;
var $found = 0;
var $start;
var $counter = 0;
var $total = 0;
var $showing_first = false;
var $page_var = 'page';
//Pass in how many results per page you want
//if you fail to pass this in it will use a default value
function pagination($limit = 10, $page_var = 'page')
{
//This makes sure the page variable is a postive int
//if not, it will default to page 1
$this->page_var = $page_var;
if(isset($_REQUEST[$this->page_var]) && is_numeric($_REQUEST[$this->page_var]))
{
$this->page_current = strtok($_REQUEST[$this->page_var],'.');
$this->start = (($_REQUEST[$this->page_var] * $limit) - ($limit));
}
else
{
$this->page_current = 1;
$this->start = 0;
}
$this->limit = $limit;
}
function orderBy( $orderby = '', $up = '/\\', $down = '\/', $hide = 1)
{
if(!isset($_GET[$this->page_var])) $_GET[$this->page_var] = 1;
if( $hide == 1 )
{
$order = '';
if(isset($_GET['orderby'])) $order = $_GET['orderby'];
$direction = '';
if(isset($_GET['direction'])) $direction = $_GET['direction'];
if( $order == $orderby && $direction == 'desc' )
$orderby1 = "<a href=\"{$_SERVER['PHP_SELF']}?{$this->page_var}={$_GET[$this->page_var]}&orderby=$orderby&direction=asc\">{$up}</a>";
elseif( $order == $orderby && $direction == 'asc' )
$orderby2 = " <a href=\"{$_SERVER['PHP_SELF']}?{$this->page_var}={$_GET[$this->page_var]}&orderby=$orderby&direction=desc\">{$down}</a>";
else
{
$orderby1 = "<a href=\"{$_SERVER['PHP_SELF']}?{$this->page_var}={$_GET[$this->page_var]}&orderby=$orderby&direction=asc\">{$up}</a>";
$orderby2 = " <a href=\"{$_SERVER['PHP_SELF']}?{$this->page_var}={$_GET[$this->page_var]}&orderby=$orderby&direction=desc\">{$down}</a>";
}
} else
{
$orderby1 = "<a href=\"{$_SERVER['PHP_SELF']}?{$this->page_var}={$_GET[$this->page_var]}&orderby=$orderby&direction=asc\">{$up}</a>";
$orderby2 = " <a href=\"{$_SERVER['PHP_SELF']}?{$this->page_var}={$_GET[$this->page_var]}&orderby=$orderby&direction=desc\">{$down}</a>";
}
$returnorder = '';
if(isset($orderby1)) $returnorder .= $orderby1;
if(isset($orderby2)) $returnorder .= $orderby2;
return $returnorder;
}
//The limit + 1 is there to find out if there are more records in the database
//to allow the next link to be displayed
function limit_query()
{
$pages = ceil($this->total / $this->limit);
if(($this->page_current > $pages) || ($this->page_current <= 0))
{
$this->page_current = 1;
$this->start = 0;
}
return ' LIMIT '.$this->start.','.($this->limit);
}
//Used to determine if it should show the next link or not
//function set_found($found){ $this->found = $found; }
//Set the total number of records that would be displayed if all were on one page
//If you want to use the show_list you must set this
function set_total($total){ $this->total = $total; }
function get_list($each_side = 10)
{
//If total was set and is greater then 0
if($this->total > 0)
{
//Get total number of possible pages
$pages = ceil($this->total / $this->limit);
//If each sides are more then the total pages then just loop through and show each page
if(($each_side * 2) >= $pages)
{
//build list of all the pages
for($x=1;$x<=$pages;$x++)
{
$list[$x] = $x;
}
//If there are more pages then how many sides you want to show
}else
{
//get where to start the list
$start = $this->page_current - $each_side;
//prevent list from going lower then 1
if($start < 1) $start = 1;
//get end of the list
$end = $this->page_current + $each_side;
//loop from start to end of given sides
for($x=$start;$x<=$end;$x++)
{
//if the loop count is less then or equal to the total pages then display
//this prevents displaying links to pages futher then the results will go
if($x <= $pages)
{
$list[$x] = $x;
}
}
}
//return complete built list
return $list;
}
else
return false;
}
//Requires that you set the total number of records
//Shows links to pages and how many links to show on each side of the current page
//EX: If you set each side to 5 and you are on page 10, it would show 5,6,7,8,9,10,11,12,13,14,15
//EX: If you set each side to 5 and you are on page 2, it would show 1,2,3,4,5,6,7
function show_list($link,$each_side = 10)
{
//If total was set and is greater then 0
if($this->total > 0)
{
$list = '';
//Get total number of possible pages
$pages = ceil($this->total / $this->limit);
//If each sides are more then the total pages then just loop through and show each page
if(($each_side * 2) >= $pages)
{
//build list of all the pages
for($x=1;$x<=$pages;$x++)
{
//Don't display link if on current page
if($this->page_current == $x)
$list .= $x;
else
//build link
$list .= ' '.$link.$this->page_var.'='.($x).'">'.$x.'</a> ';
}
//If there are more pages then how many sides you want to show
}else
{
//get where to start the list
$start = $this->page_current - $each_side;
//prevent list from going lower then 1
if($start < 1) $start = 1;
//get end of the list
$end = $this->page_current + $each_side;
//loop from start to end of given sides
for($x=$start;$x<=$end;$x++)
{
//if the loop count is less then or equal to the total pages then display
//this prevents displaying links to pages futher then the results will go
if($x <= $pages)
{
//Don't display link if on current page
if($this->page_current == $x)
$list .= $x;
else
//build link
$list .= ' '.$link.$this->page_var.'='.($x).'">'.$x.'</a> ';
}
}
}
//return complete built list if more then 1 page
if($list == ' 1')
return false;
else
return $list;
}
else
return false;
}
function show_first($one,$two)
{
//prevent showing first and previous at same time
if($this->page_current == 2) $this->showing_first = true;
//Prevent showing first if already on first page
if($this->page_current > 1)
{
return $one.$this->page_var.'=1">'.$two.'</a>';
}
else
return false;
}
function show_last($one,$two)
{
$pages = ceil($this->total / $this->limit);
//prevent showing last if already on last page
if($this->page_current == $pages)
{
return false;
}
else
{
return $one.$this->page_var.'='.$pages.'">'.$two.'</a>';
}
}
function show_link($one,$two)
{
return $one.$this->page_var.'='.($this->page_current).'">'.$two.'</a>';
}
//If more results are found this will display the link
function show_next($one,$two)
{
//get total pages possible
$pages = ceil($this->total / $this->limit);
//Prevent showing next and last links if both go to same page
if($this->page_current != ($pages) && $pages != 0)
{
return $one.$this->page_var.'='.($this->page_current + 1).'">'.$two.'</a>';
//make sure to show next link only if there is enough results to for another page
if($this->found > $this->limit)
return $one.$this->page_var.'='.($this->page_current + 1).'">'.$two.'</a>';
else
return false;
}
}
//If not of first page show the link
function show_prev($one,$two)
{
//prevent showing previous and first if both take to same page
if($this->showing_first == false)
{
//prevent showing previous link if on first page
if($this->page_current != 1)
return $one.$this->page_var.'='.($this->page_current - 1).'">'.$two.'</a>';
else
return false;
}
}
//Echo out variables in class
function debug()
{
echo 'Current Page: '.$this->page_current.'<br>';
echo 'Limit: '.$this->limit.'<br>';
echo 'Found: '.$this->found.'<br>';
echo 'Start:'.$this->start.'<br>';
echo 'Counter:'.$this->counter.'<br>';
}
}
?>