3 * Handles returning arrays based on a given pagenumber.
4 * By specifing a $_GET['pagenum'] or if not(page = 1 will be used) a few elements from a specific query will be returned. Not all elements have to be loaded into objects, only
5 * the elements needed for that specific page, this is a good thing performance wise. This is done by passign the query to the constructor and specifying how many you want to display.
6 * @author Daan Janssens, mentored by Matthew Lagoe
10 private $element_array; /**< Array containing the elements that are extracted for that specific page number */
11 private $last; /**< The last page number */
12 private $current; /**< The current page number (read from $_GET['pagenum']) */
13 private $amountOfRows; /**< Total amount of rows that a query would return (if no limits would be used) */
17 * will fetch the correct elements that match to a specific page (specified by the $_GET['pagenum'] variable). The query has to be passed as a string to the function
18 * that way it will only load the specific elements that are related to the pagenumber. The $params, parameter is optional and is used to pass the parameters for the query.
19 * The result class will be used to instantiate the found elements with, their set() function will be called. The class its getters can be later used to get the info out of the object.
20 * @param $query the query to be paginated
21 * @param $db the db on which the query should be performed
22 * @param $nrDisplayed the amount of elements that should be displayed /page
23 * @param $resultClass the elements that should be returned should be of that specific class.
24 * @param $params the parameters used by the query (optional)
26 function __construct($query, $db, $nrDisplayed, $resultClass, $params = array()) {
27 if (!(isset($_GET['pagenum']))){
30 $this->current
= $_GET['pagenum'];
33 //Here we count the number of results
34 $db = new DBLayer($db);
35 $rows = $db->execute($query, $params)->rowCount();
36 $this->amountOfRows
= $rows;
37 //the array hat will contain all users
40 //This is the number of results displayed per page
41 $page_rows = $nrDisplayed;
43 //This tells us the page number of our last page
44 $this->last
= ceil($rows/$page_rows);
46 //this makes sure the page number isn't below one, or more than our maximum pages
47 if ($this->current
< 1)
50 }else if ($this->current
> $this->last
) {
51 $this->current
= $this->last
;
54 //This sets the range to display in our query
55 $max = 'limit ' .($this->current
- 1) * $page_rows .',' .$page_rows;
57 //This is your query again, the same one... the only difference is we add $max into it
58 $data = $db->execute($query . " " . $max, $params);
60 $this->element_array
= Array();
61 //This is where we put the results in a resultArray to be sent to smarty
62 while($row = $data->fetch(PDO
::FETCH_ASSOC
)){
63 $element = new $resultClass();
65 $this->element_array
[] = $element;
72 * return the number of the 'last' object attribute
73 * @return the number of the last page
75 public function getLast(){
81 * return the number of the 'current' object attribute
82 * @return the number of the current page
84 public function getCurrent(){
85 return $this->current
;
90 * return the elements array of the object
91 * @return the elements of a specific page (these are instantiations of the class passed as parameter ($resultClass) to the constructor)
93 public function getElements(){
94 return $this->element_array
;
99 * return total amount of rows for the original query
100 * @return the total amount of rows for the original query
102 public function getAmountOfRows(){
103 return $this->amountOfRows
;
107 * return the page links.
108 * (for browsing the pages, placed under a table for example) the $nrOfLinks parameter specifies the amount of links you want to return.
109 * it will show the links closest to the current page on both sides (in case one side can't show more, it will show more on the other side)
110 * @return an array of integerswhich refer to the clickable pagenumbers for browsing other pages.
112 public function getLinks($nrOfLinks){
113 $pageLinks = Array();
114 //if amount of showable links is greater than the amount of pages: show all!
115 if ($this->last
<= $nrOfLinks){
116 for($var = 1; $var <= $this->last
; $var++
){
120 $offset = ($nrOfLinks-1)/2 ;
121 $startpoint = $this->current
- $offset;
122 $endpoint = $this->current +
$offset;
126 $endpoint = $startpoint +
$nrOfLinks - 1;
127 }else if($endpoint > $this->last
){
128 $endpoint = $this->last
;
129 $startpoint = $endpoint - ($nrOfLinks -1);
132 for($var = $startpoint; $var <= $endpoint; $var++
){