-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpaginacao.php
248 lines (217 loc) · 7.92 KB
/
paginacao.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
* PHPSense Pagination Class
*
* PHP tutorials and scripts
*
* @package PHPSense
* @author Jatinder Singh Thind
* @copyright Copyright (c) 2006, Jatinder Singh Thind
* @link http://www.phpsense.com
* @modification Mike Dalisay
* @link https://www.codeofaninja.com
*/
// ------------------------------------------------------------------------
class Paginacao {
private $php_self;
private $rows_per_page = 10; //Number of records to display per page
private $total_rows = 0; //Total number of rows returned by the query
private $links_per_page = 15; //Number of links to display per page
private $append = ""; //Paremeters to append to pagination links
private $sql = "";
private $debug = false;
private $conn = false;
private $page = 1;
private $max_pages = 0;
private $offset = 0;
/**
* Constructor
*
* @param resource $connection Mysql connection link
* @param string $sql SQL query to paginate. Example : SELECT * FROM users
* @param integer $rows_per_page Number of records to display per page. Defaults to 10
* @param integer $links_per_page Number of links to display per page. Defaults to 5
* @param string $append Parameters to be appended to pagination links
*/
function __construct($connection, $sql, $rows_per_page = 10, $links_per_page = 15, $append = "") {
$this->conn = $connection;
$this->sql = $sql;
$this->rows_per_page = (int)$rows_per_page;
if (intval($links_per_page ) > 0) {
$this->links_per_page = (int)$links_per_page;
} else {
$this->links_per_page = 5;
}
$this->append = $append;
$this->php_self = htmlspecialchars($_SERVER['PHP_SELF'] );
if (isset($_GET['page'] )) {
$this->page = intval($_GET['page'] );
}
}
/**
* Executes the SQL query and initializes internal variables
*
* @access public
* @return resource
*/
function paginate() {
//Check for valid mysql connection
//Find total number of rows
$all_rs = $this->conn->prepare( $this->sql );
$all_rs->execute();
if (! $all_rs) {
if ($this->debug)
echo "SQL query failed. Check your query.<br /><br />Error Returned: " . mysql_error();
return false;
}
$this->total_rows = $all_rs->rowCount();
//Return FALSE if no rows found
if ($this->total_rows == 0) {
if ($this->debug)
echo "Query returned zero rows.";
return FALSE;
}
//Max number of pages
$this->max_pages = ceil($this->total_rows / $this->rows_per_page );
if ($this->links_per_page > $this->max_pages) {
$this->links_per_page = $this->max_pages;
}
//Check the page value just in case someone is trying to input an aribitrary value
if ($this->page > $this->max_pages || $this->page <= 0) {
$this->page = 1;
}
//Calculate Offset
$this->offset = $this->rows_per_page * ($this->page - 1);
//Fetch the required result set
$query = $this->sql . " LIMIT {$this->rows_per_page} OFFSET {$this->offset}";
$rs = $this->conn->prepare( $query );
$rs->execute();
if (! $rs) {
if ($this->debug)
echo "Pagination query failed. Check your query.<br /><br />Error Returned: " . mysql_error();
return false;
}
return $rs;
}
/**
* Display the link to the first page
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to 'First'
* @return string
*/
function renderFirst($tag = 'Primeira') {
if ($this->total_rows == 0)
return FALSE;
if ($this->page == 1) {
return "$tag ";
} else {
//return '<a href="' . $this->php_self . '?page=1&' . $this->append . '">' . $tag . '</a> ';
//default to one (1)
return " <a href='javascript:void(0);' OnClick='getdata( 1 )' title='Primeira'>$tag</a> ";
}
}
/**
* Display the link to the last page
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to 'Last'
* @return string
*/
function renderLast($tag = 'Última') {
if ($this->total_rows == 0)
return FALSE;
if ($this->page == $this->max_pages) {
return $tag;
} else {
//return ' <a href="' . $this->php_self . '?page=' . $this->max_pages . '&' . $this->append . '">' . $tag . '</a>';
$pageno = $this->max_pages;
return " <a href='javascript:void(0);' OnClick='getdata( $pageno )' title='Última'>$tag</a> ";
}
}
/**
* Display the next link
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to '>>'
* @return string
*/
function renderNext($tag = '>>') {
if ($this->total_rows == 0)
return FALSE;
if ($this->page < $this->max_pages) {
//return '<a href="' . $this->php_self . '?page=' . ($this->page + 1) . '&' . $this->append . '" title=\'next to\'>' . $tag . '</a>';
$pageno = $this->page + 1;
return " <a href='javascript:void(0);' OnClick='getdata( $pageno )' title='Próxima'>$tag</a> ";
} else {
return $tag;
}
}
/**
* Display the previous link
*
* @access public
* @param string $tag Text string to be displayed as the link. Defaults to '<<'
* @return string
*/
function renderPrev($tag = '<<') {
if ($this->total_rows == 0)
return FALSE;
if ($this->page > 1) {
//return ' <a href="' . $this->php_self . '?page=' . ($this->page - 1) . '&' . $this->append . '">' . $tag . '</a>';
$pageno = $this->page - 1;
return " <a href='javascript:void(0);' OnClick='getdata( $pageno )' title='Anterior'>$tag</a> ";
} else {
return " $tag ";
}
}
/**
* Display the page links
*
* @access public
* @return string
*/
function renderNav($prefix = '<span class="page_link">', $suffix = '</span>') {
if ($this->total_rows == 0)
return FALSE;
$batch = ceil($this->page / $this->links_per_page );
$end = $batch * $this->links_per_page;
if ($end == $this->page) {
//$end = $end + $this->links_per_page - 1;
//$end = $end + ceil($this->links_per_page/2);
}
if ($end > $this->max_pages) {
$end = $this->max_pages;
}
$start = $end - $this->links_per_page + 1;
$links = '';
for($i = $start; $i <= $end; $i ++) {
if ($i == $this->page) {
$links .= $prefix . " $i " . $suffix;
} else {
$links .= " <a href='javascript:void(0);' OnClick='getdata( $i )' title='Outra página'>$i</a> ";
}
}
return $links;
}
/**
* Display full pagination navigation
*
* @access public
* @return string
*/
function renderFullNav() {
//echo $this->renderFirst() . " " . $this->renderPrev();
return $this->renderFirst() . ' ' . $this->renderPrev() . ' ' . $this->renderNav() . ' ' . $this->renderNext() . ' ' . $this->renderLast();
}
/**
* Set debug mode
*
* @access public
* @param bool $debug Set to TRUE to enable debug messages
* @return void
*/
function setDebug($debug) {
$this->debug = $debug;
}
}