forked from lotsofcode/php-array-pagination
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagination.class.php
206 lines (186 loc) · 6.57 KB
/
pagination.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/************************************************************\
*
* PHP Array Pagination Copyright 2007 - Derek Harvey
* www.lotsofcode.com
*
* This file is part of PHP Array Pagination .
*
* PHP Array Pagination is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* PHP Array Pagination is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PHP Array Pagination ; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
\************************************************************/
class pagination
{
/**
* Properties array
* @var array
* @access private
*/
private $_properties = array();
/**
* Default configurations
* @var array
* @access public
*/
public $_defaults = array(
'page' => 1,
'perPage' => 10
);
/**
* Constructor
*
* @param array $array Array of results to be paginated
* @param int $curPage The current page interger that should used
* @param int $perPage The amount of items that should be show per page
* @return void
* @access public
*/
public function __construct($array, $curPage = null, $perPage = null)
{
$this->array = $array;
$this->curPage = ($curPage == null ? $this->defaults['page'] : $curPage);
$this->perPage = ($perPage == null ? $this->defaults['perPage'] : $perPage);
}
/**
* Global setter
*
* Utilises the properties array
*
* @param string $name The name of the property to set
* @param string $value The value that the property is assigned
* @return void
* @access public
*/
public function __set($name, $value)
{
$this->_properties[$name] = $value;
}
/**
* Global getter
*
* Takes a param from the properties array if it exists
*
* @param string $name The name of the property to get
* @return mixed Either the property from the internal
* properties array or false if isn't set
* @access public
*/
public function __get($name)
{
if (array_key_exists($name, $this->_properties)) {
return $this->_properties[$name];
}
return false;
}
/**
* Set the show first and last configuration
*
* This will enable the "<< first" and "last >>" style
* links
*
* @param boolean $showFirstAndLast True to show, false to hide.
* @return void
* @access public
*/
public function setShowFirstAndLast($showFirstAndLast)
{
$this->_showFirstAndLast = $showFirstAndLast;
}
/**
* Set the main seperator character
*
* By default this will implode an empty string
*
* @param string $mainSeperator The seperator between the page numbers
* @return void
* @access public
*/
public function setMainSeperator($mainSeperator)
{
$this->mainSeperator = $mainSeperator;
}
/**
* Get the result portion from the provided array
*
* @return array Reduced array with correct calculated offset
* @access public
*/
public function getResults()
{
// Assign the page variable
if (empty($this->curPage) !== false) {
$this->page = $this->curPage; // using the get method
} else {
$this->page = 1; // if we don't have a page number then assume we are on the first page
}
// Take the length of the array
$this->length = count($this->array);
// Get the number of pages
$this->pages = ceil($this->length / $this->perPage);
// Calculate the starting point
$this->start = ceil(($this->page - 1) * $this->perPage);
// return the portion of results
return array_slice($this->array, $this->start, $this->perPage);
}
/**
* Get the html links for the generated page offset
*
* @param array $params A list of parameters (probably get/post) to
* pass around with each request
* @return mixed Return description (if any) ...
* @access public
*/
public function getLinks($params = array())
{
// Initiate the links array
$plinks = array();
$links = array();
$slinks = array();
// Concatenate the get variables to add to the page numbering string
$queryUrl = '';
if (!empty($params) === true) {
unset($params['page']);
$queryUrl = '&'.http_build_query($params);
}
// If we have more then one pages
if (($this->pages) > 1) {
// Assign the 'previous page' link into the array if we are not on the first page
if ($this->page != 1) {
if ($this->_showFirstAndLast) {
$plinks[] = ' <a href="?page=1'.$queryUrl.'">«« First </a> ';
}
$plinks[] = ' <a href="?page='.($this->page - 1).$queryUrl.'">« Prev</a> ';
}
// Assign all the page numbers & links to the array
for ($j = 1; $j < ($this->pages + 1); $j++) {
if ($this->page == $j) {
$links[] = ' <a class="selected">'.$j.'</a> '; // If we are on the same page as the current item
} else {
$links[] = ' <a href="?page='.$j.$queryUrl.'">'.$j.'</a> '; // add the link to the array
}
}
// Assign the 'next page' if we are not on the last page
if ($this->page < $this->pages) {
$slinks[] = ' <a href="?page='.($this->page + 1).$queryUrl.'"> Next » </a> ';
if ($this->_showFirstAndLast) {
$slinks[] = ' <a href="?page='.($this->pages).$queryUrl.'"> Last »» </a> ';
}
}
// Push the array into a string using any some glue
return implode(' ', $plinks).implode($this->mainSeperator, $links).implode(' ', $slinks);
}
return;
}
}