<?php
class Page{
//variables set in constructor
private $limit; //limit per page
private $total_numbers; //limit of numbers displayed in navigation (must be odd number)
private $total_records; //total records in query
private $pages; //number of pages
private $page; //actual page
private $previous_page; //previous page
private $next_page; //next page
private $last_page; //last page
private $start; //start page in query
private $end; //end page in query
private $i; //first number in navigation
private $navigation = array(); //navigation
public function __construct($total_records, $limit = "5")
{
$this->limit = $limit;
$this->total_records = $total_records;
$pages = $total_records/$limit;
$zv = $total_records % $limit;
if ($zv>0)
$pages++;
$this->pages = $pages;
}
//calculate actual, previous and next page
public function actualPage($url = "page")
{
if(isset($_GET[$url]))
$this->page = $_GET[$url];
else
$this->page = "1";
$this->previous_page = $this->page - 1;
$this->next_page = $this->page + 1;
$this->limits();
}
//calculate the limits
private function limits()
{
if ($this->previous_page < 1)
{
$this->previous_page = 1;
$this->page = 1;
}
$this->start = $this->page * $this->limit - $this->limit;
$this->end = $this->start + $this->limit;
}
//return true if it is first page or false when its not
public function firstPage()
{
if ($this->page == "1")
return true;
else
return false;
}
//return true if this page is last or false when its not
public function lastPage()
{
if (($this->end >= $this->total_records))
{
$this->last_page = true;
return true;
}
else
{
$this->last_page = false;
return false;
}
}
//set the variable i, its the start number in navigation
public function numbers($total_numbers = "5")
{
$this->total_numbers = $total_numbers;
$half = ($this->total_numbers/2)-0.5;
$this->i=$this->page - $half;
if ($this->i < 1)
$this->i = 1;
while (($this->i + $this->total_numbers) > $this->pages +1)
$this->i--;
$this->navigation();
}
//calculate navigation to the navigation array
private function navigation()
{
for ($j="0"; $j < $this->total_numbers; $j++)
{
$temp = $this->i + $j;
if ($temp >= "1")
{
if ($this->page == $temp)
{
$this->navigation[$temp] = true;
}
else
{
$this->navigation[$temp] = false;
}
}
}
}
public function getStart()
{
return $this->start;
}
public function getLimit()
{
return $this->limit;
}
public function getPage()
{
return $this->page;
}
public function getPreviousPage()
{
return $this->previous_page;
}
public function getNextPage()
{
return $this->next_page;
}
public function getNavigation()
{
return $this->navigation;
}
}
?>
<?php
/*
Example (with usage of mysql class):
$book -> execute("select * from test");
$total_records = $book->total_row();
$page = new Page($total_records);
$page->actualPage();
$start = $page->getStart();
$limit = $page->getLimit();
$book->execute("select * from test order by id desc limit $start, $limit");
$page->numbers();
////navigation////
echo "<table class='nav'><tr>";
if (!$page->firstPage())
echo "<td class='left'><a href='guestbook/".$page->getPreviousPage()."/'><< predošla</a></td>\r\n";
else
echo "<td class='left un'><< predošla</td>\r\n";
echo "<td>";
$data = $page->getNavigation();
foreach ($data as $a)
{
if ($a)
{
echo "<b>".key($data)."</b>";
}
else
{
echo "<a href='guestbook/".key($data)."/'>".key($data)."</a>";
}
echo " ";
next($data);
}
echo "</td>";
if (!$page->lastPage())
echo "<td class='right'><a href='guestbook/".$page->getNextPage()."/'>ďalšia >></a></td>\r\n";
else
echo "<td class='right un'>ďalšia >></td>\r\n"."\r\n";
echo "</table>";
/////////////////////////////
*/
?>