-
Notifications
You must be signed in to change notification settings - Fork 2
/
pagination.php
213 lines (190 loc) · 6.71 KB
/
pagination.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
207
208
209
210
211
212
213
<?php
/**
* The Flexible PHP pagination class was designed to be the easiest to use PHP Pagination system without losing any customization options.
*/
class Pagination
{
public $max, $total, $value, $start = 0;
public $page = 1;
public $max_items = 10;
public $url, $theme = '../themes/default';
public $i = 0;
/**
* Class Constructor, Accepts 3 parameters. The minimum that any pagination requires.
* @param int $max maximum amount of results per page
* @param int $total total number of results in data source
* @param int $page The value used to determine what page we're currently on
* @param int $max_items If set to 7 then a maximum of 7 page numbers are shown. The previous 3 pages, the current page and the next 3 pages shown. (Set to false for all page numbers)
*/
public function __construct($max, $total, $page = 1, $max_items = 10)
{
$this->max = $max;
$this->total = $total;
$this->max_items = $max_items;
# Set the page as a number, bypassing GET
$this->page = (!empty($page) && ($page <= $this->pages()) && is_numeric($page) && $page>0 ) ? $page : 1;
}
/**
* A pre-formatted string of html to display general pagination links. Use this if you don't care too much about
* The html includes links to first, last, next, previous and numbered pages.
* @param int $url the url to be used in the links
* @return string HTML
*/
public function get_html($theme_file=null)
{
if($theme_file != null)
{
$this->theme = $theme_file;
}
$theme = include $this->theme.'.php';
$links = $theme['pre'];
$links .= $this->first($theme['first'][0], $theme['first'][1]);
$links .= $this->previous($theme['previous'][0], $theme['previous'][1]);
$links .= $this->numbers($theme['numbers'][0], $theme['numbers'][1]);
$links .= $this->next($theme['next'][0], $theme['next'][1]);
$links .= $this->last($theme['last'][0], $theme['last'][1]);
$links .= $theme['post'];
return $links;
}
/**
* This calculates the start of our result set, based on our current page
* @return int Final Calculation of where our result set should begin
*/
public function start()
{
# Computers Calculate From 0 thus, page1 must start results from 0
$start = abs($this->page - 1);
# Calculate Our Starting Point (0x6=0(start from 0, page1), 1x6=6(start from 6, page2), etc)
$calc = $start*$this->max;
# return our final outcome
return $calc;
}
/**
* This calculates the end of our result set, based on our current page
* @return int Final Calculation of where our result set should end
*/
public function end()
{
# Calculate the Beginning + The maximum amount of results
$calc = $this->start() + $this->max;
# Only return this if it is not above the total otherwise return our maximum
# example, 24 + 6 = 30, but with only 26 reselts it will display the total results istead (26)
$r = ($calc > $this->total) ? $this->total : $calc;
# return the result
return $r;
}
/**
* This calculates the total pages in our result set
* @return int return Rounds Up the total results / maximum per page
*/
public function pages()
{
return ceil($this->total/$this->max);
}
/**
* Based on which page you are this returns informations like, start result, end result, total results, current page, total pages
* @param string $html The HTML you wish to use to display the link
* @return mixed return information we may need to display
*/
public function info($html)
{
$tags = array('{total}', '{start}', '{end}', '{page}', '{pages}');
$code = array($this->total, $this->start()+1, $this->end(), $this->page, $this->pages());
return str_replace($tags, $code, $html);
}
/**
* This shows the 'first' link with custom html
* @param string $html The HTML you wish to use to display the link
* @return string The Same HTML replaced the tags with the proper number
*/
public function first($html, $html2='')
{
# Only show if you are not on page 1, otherwise show HTML2
$value = ($this->page != 1) ? $html : $html2;
return str_replace(array('{nr}','{url}'), array(1,$this->url), $value);
}
/**
* This shows the 'previous' link with custom html
* @param string $html The HTML you wish to use to display the link
* @return string The Same HTML replaced the tags with the proper number
*/
public function previous($html, $html2='')
{
# Only show if you are not on page 1, otherwise show HTML2
$value = ($this->page != 1) ? $html : $html2;
return str_replace(array('{nr}','{url}'), array($this->page-1,$this->url), $value);
}
/**
* This shows the 'next' link with custom html
* @param string $html The HTML you wish to use to display the link
* @return string The Same HTML replaced the tags with the proper number
*/
public function next($html, $html2='')
{
# Only show if you are not on the last page
$value = ($this->page < $this->pages()) ? $html : $html2;
return str_replace(array('{nr}','{url}'), array($this->page+1,$this->url), $value);
}
/**
* This shows the 'last' link with custom html
* @param string $html The HTML you wish to use to display the link
* @return string The Same HTML replaced the tags with the proper number
*/
public function last($html, $html2='')
{
# Only show if you are not on the last page
$value = ($this->page < $this->pages()) ? $html : $html2;
return str_replace(array('{nr}','{url}'), array($this->pages(),$this->url), $value);
}
/**
* This shows an loop of 'numbers' with their appropriate link in custom html
* @param string $link The HTML to display a number with a link
* @param string $current The HTML to display a the current page number without a link
* @param string $reversed Optional Parameter, set to true if you want the numbers reversed (align to right for designs)
* @return string The Same HTML replaced the tags with the proper numbers and links
*/
public function numbers($link, $current, $reversed=false)
{
$r = '';
$range = floor(($this->max_items-1)/2);
if (!$this->max_items)
{
$page_nums = range(1, $this->pages());
}
else
{
$lower_limit = max($this->page - $range, 1);
$upper_limit = min($this->pages(), $this->page + $range);
$page_nums = range($lower_limit, $upper_limit);
}
if($reversed)
{
$page_nums = array_reverse($page_nums);
}
foreach ($page_nums as $i)
{
$tags = array('{nr}','{url}');
$code = array($i,$this->url);
if($this->page == $i)
{
$r .= str_replace($tags, $code, $current);
}
else
{
$r .= str_replace($tags, $code, $link);
}
}
return $r;
}
/**
* This function allows you to limit the loop without using a limit inside another query. Or if you are using arrays / xml.
*/
public function paginator()
{
$this->i = $this->i+1;
if( $this->i > $this->start() && $this->i <= $this->end() )
{
return true;
}
}
}