2013-08-04 18:27:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Pagination{
|
|
|
|
|
|
|
|
private $element_array;
|
|
|
|
private $last;
|
|
|
|
private $current;
|
2013-08-05 03:27:19 +00:00
|
|
|
private $amountOfRows;
|
2013-08-04 18:27:07 +00:00
|
|
|
|
2013-08-05 20:35:22 +00:00
|
|
|
function __construct($query, $db, $nrDisplayed, $resultClass, $params = array()) {
|
2013-08-04 18:27:07 +00:00
|
|
|
if (!(isset($_GET['pagenum']))){
|
|
|
|
$this->current= 1;
|
|
|
|
}else{
|
|
|
|
$this->current= $_GET['pagenum'];
|
|
|
|
}
|
|
|
|
|
|
|
|
//Here we count the number of results
|
|
|
|
$db = new DBLayer($db);
|
2013-08-05 20:35:22 +00:00
|
|
|
$rows = $db->execute($query, $params)->rowCount();
|
2013-08-05 03:27:19 +00:00
|
|
|
$this->amountOfRows = $rows;
|
2013-08-04 18:27:07 +00:00
|
|
|
//the array hat will contain all users
|
|
|
|
|
|
|
|
if($rows > 0){
|
|
|
|
//This is the number of results displayed per page
|
|
|
|
$page_rows = $nrDisplayed;
|
|
|
|
|
|
|
|
//This tells us the page number of our last page
|
|
|
|
$this->last = ceil($rows/$page_rows);
|
|
|
|
|
|
|
|
//this makes sure the page number isn't below one, or more than our maximum pages
|
|
|
|
if ($this->current< 1)
|
|
|
|
{
|
|
|
|
$this->current= 1;
|
|
|
|
}else if ($this->current> $this->last) {
|
|
|
|
$this->current= $this->last;
|
|
|
|
}
|
|
|
|
|
|
|
|
//This sets the range to display in our query
|
|
|
|
$max = 'limit ' .($this->current- 1) * $page_rows .',' .$page_rows;
|
|
|
|
|
|
|
|
//This is your query again, the same one... the only difference is we add $max into it
|
2013-08-05 20:35:22 +00:00
|
|
|
$data = $db->execute($query . " " . $max, $params);
|
2013-08-04 18:27:07 +00:00
|
|
|
|
|
|
|
$this->element_array = Array();
|
|
|
|
//This is where we put the results in a resultArray to be sent to smarty
|
|
|
|
while($row = $data->fetch(PDO::FETCH_ASSOC)){
|
|
|
|
$element = new $resultClass();
|
2013-08-05 02:37:20 +00:00
|
|
|
$element->set($row);
|
2013-08-04 18:27:07 +00:00
|
|
|
$this->element_array[] = $element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-05 02:37:20 +00:00
|
|
|
public function getLast(){
|
2013-08-04 18:27:07 +00:00
|
|
|
return $this->last;
|
|
|
|
}
|
|
|
|
|
2013-08-05 03:27:19 +00:00
|
|
|
public function getCurrent(){
|
|
|
|
return $this->current;
|
|
|
|
}
|
2013-08-04 18:27:07 +00:00
|
|
|
|
2013-08-05 02:37:20 +00:00
|
|
|
public function getElements(){
|
2013-08-04 18:27:07 +00:00
|
|
|
return $this->element_array;
|
|
|
|
}
|
|
|
|
|
2013-08-05 03:27:19 +00:00
|
|
|
public function getAmountOfRows(){
|
|
|
|
return $this->amountOfRows;
|
|
|
|
}
|
2013-08-04 18:27:07 +00:00
|
|
|
|
2013-08-05 02:37:20 +00:00
|
|
|
public function getLinks($nrOfLinks){
|
2013-08-04 18:27:07 +00:00
|
|
|
$pageLinks = Array();
|
2013-08-05 02:37:20 +00:00
|
|
|
//if amount of showable links is greater than the amount of pages: show all!
|
2013-08-04 18:27:07 +00:00
|
|
|
if ($this->last <= $nrOfLinks){
|
2013-08-05 03:27:19 +00:00
|
|
|
for($var = 1; $var <= $this->last; $var++){
|
2013-08-04 18:27:07 +00:00
|
|
|
$pageLinks[] = $var;
|
|
|
|
}
|
|
|
|
}else{
|
2013-08-05 03:27:19 +00:00
|
|
|
$offset = ($nrOfLinks-1)/2 ;
|
2013-08-04 18:27:07 +00:00
|
|
|
$startpoint = $this->current - $offset;
|
|
|
|
$endpoint = $this->current + $offset;
|
2013-08-05 03:27:19 +00:00
|
|
|
|
|
|
|
if($startpoint < 1){
|
|
|
|
$startpoint = 1;
|
|
|
|
$endpoint = $startpoint + $nrOfLinks - 1;
|
|
|
|
}else if($endpoint > $this->last){
|
|
|
|
$endpoint = $this->last;
|
|
|
|
$startpoint = $endpoint - ($nrOfLinks -1);
|
2013-08-04 18:27:07 +00:00
|
|
|
}
|
2013-08-05 03:27:19 +00:00
|
|
|
|
2013-08-04 18:27:07 +00:00
|
|
|
for($var = $startpoint; $var <= $endpoint; $var++){
|
|
|
|
$pageLinks[] = $var;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $pageLinks;
|
|
|
|
}
|
|
|
|
}
|