Paginanummering Klasse
gekregen kritiek te luisteren, maar dat lukt nog niet helemaal.
Ik heb m'n klasse nu opgesplitst in 3 klassen, echter doet dat ding nu weer 's helemaal niks. Hij geeft zelfs geen fout...
Ik heb al wat dingen geprobeerd om erachter te komen waar iets fout moet zitten. Maar volgens die methodes moet overal een fout zitten? :-S
Iemand een idee wat er aan de hand zou kunnen zijn?
Ik ben bezig naar de Ik heb m'n klasse nu opgesplitst in 3 klassen, echter doet dat ding nu weer 's helemaal niks. Hij geeft zelfs geen fout...
Ik heb al wat dingen geprobeerd om erachter te komen waar iets fout moet zitten. Maar volgens die methodes moet overal een fout zitten? :-S
Code (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors');
// <title>Navigatie Klasse</title>
include("phphulp/config.php");
class Navigation {
// current page, total pages and pagination pages amount.
protected $iCurrent;
protected $iTotal;
// limitation vars
protected $iStart;
protected $iShowRecords;
protected $iShowNumbers;
// query vars by using setQuery and setQueryCountField()
protected $sCountQuery;
protected $sQueryCountField = 'aantal';
// query vars by using setQueryTable and setQueryWhereStatement()
protected $sTable;
protected $sWhereStatement;
// parameter var
protected $sGetParameter;
// output vars
protected $aPagination = array();
protected $aToStringReplacements = array();
protected $aWords = array('first'=>'First','prev'=>'Previous','next'=>'Next','last'=>'Last');
protected $sToStringRegex;
protected $sSpace = '...';
// construction
public function __construct($iCurrent, $iShowRecords = 20, $iShowNumbers = 6) {
$this->iCurrent = ($iCurrent >= 0) ? (int)$iCurrent : 0;
$this->iShowRecords = ($iShowRecords > 0) ? (int)$iShowRecords : 20;
$this->iShowNumbers = ($iShowRecords > 0 && $iShowRecords % 2 == 0) ? (int)$iShowNumbers : 6;
}
// set style for the toString function.
public function setToStringRegex($sRegex, $aReplacements) {
$this->sToStringRegex = (string)trim($sRegex);
$this->aToStringReplacements = $aReplacements;
}
// set $_GET parameter
public function setGetParameter($sString) {
$this->sGetParameter = (string)trim($sString);
}
// set ...
public function setSpace($sString) {
$this->sSpace = (string)trim($sString);
}
// set words, first, next, previous, last.
public function setWord($sEnglishWord, $sWord) {
if($sEnglishWord == 'first') {
$sArrayKey = 'first';
} else {
$sArrayKey = substr($sWord, 0, 4);
}
}
// fill array and vars
public function do() {
if(empty($this->sCountQuery)) {
if(!empty($this->sTable)) {
$this->createQuery();
} else {
throw new Exception('First enter a table to be searched in.');
}
}
$this->calculateTotalPages();
$this->setNavigation();
}
// set next page in the pagination array.
protected function setNext() {
if($this->iCurrent == $this->iTotal) {
$this->aPagination['next'] = false;
} else {
$this->aPagination['next'] = $this->iCurrent+1;
}
}
// set last page in the pagination array.
protected function setLast() {
if($this->iCurrent == $this->iTotal) {
$this->aPagination['last'] = false;
} else {
$this->aPagination['last'] = $this->iTotal;
}
}
// set previous page in the pagination array.
protected function setPrevious() {
if($this->iCurrent == 0) {
$this->aPagination['prev'] = false;
} else {
$this->aPagination['prev'] = $this->iCurrent-1;
}
}
// set first page in the pagination array.
protected function setFirst() {
if($this->iCurrent == 0) {
$this->aPagination['first'] = false;
} else {
$this->aPagination['first'] = 0;
}
}
// set points in the pagination array.
protected function setSpaces() {
$iNumbersRound = ceil($this->iShowNumbers / 2);
if(($this->iCurrent - $iNumbersRound) <= 0) {
$this->aPagination['space_before'] = false;
} else {
$this->aPagination['space_before'] = $this->sSpace;
}
if(($this->iCurrent + $iNumbersRound) >= $this->iTotal) {
$this->aPagination['space_after'] = false;
} else {
$this->aPagination['space_after'] = $this->sSpace;
}
}
// set the pagination numbers in the pagination array.
protected function setNumbers() {
$iNumbersRound = ceil($this->iShowNumbers / 2);
$iStart = ($this->iCurrent - $iNumbersRound) <= 0 ? 0 : ($this->iCurrent - $iNumbersRound);
$iEnd = ($this->iCurrent + $iNumbersRound) >= $this->iTotal ? $this->iTotal : ($this->iCurrent + $iNumbersRound);
for($i=$iStart;$i<=$iEnd;$i++) {
if($i == $this->iCurrent) $this->aPagination['current'] = $i;
$this->aPagination['numbers'][] = $i;
}
}
// set everything by using the six functions above.
protected function setNavigation() {
$this->setFirst();
$this->setLast();
$this->setNext();
$this->setPrevious();
$this->setSpaces();
$this->setNumbers();
}
// zip everything to a string, can be used by the inexperienced programmer.
// there is also an expert version; use the stringReplacement methods to set up your own style.
public function __toString() {
if(!empty($this->sToStringRegex)) {
$sString = $this->sToStringRegex;
foreach($this->aToStringReplacements as $sRegex => $sReplace) {
$sString = str_replace($sRegex, $sReplace, $sString);
}
} else {
$aPags = NavigationOutput::getNavigation();
$sString = '';
if(is_bool(NavigationOutput::getFirst()))
$sString .= $aWords['first'];
else
$sString .= '<a href="?'.$this->sGetParameter.'='.$aPags['first'].'">'.$aWords['first'].'</a> | ';
if(is_bool(NavigationOutput::getPrevious()))
$sString .= $aWords['prev'];
else
$sString .= '<a href="?'.$this->sGetParameter.'='.$aPags['prev'].'">'.$aWords['prev'].'</a> | ';
$sString .= NavigationOutput::getSpaceBefore();
foreach($aPags['numbers'] as $iNum) {
if($iNum == NavigationOutput::getCurrent()) {
$sString .= ' ['.($iNum+1).'] ';
} else {
$sString .= ' <a href="?'.$this->sGetParameter.'='.$iNum.'">'.($iNum+1).'</a> ';
}
}
$sString .= NavigationOutput::getSpaceAfter();
if(is_bool(NavigationOutput::getNext()))
$sString .= $aWords['next'];
else
$sString .= ' | <a href="?'.$this->sGetParameter.'='.$aPags['next'].'">'.$aWords['next'].'</a>';
if(is_bool(NavigationOutput::getLast()))
$sString .= $aWords['last'];
else
$sString .= ' | <a href="?'.$this->sGetParameter.'='.$aPags['last'].'">'.$aWords['last'].'</a>';
}
return $sString;
}
}
class NavigationDatabase extends Navigation {
// only use setQuery() and setQueryCountField()...
static public function setQuery($sQuery) {
parent::sCountQuery = (string)trim($sQuery);
}
// set fieldname which contains the query count result, standard value: aantal.
static public function setQueryCountField($sField) {
parent::sQueryCountField = (string)trim($sField);
}
// ...or setQueryTable() and setQueryWhereStatement()
static public function setQueryTable($sTable) {
parent::sTable = (string)trim($sTable);
}
// set where statement for the query to be created with createQuery. If there's anything elses like JOINs used in the query, please use setQuery instead of these, cause setQuery is much more flexible to use with that kinds of queries.
static public function setQueryWhereStatement($sWhere) {
parent::sWhereStatement = (string)trim($sWhere);
}
// only execute when setQuery() isn't used.
static protected function createQuery() {
parent::sCountQuery = "SELECT COUNT(*) AS ".parent::sQueryCountField." FROM ".parent::sTable;
if(!empty($this->sWhereStatement)) {
parent::sCountQuery .= " WHERE ".parent::sWhereStatement;
}
}
// calculate total pages amount.
static protected function calculateTotalPages() {
$sResult = mysql_query(parent::sCountQuery);
if($sResult) {
if(mysql_num_rows($sResult) > 0) {
$sRij = mysql_fetch_assoc($sResult);
parent::iTotal = ceil($sRij[parent::sQueryCountField]/parent::iShowRecords)-1;
} else {
parent::iTotal = 0;
}
} else {
parent::iTotal = 0;
}
}
// return the array, if everything completes, containing the results of the query of the showed page.
static public function getQueryResult($sQuery) {
$sResult = mysql_query($sQuery);
if($sResult) {
$aReturnArray = array();
if(mysql_num_rows($sResult) > 0) {
while($sRij = mysql_fetch_assoc($sResult)) {
$aReturnArray[] = $sRij;
}
}
} else {
throw new Exception(mysql_error().' in query: '.$sQuery);
}
return $aReturnArray;
}
// when you create the query by your own, but not knowing how to use the LIMIT-statement, we make it for you.
static public function getLimit() {
return ' LIMIT '.parent::iStart.','.parent::iShowRecords;
}
}
class NavigationOutput extends Navigation {
// get the pagination array. this can also be used by the user of this class.
static public function getNavigation() {
return parent::aPagination;
}
// get next page in the array.
static public function getNext() {
return parent::aPagination['next'];
}
// get last page in the array.
static public function getLast() {
return parent::aPagination['last'];
}
// get previous page in the array.
static public function getPrevious() {
return parent::aPagination['prev'];
}
// get first page in the array.
static public function getFirst() {
return parent::aPagination['first'];
}
// get the pagination numbers in the array.
static public function getNumbers() {
return parent::aPagination['numbers'];
}
// get the points before the numbers in the array.
static public function getSpaceBefore() {
return parent::aPagination['space_before'];
}
// get the points after the numbers in the array.
static public function getSpaceAfter() {
return parent::aPagination['space_after'];
}
}
if(isset($_GET['pag'])) {
$iPage = (int)$_GET['pag'];
} else {
$iPage = 0;
}
$oPagination = new Navigation($iPage, 10, 2);
NavigationDatabase::setQueryTable('alfabet');
try {
$oPagination->do();
$bContinue = true;
} catch(Exception $e) {
echo $e->getMessage();
$bContinue = false;
}
if($bContinue === true) {
$oPagination->setGetParameter('pag');
$oPagination->setToStringRegex('*f | *p | *sb *n *sa | *x | *l',
array(
'*f'=>NavigationOutput::getFirst(),
'*p'=>NavigationOutput::getPrevious(),
'*sb'=>NavigationOutput::getSpaceBefore(),
'*n'=>NavigationOutput::getNumbers(),
'*sa'=>NavigationOutput::getSpaceAfter(),
'*x'=>NavigationOutput::getNext(),
'*l'=>NavigationOutput::getLast()
)
);
try {
$sQuery = "SELECT * FROM alfabet".NavigationDatabase::getLimit();
$sArray = NavigationDatabase::getQueryResult($sQuery);
} catch(Exception $e) {
$sArray = '';
echo $e->getMessage();
}
if(is_array($sArray)) {
if(!empty($sArray)) {
foreach($sArray as $iKey => $aValue) {
echo $aValue['id'].'. '.$aValue['letter'].'<br />';
}
echo '<div>'.$oPagination.'</div>';
} else {
echo 'Geen records gevonden.';
}
}
}
?>
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors');
// <title>Navigatie Klasse</title>
include("phphulp/config.php");
class Navigation {
// current page, total pages and pagination pages amount.
protected $iCurrent;
protected $iTotal;
// limitation vars
protected $iStart;
protected $iShowRecords;
protected $iShowNumbers;
// query vars by using setQuery and setQueryCountField()
protected $sCountQuery;
protected $sQueryCountField = 'aantal';
// query vars by using setQueryTable and setQueryWhereStatement()
protected $sTable;
protected $sWhereStatement;
// parameter var
protected $sGetParameter;
// output vars
protected $aPagination = array();
protected $aToStringReplacements = array();
protected $aWords = array('first'=>'First','prev'=>'Previous','next'=>'Next','last'=>'Last');
protected $sToStringRegex;
protected $sSpace = '...';
// construction
public function __construct($iCurrent, $iShowRecords = 20, $iShowNumbers = 6) {
$this->iCurrent = ($iCurrent >= 0) ? (int)$iCurrent : 0;
$this->iShowRecords = ($iShowRecords > 0) ? (int)$iShowRecords : 20;
$this->iShowNumbers = ($iShowRecords > 0 && $iShowRecords % 2 == 0) ? (int)$iShowNumbers : 6;
}
// set style for the toString function.
public function setToStringRegex($sRegex, $aReplacements) {
$this->sToStringRegex = (string)trim($sRegex);
$this->aToStringReplacements = $aReplacements;
}
// set $_GET parameter
public function setGetParameter($sString) {
$this->sGetParameter = (string)trim($sString);
}
// set ...
public function setSpace($sString) {
$this->sSpace = (string)trim($sString);
}
// set words, first, next, previous, last.
public function setWord($sEnglishWord, $sWord) {
if($sEnglishWord == 'first') {
$sArrayKey = 'first';
} else {
$sArrayKey = substr($sWord, 0, 4);
}
}
// fill array and vars
public function do() {
if(empty($this->sCountQuery)) {
if(!empty($this->sTable)) {
$this->createQuery();
} else {
throw new Exception('First enter a table to be searched in.');
}
}
$this->calculateTotalPages();
$this->setNavigation();
}
// set next page in the pagination array.
protected function setNext() {
if($this->iCurrent == $this->iTotal) {
$this->aPagination['next'] = false;
} else {
$this->aPagination['next'] = $this->iCurrent+1;
}
}
// set last page in the pagination array.
protected function setLast() {
if($this->iCurrent == $this->iTotal) {
$this->aPagination['last'] = false;
} else {
$this->aPagination['last'] = $this->iTotal;
}
}
// set previous page in the pagination array.
protected function setPrevious() {
if($this->iCurrent == 0) {
$this->aPagination['prev'] = false;
} else {
$this->aPagination['prev'] = $this->iCurrent-1;
}
}
// set first page in the pagination array.
protected function setFirst() {
if($this->iCurrent == 0) {
$this->aPagination['first'] = false;
} else {
$this->aPagination['first'] = 0;
}
}
// set points in the pagination array.
protected function setSpaces() {
$iNumbersRound = ceil($this->iShowNumbers / 2);
if(($this->iCurrent - $iNumbersRound) <= 0) {
$this->aPagination['space_before'] = false;
} else {
$this->aPagination['space_before'] = $this->sSpace;
}
if(($this->iCurrent + $iNumbersRound) >= $this->iTotal) {
$this->aPagination['space_after'] = false;
} else {
$this->aPagination['space_after'] = $this->sSpace;
}
}
// set the pagination numbers in the pagination array.
protected function setNumbers() {
$iNumbersRound = ceil($this->iShowNumbers / 2);
$iStart = ($this->iCurrent - $iNumbersRound) <= 0 ? 0 : ($this->iCurrent - $iNumbersRound);
$iEnd = ($this->iCurrent + $iNumbersRound) >= $this->iTotal ? $this->iTotal : ($this->iCurrent + $iNumbersRound);
for($i=$iStart;$i<=$iEnd;$i++) {
if($i == $this->iCurrent) $this->aPagination['current'] = $i;
$this->aPagination['numbers'][] = $i;
}
}
// set everything by using the six functions above.
protected function setNavigation() {
$this->setFirst();
$this->setLast();
$this->setNext();
$this->setPrevious();
$this->setSpaces();
$this->setNumbers();
}
// zip everything to a string, can be used by the inexperienced programmer.
// there is also an expert version; use the stringReplacement methods to set up your own style.
public function __toString() {
if(!empty($this->sToStringRegex)) {
$sString = $this->sToStringRegex;
foreach($this->aToStringReplacements as $sRegex => $sReplace) {
$sString = str_replace($sRegex, $sReplace, $sString);
}
} else {
$aPags = NavigationOutput::getNavigation();
$sString = '';
if(is_bool(NavigationOutput::getFirst()))
$sString .= $aWords['first'];
else
$sString .= '<a href="?'.$this->sGetParameter.'='.$aPags['first'].'">'.$aWords['first'].'</a> | ';
if(is_bool(NavigationOutput::getPrevious()))
$sString .= $aWords['prev'];
else
$sString .= '<a href="?'.$this->sGetParameter.'='.$aPags['prev'].'">'.$aWords['prev'].'</a> | ';
$sString .= NavigationOutput::getSpaceBefore();
foreach($aPags['numbers'] as $iNum) {
if($iNum == NavigationOutput::getCurrent()) {
$sString .= ' ['.($iNum+1).'] ';
} else {
$sString .= ' <a href="?'.$this->sGetParameter.'='.$iNum.'">'.($iNum+1).'</a> ';
}
}
$sString .= NavigationOutput::getSpaceAfter();
if(is_bool(NavigationOutput::getNext()))
$sString .= $aWords['next'];
else
$sString .= ' | <a href="?'.$this->sGetParameter.'='.$aPags['next'].'">'.$aWords['next'].'</a>';
if(is_bool(NavigationOutput::getLast()))
$sString .= $aWords['last'];
else
$sString .= ' | <a href="?'.$this->sGetParameter.'='.$aPags['last'].'">'.$aWords['last'].'</a>';
}
return $sString;
}
}
class NavigationDatabase extends Navigation {
// only use setQuery() and setQueryCountField()...
static public function setQuery($sQuery) {
parent::sCountQuery = (string)trim($sQuery);
}
// set fieldname which contains the query count result, standard value: aantal.
static public function setQueryCountField($sField) {
parent::sQueryCountField = (string)trim($sField);
}
// ...or setQueryTable() and setQueryWhereStatement()
static public function setQueryTable($sTable) {
parent::sTable = (string)trim($sTable);
}
// set where statement for the query to be created with createQuery. If there's anything elses like JOINs used in the query, please use setQuery instead of these, cause setQuery is much more flexible to use with that kinds of queries.
static public function setQueryWhereStatement($sWhere) {
parent::sWhereStatement = (string)trim($sWhere);
}
// only execute when setQuery() isn't used.
static protected function createQuery() {
parent::sCountQuery = "SELECT COUNT(*) AS ".parent::sQueryCountField." FROM ".parent::sTable;
if(!empty($this->sWhereStatement)) {
parent::sCountQuery .= " WHERE ".parent::sWhereStatement;
}
}
// calculate total pages amount.
static protected function calculateTotalPages() {
$sResult = mysql_query(parent::sCountQuery);
if($sResult) {
if(mysql_num_rows($sResult) > 0) {
$sRij = mysql_fetch_assoc($sResult);
parent::iTotal = ceil($sRij[parent::sQueryCountField]/parent::iShowRecords)-1;
} else {
parent::iTotal = 0;
}
} else {
parent::iTotal = 0;
}
}
// return the array, if everything completes, containing the results of the query of the showed page.
static public function getQueryResult($sQuery) {
$sResult = mysql_query($sQuery);
if($sResult) {
$aReturnArray = array();
if(mysql_num_rows($sResult) > 0) {
while($sRij = mysql_fetch_assoc($sResult)) {
$aReturnArray[] = $sRij;
}
}
} else {
throw new Exception(mysql_error().' in query: '.$sQuery);
}
return $aReturnArray;
}
// when you create the query by your own, but not knowing how to use the LIMIT-statement, we make it for you.
static public function getLimit() {
return ' LIMIT '.parent::iStart.','.parent::iShowRecords;
}
}
class NavigationOutput extends Navigation {
// get the pagination array. this can also be used by the user of this class.
static public function getNavigation() {
return parent::aPagination;
}
// get next page in the array.
static public function getNext() {
return parent::aPagination['next'];
}
// get last page in the array.
static public function getLast() {
return parent::aPagination['last'];
}
// get previous page in the array.
static public function getPrevious() {
return parent::aPagination['prev'];
}
// get first page in the array.
static public function getFirst() {
return parent::aPagination['first'];
}
// get the pagination numbers in the array.
static public function getNumbers() {
return parent::aPagination['numbers'];
}
// get the points before the numbers in the array.
static public function getSpaceBefore() {
return parent::aPagination['space_before'];
}
// get the points after the numbers in the array.
static public function getSpaceAfter() {
return parent::aPagination['space_after'];
}
}
if(isset($_GET['pag'])) {
$iPage = (int)$_GET['pag'];
} else {
$iPage = 0;
}
$oPagination = new Navigation($iPage, 10, 2);
NavigationDatabase::setQueryTable('alfabet');
try {
$oPagination->do();
$bContinue = true;
} catch(Exception $e) {
echo $e->getMessage();
$bContinue = false;
}
if($bContinue === true) {
$oPagination->setGetParameter('pag');
$oPagination->setToStringRegex('*f | *p | *sb *n *sa | *x | *l',
array(
'*f'=>NavigationOutput::getFirst(),
'*p'=>NavigationOutput::getPrevious(),
'*sb'=>NavigationOutput::getSpaceBefore(),
'*n'=>NavigationOutput::getNumbers(),
'*sa'=>NavigationOutput::getSpaceAfter(),
'*x'=>NavigationOutput::getNext(),
'*l'=>NavigationOutput::getLast()
)
);
try {
$sQuery = "SELECT * FROM alfabet".NavigationDatabase::getLimit();
$sArray = NavigationDatabase::getQueryResult($sQuery);
} catch(Exception $e) {
$sArray = '';
echo $e->getMessage();
}
if(is_array($sArray)) {
if(!empty($sArray)) {
foreach($sArray as $iKey => $aValue) {
echo $aValue['id'].'. '.$aValue['letter'].'<br />';
}
echo '<div>'.$oPagination.'</div>';
} else {
echo 'Geen records gevonden.';
}
}
}
?>
Iemand een idee wat er aan de hand zou kunnen zijn?
Je kan geen method 'do' noemen blijkbaar, waarschijnlijk omdat dat botst met de do..while lus.
Het helpt denk ik overigens wel om 'ini_set('display_errors')' te veranderen naar 'ini_set('display_errors', true)' :P
Hoe ben je trouwens aan die error gekomen? Ik krijg m namelijk maar niet op m'n scherm...
wat je waarschijnlijk bedoelt is
Ik gebruik PHP via de cli via TextMate, en in de php.ini die die PHP binary gebruikt staat display_errors standaard aan :)
Probeer anders eens in de logbestanden (error_log van Apache?) van je webserver te kijken. Meestal zet PHP daar wel de fouten in, ook al geeft hij ze niet weer.
Mag ik vragen wat het verschil is tussen de 2 stukjes code van je ?
EDIT:
Laat maar... al gezien
Gewijzigd op 01/01/1970 01:00:00 door Ivo K
Oke. Weer iets wat ik door elkaar haal dan. Maar parent->sCountQuery kan niet?
bumpie.
Waarom trouwens die parent::$sCountQuery ?
De extend draait toch rond het feit dat een extended class alle methodes en eigenschappen (properties: variabelen binnen een class) zomaar cadeau krijgt.
Je kan die zomaar gebruiken.
Waarom dan de nadruk leggen op het feit dat de eigenschap in de parent is gedefiniëerd?
Vervang die parent:: gerust door $this-> (of self:: bij static )
(Misschien zie ik iets over het hoofd.)
Gewijzigd op 01/01/1970 01:00:00 door Emmanuel Delay
Als ik dat doe zegt ie dat ik $this niet mag aanroepen in een niet-klasse omgeving..
... en daar heb je een foutje. Je probeert via static methods properties uit te lezen, maar die properties zijn niet static. Met andere woorden, dat zijn eigenschappen van een instantie van je class. Aangezien je bij static methods niet te maken hebt met een instantie, heb je de waarden van die eigenschappen niet :)
Nee, daar was ik al achter :-P. Ik moet dus iets anders dan parent:: of self:: gebruiken omdat de variabelen niet static zijn. Wat? $this werkt ook niet. Of moet ik wel al die variabelen static gaan maken? Da's nogal veel werk...
Mag ik vragen waarom je de NavigationDatabase static hebt gemaakt? Ik heb het even vluchtig bekeken en het lijkt erop alsof het nu niet mogelijk is om 2 verschillende pagination's binnen één pagina te hebben.
Ik denk dat je het onderscheid tussen die 2 nog niet goed begrijpt. En waarschijnlijk zeker niet als ik je er nog bij vertel dat er nog iets bestaat als const ook :p
Als je echt objecten maakt van de klassen. Als je statisch werkt, werk je best met self::
Ik gebruik parent maar voor 1 ding: het uitvoeren van de constructor van de parent.
een voorbeeld:
Ik heb een klasse pagina. Dat is dus een html pagina, met content, titel, javascript data; methodes voor output, converten van gegevens, ...
Een pagina met een gastenboek wordt dan een eigen klasse, als extend van die klasse pagina.
Een extend kan zomaar alle methodes van de parent gebruiken, behalve de contructor (van de parent dus). Die wordt nooit uitgevoerd. Wel, daarvoor laat ik in de constructor van de extended pagina klasse ook de constructor van de parent uitvoeren, omdat ik daar een hoop initialiseer.
Dat is dus met
Code (php)
Achteraf gezien, had ik die initialisering beter in een aparte methode init() gestoken. Kijk, dan heb je parent:: nergens meer voor nodig.
Gewijzigd op 01/01/1970 01:00:00 door Emmanuel Delay
Hm.. Ik heb de static dingen nu maar weggehaald, want die kloppen niet. En nu kan $this wel :-), omdat ik me nu in een klasse-omgeving bevind, gok ik?
Het is nu alleen, hij geeft aan dat ik geen querytabel heb gedefinieerd, wat ik wel doe. Maar ik gok dat dit iets te maken heeft met het feit dat ik een nieuw object aanmaak, waardoor hij de gegevens niet bij de 'hoofdklasse' Navigation opslaat, maar bij een aparte?
Dit is nu mijn code:
Code (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
// <title>Navigatie Klasse</title>
include("phphulp/config.php");
class Navigation {
// current page, total pages and pagination pages amount.
protected $iCurrent;
protected $iTotal;
// limitation vars
protected $iStart;
protected $iShowRecords;
protected $iShowNumbers;
// query vars by using setQuery and setQueryCountField()
protected $sCountQuery;
protected $sQueryCountField = 'aantal';
// query vars by using setQueryTable and setQueryWhereStatement()
protected $sTable;
protected $sWhereStatement;
// parameter var
protected $sGetParameter;
// output vars
protected $aPagination = array();
protected $aToStringReplacements = array();
protected $aWords = array('first'=>'First','prev'=>'Previous','next'=>'Next','last'=>'Last');
protected $sToStringRegex;
protected $sSpace = '...';
// construction
public function __construct($iCurrent, $iShowRecords = 20, $iShowNumbers = 6) {
$this->iCurrent = ($iCurrent >= 0) ? (int)$iCurrent : 0;
$this->iShowRecords = ($iShowRecords > 0) ? (int)$iShowRecords : 20;
$this->iShowNumbers = ($iShowRecords > 0 && $iShowRecords % 2 == 0) ? (int)$iShowNumbers : 6;
}
// set style for the toString function.
public function setToStringRegex($sRegex, $aReplacements) {
$this->sToStringRegex = (string)trim($sRegex);
$this->aToStringReplacements = $aReplacements;
}
// set $_GET parameter
public function setGetParameter($sString) {
$this->sGetParameter = (string)trim($sString);
}
// set ...
public function setSpace($sString) {
$this->sSpace = (string)trim($sString);
}
// set words, first, next, previous, last.
public function setWord($sEnglishWord, $sWord) {
if($sEnglishWord == 'first') {
$sArrayKey = 'first';
} else {
$sArrayKey = substr($sWord, 0, 4);
}
}
// fill array and vars
public function doExecute() {
if(empty($this->sCountQuery)) {
if(!empty($this->sTable)) {
$this->createQuery();
} else {
throw new Exception('First enter a table to be searched in.');
}
}
$this->calculateTotalPages();
$this->setNavigation();
}
// set next page in the pagination array.
protected function setNext() {
if($this->iCurrent == $this->iTotal) {
$this->aPagination['next'] = false;
} else {
$this->aPagination['next'] = $this->iCurrent+1;
}
}
// set last page in the pagination array.
protected function setLast() {
if($this->iCurrent == $this->iTotal) {
$this->aPagination['last'] = false;
} else {
$this->aPagination['last'] = $this->iTotal;
}
}
// set previous page in the pagination array.
protected function setPrevious() {
if($this->iCurrent == 0) {
$this->aPagination['prev'] = false;
} else {
$this->aPagination['prev'] = $this->iCurrent-1;
}
}
// set first page in the pagination array.
protected function setFirst() {
if($this->iCurrent == 0) {
$this->aPagination['first'] = false;
} else {
$this->aPagination['first'] = 0;
}
}
// set points in the pagination array.
protected function setSpaces() {
$iNumbersRound = ceil($this->iShowNumbers / 2);
if(($this->iCurrent - $iNumbersRound) <= 0) {
$this->aPagination['space_before'] = false;
} else {
$this->aPagination['space_before'] = $this->sSpace;
}
if(($this->iCurrent + $iNumbersRound) >= $this->iTotal) {
$this->aPagination['space_after'] = false;
} else {
$this->aPagination['space_after'] = $this->sSpace;
}
}
// set the pagination numbers in the pagination array.
protected function setNumbers() {
$iNumbersRound = ceil($this->iShowNumbers / 2);
$iStart = ($this->iCurrent - $iNumbersRound) <= 0 ? 0 : ($this->iCurrent - $iNumbersRound);
$iEnd = ($this->iCurrent + $iNumbersRound) >= $this->iTotal ? $this->iTotal : ($this->iCurrent + $iNumbersRound);
for($i=$iStart;$i<=$iEnd;$i++) {
if($i == $this->iCurrent) $this->aPagination['current'] = $i;
$this->aPagination['numbers'][] = $i;
}
}
// set everything by using the six functions above.
protected function setNavigation() {
$this->setFirst();
$this->setLast();
$this->setNext();
$this->setPrevious();
$this->setSpaces();
$this->setNumbers();
}
// zip everything to a string, can be used by the inexperienced programmer.
// there is also an expert version; use the stringReplacement methods to set up your own style.
public function __toString() {
if(!empty($this->sToStringRegex)) {
$sString = $this->sToStringRegex;
foreach($this->aToStringReplacements as $sRegex => $sReplace) {
$sString = str_replace($sRegex, $sReplace, $sString);
}
} else {
$aPags = NavigationOutput::getNavigation();
$sString = '';
if(is_bool(NavigationOutput::getFirst()))
$sString .= $aWords['first'];
else
$sString .= '<a href="?'.$this->sGetParameter.'='.$aPags['first'].'">'.$aWords['first'].'</a> | ';
if(is_bool(NavigationOutput::getPrevious()))
$sString .= $aWords['prev'];
else
$sString .= '<a href="?'.$this->sGetParameter.'='.$aPags['prev'].'">'.$aWords['prev'].'</a> | ';
$sString .= NavigationOutput::getSpaceBefore();
foreach($aPags['numbers'] as $iNum) {
if($iNum == NavigationOutput::getCurrent()) {
$sString .= ' ['.($iNum+1).'] ';
} else {
$sString .= ' <a href="?'.$this->sGetParameter.'='.$iNum.'">'.($iNum+1).'</a> ';
}
}
$sString .= NavigationOutput::getSpaceAfter();
if(is_bool(NavigationOutput::getNext()))
$sString .= $aWords['next'];
else
$sString .= ' | <a href="?'.$this->sGetParameter.'='.$aPags['next'].'">'.$aWords['next'].'</a>';
if(is_bool(NavigationOutput::getLast()))
$sString .= $aWords['last'];
else
$sString .= ' | <a href="?'.$this->sGetParameter.'='.$aPags['last'].'">'.$aWords['last'].'</a>';
}
return $sString;
}
}
class NavigationDatabase extends Navigation {
public function __construct() { }
// only use setQuery() and setQueryCountField()...
public function setQuery($sQuery) {
$this->sCountQuery = trim($sQuery);
}
// set fieldname which contains the query count result, standard value: aantal.
public function setQueryCountField($sField) {
$this->sQueryCountField = trim($sField);
}
// ...or setQueryTable() and setQueryWhereStatement()
public function setQueryTable($sTable) {
$this->sTable = trim($sTable);
}
// set where statement for the query to be created with createQuery. If there's anything elses like JOINs used in the query, please use setQuery instead of these, cause setQuery is much more flexible to use with that kinds of queries.
public function setQueryWhereStatement($sWhere) {
$this->sWhereStatement = trim($sWhere);
}
// only execute when setQuery() isn't used.
protected function createQuery() {
$this->sCountQuery = "SELECT COUNT(*) AS ".$this->sQueryCountField." FROM ".$this->sTable;
if(!empty($this->$sWhereStatement)) {
$this->sCountQuery .= " WHERE ".$this->sWhereStatement;
}
}
// calculate total pages amount.
protected function calculateTotalPages() {
$sResult = mysql_query($this->sCountQuery);
if($sResult) {
if(mysql_num_rows($sResult) > 0) {
$sRij = mysql_fetch_assoc($sResult);
$this->iTotal = ceil($sRij[$this->sQueryCountField]/$this->iShowRecords)-1;
} else {
$this->iTotal = 0;
}
} else {
$this->iTotal = 0;
}
}
// return the array, if everything completes, containing the results of the query of the showed page.
public function getQueryResult($sQuery) {
$sResult = mysql_query($sQuery);
if($sResult) {
$aReturnArray = array();
if(mysql_num_rows($sResult) > 0) {
while($sRij = mysql_fetch_assoc($sResult)) {
$aReturnArray[] = $sRij;
}
}
} else {
throw new Exception(mysql_error().' in query: '.$sQuery);
}
return $aReturnArray;
}
// when you create the query by your own, but not knowing how to use the LIMIT-statement, we make it for you.
public function getLimit() {
return ' LIMIT '.$this->iStart.','.$this->iShowRecords;
}
}
class NavigationOutput extends Navigation {
public function __construct() { }
// get the pagination array. this can also be used by the user of this class.
public function getNavigation() {
return $this->aPagination;
}
// get next page in the array.
public function getNext() {
return $this->aPagination['next'];
}
// get last page in the array.
public function getLast() {
return $this->aPagination['last'];
}
// get previous page in the array.
public function getPrevious() {
return $this->aPagination['prev'];
}
// get first page in the array.
public function getFirst() {
return $this->aPagination['first'];
}
// get the pagination numbers in the array.
public function getNumbers() {
return $this->aPagination['numbers'];
}
// get the points before the numbers in the array.
public function getSpaceBefore() {
return $this->aPagination['space_before'];
}
// get the points after the numbers in the array.
public function getSpaceAfter() {
return $this->aPagination['space_after'];
}
}
if(isset($_GET['pag'])) {
$iPage = (int)$_GET['pag'];
} else {
$iPage = 0;
}
$oPagination = new Navigation($iPage, 10, 2);
$oDatabase = new NavigationDatabase();
$oOutput = new NavigationOutput();
$oDatabase->setQueryTable('alfabet');
try {
$oPagination->doExecute();
$bContinue = true;
} catch(Exception $e) {
echo $e->getMessage();
$bContinue = false;
}
if($bContinue === true) {
$oPagination->setGetParameter('pag');
$oPagination->setToStringRegex('*f | *p | *sb *n *sa | *x | *l',
array(
'*f'=>$oOutput->getFirst(),
'*p'=>$oOutput->getPrevious(),
'*sb'=>$oOutput->getSpaceBefore(),
'*n'=>$oOutput->getNumbers(),
'*sa'=>$oOutput->getSpaceAfter(),
'*x'=>$oOutput->getNext(),
'*l'=>$oOutput->getLast()
)
);
try {
$sQuery = "SELECT * FROM alfabet".$oDatabase->getLimit();
$sArray = $oDatabase->getQueryResult($sQuery);
} catch(Exception $e) {
$sArray = '';
echo $e->getMessage();
}
if(is_array($sArray)) {
if(!empty($sArray)) {
foreach($sArray as $iKey => $aValue) {
echo $aValue['id'].'. '.$aValue['letter'].'<br />';
}
echo '<div>'.$oPagination.'</div>';
} else {
echo 'Geen records gevonden.';
}
}
}
?>
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
// <title>Navigatie Klasse</title>
include("phphulp/config.php");
class Navigation {
// current page, total pages and pagination pages amount.
protected $iCurrent;
protected $iTotal;
// limitation vars
protected $iStart;
protected $iShowRecords;
protected $iShowNumbers;
// query vars by using setQuery and setQueryCountField()
protected $sCountQuery;
protected $sQueryCountField = 'aantal';
// query vars by using setQueryTable and setQueryWhereStatement()
protected $sTable;
protected $sWhereStatement;
// parameter var
protected $sGetParameter;
// output vars
protected $aPagination = array();
protected $aToStringReplacements = array();
protected $aWords = array('first'=>'First','prev'=>'Previous','next'=>'Next','last'=>'Last');
protected $sToStringRegex;
protected $sSpace = '...';
// construction
public function __construct($iCurrent, $iShowRecords = 20, $iShowNumbers = 6) {
$this->iCurrent = ($iCurrent >= 0) ? (int)$iCurrent : 0;
$this->iShowRecords = ($iShowRecords > 0) ? (int)$iShowRecords : 20;
$this->iShowNumbers = ($iShowRecords > 0 && $iShowRecords % 2 == 0) ? (int)$iShowNumbers : 6;
}
// set style for the toString function.
public function setToStringRegex($sRegex, $aReplacements) {
$this->sToStringRegex = (string)trim($sRegex);
$this->aToStringReplacements = $aReplacements;
}
// set $_GET parameter
public function setGetParameter($sString) {
$this->sGetParameter = (string)trim($sString);
}
// set ...
public function setSpace($sString) {
$this->sSpace = (string)trim($sString);
}
// set words, first, next, previous, last.
public function setWord($sEnglishWord, $sWord) {
if($sEnglishWord == 'first') {
$sArrayKey = 'first';
} else {
$sArrayKey = substr($sWord, 0, 4);
}
}
// fill array and vars
public function doExecute() {
if(empty($this->sCountQuery)) {
if(!empty($this->sTable)) {
$this->createQuery();
} else {
throw new Exception('First enter a table to be searched in.');
}
}
$this->calculateTotalPages();
$this->setNavigation();
}
// set next page in the pagination array.
protected function setNext() {
if($this->iCurrent == $this->iTotal) {
$this->aPagination['next'] = false;
} else {
$this->aPagination['next'] = $this->iCurrent+1;
}
}
// set last page in the pagination array.
protected function setLast() {
if($this->iCurrent == $this->iTotal) {
$this->aPagination['last'] = false;
} else {
$this->aPagination['last'] = $this->iTotal;
}
}
// set previous page in the pagination array.
protected function setPrevious() {
if($this->iCurrent == 0) {
$this->aPagination['prev'] = false;
} else {
$this->aPagination['prev'] = $this->iCurrent-1;
}
}
// set first page in the pagination array.
protected function setFirst() {
if($this->iCurrent == 0) {
$this->aPagination['first'] = false;
} else {
$this->aPagination['first'] = 0;
}
}
// set points in the pagination array.
protected function setSpaces() {
$iNumbersRound = ceil($this->iShowNumbers / 2);
if(($this->iCurrent - $iNumbersRound) <= 0) {
$this->aPagination['space_before'] = false;
} else {
$this->aPagination['space_before'] = $this->sSpace;
}
if(($this->iCurrent + $iNumbersRound) >= $this->iTotal) {
$this->aPagination['space_after'] = false;
} else {
$this->aPagination['space_after'] = $this->sSpace;
}
}
// set the pagination numbers in the pagination array.
protected function setNumbers() {
$iNumbersRound = ceil($this->iShowNumbers / 2);
$iStart = ($this->iCurrent - $iNumbersRound) <= 0 ? 0 : ($this->iCurrent - $iNumbersRound);
$iEnd = ($this->iCurrent + $iNumbersRound) >= $this->iTotal ? $this->iTotal : ($this->iCurrent + $iNumbersRound);
for($i=$iStart;$i<=$iEnd;$i++) {
if($i == $this->iCurrent) $this->aPagination['current'] = $i;
$this->aPagination['numbers'][] = $i;
}
}
// set everything by using the six functions above.
protected function setNavigation() {
$this->setFirst();
$this->setLast();
$this->setNext();
$this->setPrevious();
$this->setSpaces();
$this->setNumbers();
}
// zip everything to a string, can be used by the inexperienced programmer.
// there is also an expert version; use the stringReplacement methods to set up your own style.
public function __toString() {
if(!empty($this->sToStringRegex)) {
$sString = $this->sToStringRegex;
foreach($this->aToStringReplacements as $sRegex => $sReplace) {
$sString = str_replace($sRegex, $sReplace, $sString);
}
} else {
$aPags = NavigationOutput::getNavigation();
$sString = '';
if(is_bool(NavigationOutput::getFirst()))
$sString .= $aWords['first'];
else
$sString .= '<a href="?'.$this->sGetParameter.'='.$aPags['first'].'">'.$aWords['first'].'</a> | ';
if(is_bool(NavigationOutput::getPrevious()))
$sString .= $aWords['prev'];
else
$sString .= '<a href="?'.$this->sGetParameter.'='.$aPags['prev'].'">'.$aWords['prev'].'</a> | ';
$sString .= NavigationOutput::getSpaceBefore();
foreach($aPags['numbers'] as $iNum) {
if($iNum == NavigationOutput::getCurrent()) {
$sString .= ' ['.($iNum+1).'] ';
} else {
$sString .= ' <a href="?'.$this->sGetParameter.'='.$iNum.'">'.($iNum+1).'</a> ';
}
}
$sString .= NavigationOutput::getSpaceAfter();
if(is_bool(NavigationOutput::getNext()))
$sString .= $aWords['next'];
else
$sString .= ' | <a href="?'.$this->sGetParameter.'='.$aPags['next'].'">'.$aWords['next'].'</a>';
if(is_bool(NavigationOutput::getLast()))
$sString .= $aWords['last'];
else
$sString .= ' | <a href="?'.$this->sGetParameter.'='.$aPags['last'].'">'.$aWords['last'].'</a>';
}
return $sString;
}
}
class NavigationDatabase extends Navigation {
public function __construct() { }
// only use setQuery() and setQueryCountField()...
public function setQuery($sQuery) {
$this->sCountQuery = trim($sQuery);
}
// set fieldname which contains the query count result, standard value: aantal.
public function setQueryCountField($sField) {
$this->sQueryCountField = trim($sField);
}
// ...or setQueryTable() and setQueryWhereStatement()
public function setQueryTable($sTable) {
$this->sTable = trim($sTable);
}
// set where statement for the query to be created with createQuery. If there's anything elses like JOINs used in the query, please use setQuery instead of these, cause setQuery is much more flexible to use with that kinds of queries.
public function setQueryWhereStatement($sWhere) {
$this->sWhereStatement = trim($sWhere);
}
// only execute when setQuery() isn't used.
protected function createQuery() {
$this->sCountQuery = "SELECT COUNT(*) AS ".$this->sQueryCountField." FROM ".$this->sTable;
if(!empty($this->$sWhereStatement)) {
$this->sCountQuery .= " WHERE ".$this->sWhereStatement;
}
}
// calculate total pages amount.
protected function calculateTotalPages() {
$sResult = mysql_query($this->sCountQuery);
if($sResult) {
if(mysql_num_rows($sResult) > 0) {
$sRij = mysql_fetch_assoc($sResult);
$this->iTotal = ceil($sRij[$this->sQueryCountField]/$this->iShowRecords)-1;
} else {
$this->iTotal = 0;
}
} else {
$this->iTotal = 0;
}
}
// return the array, if everything completes, containing the results of the query of the showed page.
public function getQueryResult($sQuery) {
$sResult = mysql_query($sQuery);
if($sResult) {
$aReturnArray = array();
if(mysql_num_rows($sResult) > 0) {
while($sRij = mysql_fetch_assoc($sResult)) {
$aReturnArray[] = $sRij;
}
}
} else {
throw new Exception(mysql_error().' in query: '.$sQuery);
}
return $aReturnArray;
}
// when you create the query by your own, but not knowing how to use the LIMIT-statement, we make it for you.
public function getLimit() {
return ' LIMIT '.$this->iStart.','.$this->iShowRecords;
}
}
class NavigationOutput extends Navigation {
public function __construct() { }
// get the pagination array. this can also be used by the user of this class.
public function getNavigation() {
return $this->aPagination;
}
// get next page in the array.
public function getNext() {
return $this->aPagination['next'];
}
// get last page in the array.
public function getLast() {
return $this->aPagination['last'];
}
// get previous page in the array.
public function getPrevious() {
return $this->aPagination['prev'];
}
// get first page in the array.
public function getFirst() {
return $this->aPagination['first'];
}
// get the pagination numbers in the array.
public function getNumbers() {
return $this->aPagination['numbers'];
}
// get the points before the numbers in the array.
public function getSpaceBefore() {
return $this->aPagination['space_before'];
}
// get the points after the numbers in the array.
public function getSpaceAfter() {
return $this->aPagination['space_after'];
}
}
if(isset($_GET['pag'])) {
$iPage = (int)$_GET['pag'];
} else {
$iPage = 0;
}
$oPagination = new Navigation($iPage, 10, 2);
$oDatabase = new NavigationDatabase();
$oOutput = new NavigationOutput();
$oDatabase->setQueryTable('alfabet');
try {
$oPagination->doExecute();
$bContinue = true;
} catch(Exception $e) {
echo $e->getMessage();
$bContinue = false;
}
if($bContinue === true) {
$oPagination->setGetParameter('pag');
$oPagination->setToStringRegex('*f | *p | *sb *n *sa | *x | *l',
array(
'*f'=>$oOutput->getFirst(),
'*p'=>$oOutput->getPrevious(),
'*sb'=>$oOutput->getSpaceBefore(),
'*n'=>$oOutput->getNumbers(),
'*sa'=>$oOutput->getSpaceAfter(),
'*x'=>$oOutput->getNext(),
'*l'=>$oOutput->getLast()
)
);
try {
$sQuery = "SELECT * FROM alfabet".$oDatabase->getLimit();
$sArray = $oDatabase->getQueryResult($sQuery);
} catch(Exception $e) {
$sArray = '';
echo $e->getMessage();
}
if(is_array($sArray)) {
if(!empty($sArray)) {
foreach($sArray as $iKey => $aValue) {
echo $aValue['id'].'. '.$aValue['letter'].'<br />';
}
echo '<div>'.$oPagination.'</div>';
} else {
echo 'Geen records gevonden.';
}
}
}
?>
Daaraan zie je of je klasse goed in mekaar steekt.
Je zou kunnen zeggen: een klasse groepeert functies; een object groepeert variabelen met hun waarde.
Een statische class kan nuttig zijn wanneer je een aantal functies wil groeperen. Bv. een statistiek class waar je gemiddelde(array $gegevens), mod(array $gegevens), med(array $gegevens), ...
Wat je bij een object doet, is de eigenschappen stap voor stap een juiste waarde geven.
Je maakt een nieuw object aan bv. $m_formulier = new form();
Dan geef je het object de juiste waarden, bv.
$m_formulier->addInput('titel, ''text', $_POST['titel']); $m_formulier->addInput('bericht', 'textarea', $_POST['bericht']);
$m_formulier->addInput('tijd', 'hidden', 'now'); ...
Op het einde kan je het object, dat nu helemaal opgebouwd is, laten doen wat het moet doen.
Bv. $m_formulier->insertIntoDB();
of
echo $m_formulier->getOutput();
Gewijzigd op 01/01/1970 01:00:00 door Emmanuel Delay
Code (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
// <title>Navigatie Klasse</title>
include("phphulp/config.php");
class Navigation {
// current page, total pages and pagination pages amount.
protected $iCurrent;
protected $iTotal;
// limitation vars
protected $iStart;
protected $iShowRecords;
protected $iShowNumbers;
// query vars by using setQuery and setQueryCountField()
protected $sCountQuery;
protected $sQueryCountField = 'aantal';
// query vars by using setQueryTable and setQueryWhereStatement()
protected $sTable;
protected $sWhereStatement;
// parameter var
protected $sGetParameter;
// output vars
protected $aPagination = array();
protected $aToStringReplacements = array();
protected $aWords = array('first'=>'First','prev'=>'Previous','next'=>'Next','last'=>'Last');
protected $sToStringRegex;
protected $sCurrentStringRegex;
protected $sSpace = '...';
// construction
public function __construct($iCurrent, $iShowRecords = 20, $iShowNumbers = 6) {
$this->iCurrent = ($iCurrent >= 0) ? (int)$iCurrent : 0;
$this->iShowRecords = ($iShowRecords > 0) ? (int)$iShowRecords : 20;
$this->iShowNumbers = ($iShowRecords > 0) ? (int)$iShowNumbers : 6;
$this->iStart = $iCurrent * $iShowRecords;
}
public function setCurrentStringRegex($sCurrent) {
$this->sCurrentStringRegex = trim($sCurrent);
}
// set style for the toString function.
public function setToStringRegex($sRegex, $aReplacements) {
$this->sToStringRegex = (string)trim($sRegex);
$this->aToStringReplacements = $aReplacements;
}
// set $_GET parameter
public function setGetParameter($sString) {
$this->sGetParameter = (string)trim($sString);
}
// set ...
public function setSpace($sString) {
$this->sSpace = (string)trim($sString);
}
// set words, first, next, previous, last.
public function setWord($sEnglishWord, $sWord) {
if($sEnglishWord == 'first') {
$sArrayKey = 'first';
} else {
$sArrayKey = substr($sEnglishWord, 0, 4);
}
$this->aWords[$sArrayKey] = trim($sWord);
}
// fill array and vars
public function doExecute() {
$oDatabase = new NavigationDatabase($this);
if(empty($this->sCountQuery)) {
if(!empty($this->sTable)) {
$oDatabase->createQuery();
} else {
throw new Exception('First enter a table to be searched in.');
}
}
$oDatabase->calculateTotalPages();
$this->setNavigation();
}
// set next page in the pagination array.
protected function setNext() {
if($this->iCurrent == $this->iTotal) {
$this->aPagination['next'] = false;
} else {
$this->aPagination['next'] = $this->iCurrent+1;
}
}
// set last page in the pagination array.
protected function setLast() {
if($this->iCurrent == $this->iTotal) {
$this->aPagination['last'] = false;
} else {
$this->aPagination['last'] = $this->iTotal;
}
}
// set previous page in the pagination array.
protected function setPrevious() {
if($this->iCurrent == 0) {
$this->aPagination['prev'] = false;
} else {
$this->aPagination['prev'] = $this->iCurrent-1;
}
}
// set first page in the pagination array.
protected function setFirst() {
if($this->iCurrent == 0) {
$this->aPagination['first'] = false;
} else {
$this->aPagination['first'] = 0;
}
}
// set points in the pagination array.
protected function setSpaces() {
$iNumbersRound = ceil($this->iShowNumbers / 2);
if(($this->iCurrent - $iNumbersRound) <= 0) {
$this->aPagination['space_before'] = false;
} else {
$this->aPagination['space_before'] = $this->sSpace;
}
if(($this->iCurrent + $iNumbersRound) >= $this->iTotal) {
$this->aPagination['space_after'] = false;
} else {
$this->aPagination['space_after'] = $this->sSpace;
}
}
// set the pagination numbers in the pagination array.
protected function setNumbers() {
$iNumbersRound = floor($this->iShowNumbers / 2);
$iStart = ($this->iCurrent - $iNumbersRound) <= 0 ? 0 : ($this->iCurrent - $iNumbersRound);
$iEnd = ($this->iCurrent + $iNumbersRound) >= $this->iTotal ? $this->iTotal : ($this->iCurrent + $iNumbersRound);
for($i=$iStart;$i<=$iEnd;$i++) {
if($i == $this->iCurrent)
$this->aPagination['current'] = $i;
$this->aPagination['numbers'][] = $i;
}
}
// set everything by using the six functions above.
protected function setNavigation() {
$this->setFirst();
$this->setLast();
$this->setNext();
$this->setPrevious();
$this->setSpaces();
$this->setNumbers();
}
// zip everything to a string, can be used by the inexperienced programmer.
// there is also an expert version; use the stringReplacement methods to set up your own style.
public function __toString() {
if(!empty($this->sToStringRegex)) {
$sString = $this->sToStringRegex;
foreach($this->aToStringReplacements as $sRegex => $sReplace) {
$sString = str_replace($sRegex, $sReplace, $sString);
}
} else {
$aPags = NavigationOutput::getNavigation();
$sString = '';
if(is_bool(NavigationOutput::getFirst()))
$sString .= $aWords['first'];
else
$sString .= '<a href="?'.$this->sGetParameter.'='.$aPags['first'].'">'.$aWords['first'].'</a> | ';
if(is_bool(NavigationOutput::getPrevious()))
$sString .= $aWords['prev'];
else
$sString .= '<a href="?'.$this->sGetParameter.'='.$aPags['prev'].'">'.$aWords['prev'].'</a> | ';
$sString .= NavigationOutput::getSpaceBefore();
foreach($aPags['numbers'] as $iNum) {
if($iNum == NavigationOutput::getCurrent()) {
$sString .= ' ['.($iNum+1).'] ';
} else {
$sString .= ' <a href="?'.$this->sGetParameter.'='.$iNum.'">'.($iNum+1).'</a> ';
}
}
$sString .= NavigationOutput::getSpaceAfter();
if(is_bool(NavigationOutput::getNext()))
$sString .= $aWords['next'];
else
$sString .= ' | <a href="?'.$this->sGetParameter.'='.$aPags['next'].'">'.$aWords['next'].'</a>';
if(is_bool(NavigationOutput::getLast()))
$sString .= $aWords['last'];
else
$sString .= ' | <a href="?'.$this->sGetParameter.'='.$aPags['last'].'">'.$aWords['last'].'</a>';
}
return $sString;
}
}
class NavigationDatabase extends Navigation {
protected $oParent;
public function __construct($oNavigation) {
$this->oParent = $oNavigation;
}
// only use setQuery() and setQueryCountField()...
public function setQuery($sQuery) {
$this->oParent->sCountQuery = trim($sQuery);
}
// set fieldname which contains the query count result, standard value: aantal.
public function setQueryCountField($sField) {
$this->oParent->sQueryCountField = trim($sField);
}
// ...or setQueryTable() and setQueryWhereStatement()
public function setQueryTable($sTable) {
$this->oParent->sTable = trim($sTable);
}
// set where statement for the query to be created with createQuery. If there's anything elses like JOINs used in the query, please use setQuery instead of these, cause setQuery is much more flexible to use with that kinds of queries.
public function setQueryWhereStatement($sWhere) {
$this->oParent->sWhereStatement = trim($sWhere);
}
// only execute when setQuery() isn't used.
protected function createQuery() {
$this->oParent->sCountQuery = "SELECT COUNT(*) AS ".$this->oParent->sQueryCountField." FROM ".$this->oParent->sTable;
if(!empty($this->oParent->sWhereStatement)) {
$this->oParent->sCountQuery .= " WHERE ".$this->oParent->sWhereStatement;
}
}
// calculate total pages amount.
protected function calculateTotalPages() {
$sResult = mysql_query($this->oParent->sCountQuery);
if($sResult) {
if(mysql_num_rows($sResult) > 0) {
$sRij = mysql_fetch_assoc($sResult);
$this->oParent->iTotal = ceil($sRij[$this->oParent->sQueryCountField]/$this->oParent->iShowRecords)-1;
} else {
$this->oParent->iTotal = 0;
}
} else {
$this->oParent->iTotal = 0;
}
}
// return the array, if everything completes, containing the results of the query of the showed page.
public function getQueryResult($sQuery) {
$sResult = mysql_query($sQuery);
if($sResult) {
$aReturnArray = array();
if(mysql_num_rows($sResult) > 0) {
while($sRij = mysql_fetch_assoc($sResult)) {
$aReturnArray[] = $sRij;
}
}
} else {
throw new Exception(mysql_error().' in query: '.$sQuery);
}
return $aReturnArray;
}
// when you create the query by your own, but not knowing how to use the LIMIT-statement, we make it for you.
public function getLimit() {
return ' LIMIT '.$this->oParent->iStart.','.$this->oParent->iShowRecords;
}
}
class NavigationOutput extends Navigation {
protected $oParent;
public function __construct($oNavigation) {
$this->oParent = $oNavigation;
}
// get the pagination array. this can also be used by the user of this class.
public function getNavigation() {
return $this->oParent->aPagination;
}
public function getCurrent() {
return str_replace('*c', ($this->oParent->iCurrent+1), $this->oParent->sCurrentStringRegex);
}
// get next page in the array.
public function getNext() {
if($this->oParent->aPagination['next'] === false) {
return $this->oParent->aWords['next'];
} else {
return '<a href="?'.$this->oParent->sGetParameter.'='.$this->oParent->aPagination['next'].'">'.$this->oParent->aWords['next'].'</a>';
}
}
// get last page in the array.
public function getLast() {
if($this->oParent->aPagination['last'] === false) {
return $this->oParent->aWords['last'];
} else {
return '<a href="?'.$this->oParent->sGetParameter.'='.$this->oParent->aPagination['last'].'">'.$this->oParent->aWords['last'].'</a>';
}
}
// get previous page in the array.
public function getPrevious() {
if($this->oParent->aPagination['prev'] === false) {
return $this->oParent->aWords['prev'];
} else {
return '<a href="?'.$this->oParent->sGetParameter.'='.$this->oParent->aPagination['prev'].'">'.$this->oParent->aWords['prev'].'</a>';
}
}
// get first page in the array.
public function getFirst() {
if($this->oParent->aPagination['first'] === false) {
return $this->oParent->aWords['first'];
} else {
return '<a href="?'.$this->oParent->sGetParameter.'='.$this->oParent->aPagination['first'].'">'.$this->oParent->aWords['first'].'</a>';
}
}
// get the pagination numbers in the array.
public function getNumbers() {
if(empty($this->oParent->aPagination['numbers'])) {
return '';
} else {
$sString = '';
foreach($this->oParent->aPagination['numbers'] as $sKey => $sVal) {
if($this->oParent->iCurrent == $sVal)
$sString .= $this->getCurrent().' ';
else
$sString .= '<a href="?'.$this->oParent->sGetParameter.'='.$sVal.'">'.($sVal+1).'</a> ';
}
return trim($sString);
}
}
// get the points before the numbers in the array.
public function getSpaceBefore() {
return $this->oParent->aPagination['space_before'];
}
// get the points after the numbers in the array.
public function getSpaceAfter() {
return $this->oParent->aPagination['space_after'];
}
}
try {
if(isset($_GET['pag'])) {
$iPage = (int)$_GET['pag'];
} else {
$iPage = 0;
}
$oPagination = new Navigation($iPage, 5, 4);
$oDatabase = new NavigationDatabase($oPagination);
$oOutput = new NavigationOutput($oPagination);
$oDatabase->setQueryTable('alfabet');
$oPagination->doExecute();
$oPagination->setGetParameter('pag');
$oPagination->setWord('first', 'eerste');
$oPagination->setWord('previous', 'vorige');
$oPagination->setWord('next', 'volgende');
$oPagination->setWord('last', 'laatste');
$oPagination->setCurrentStringRegex('[*c]');
$oPagination->setToStringRegex('*f | *p | *sb *n *sa | *x | *l',
array(
'*f'=>$oOutput->getFirst(),
'*p'=>$oOutput->getPrevious(),
'*sb'=>$oOutput->getSpaceBefore(),
'*n'=>$oOutput->getNumbers(),
'*sa'=>$oOutput->getSpaceAfter(),
'*x'=>$oOutput->getNext(),
'*l'=>$oOutput->getLast(),
'*c'=>$oOutput->getCurrent()
)
);
$sQuery = "SELECT * FROM alfabet".$oDatabase->getLimit();
$sArray = $oDatabase->getQueryResult($sQuery);
if(is_array($sArray)) {
if(!empty($sArray)) {
foreach($sArray as $iKey => $aValue) {
echo $aValue['id'].'. '.$aValue['letter'].'<br />';
}
echo '<div>'.$oPagination.'</div>';
} else {
echo 'Geen records gevonden.';
}
}
} catch(Exception $e) {
echo $e->getMessage() . $e->getLine();
}
?>
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
// <title>Navigatie Klasse</title>
include("phphulp/config.php");
class Navigation {
// current page, total pages and pagination pages amount.
protected $iCurrent;
protected $iTotal;
// limitation vars
protected $iStart;
protected $iShowRecords;
protected $iShowNumbers;
// query vars by using setQuery and setQueryCountField()
protected $sCountQuery;
protected $sQueryCountField = 'aantal';
// query vars by using setQueryTable and setQueryWhereStatement()
protected $sTable;
protected $sWhereStatement;
// parameter var
protected $sGetParameter;
// output vars
protected $aPagination = array();
protected $aToStringReplacements = array();
protected $aWords = array('first'=>'First','prev'=>'Previous','next'=>'Next','last'=>'Last');
protected $sToStringRegex;
protected $sCurrentStringRegex;
protected $sSpace = '...';
// construction
public function __construct($iCurrent, $iShowRecords = 20, $iShowNumbers = 6) {
$this->iCurrent = ($iCurrent >= 0) ? (int)$iCurrent : 0;
$this->iShowRecords = ($iShowRecords > 0) ? (int)$iShowRecords : 20;
$this->iShowNumbers = ($iShowRecords > 0) ? (int)$iShowNumbers : 6;
$this->iStart = $iCurrent * $iShowRecords;
}
public function setCurrentStringRegex($sCurrent) {
$this->sCurrentStringRegex = trim($sCurrent);
}
// set style for the toString function.
public function setToStringRegex($sRegex, $aReplacements) {
$this->sToStringRegex = (string)trim($sRegex);
$this->aToStringReplacements = $aReplacements;
}
// set $_GET parameter
public function setGetParameter($sString) {
$this->sGetParameter = (string)trim($sString);
}
// set ...
public function setSpace($sString) {
$this->sSpace = (string)trim($sString);
}
// set words, first, next, previous, last.
public function setWord($sEnglishWord, $sWord) {
if($sEnglishWord == 'first') {
$sArrayKey = 'first';
} else {
$sArrayKey = substr($sEnglishWord, 0, 4);
}
$this->aWords[$sArrayKey] = trim($sWord);
}
// fill array and vars
public function doExecute() {
$oDatabase = new NavigationDatabase($this);
if(empty($this->sCountQuery)) {
if(!empty($this->sTable)) {
$oDatabase->createQuery();
} else {
throw new Exception('First enter a table to be searched in.');
}
}
$oDatabase->calculateTotalPages();
$this->setNavigation();
}
// set next page in the pagination array.
protected function setNext() {
if($this->iCurrent == $this->iTotal) {
$this->aPagination['next'] = false;
} else {
$this->aPagination['next'] = $this->iCurrent+1;
}
}
// set last page in the pagination array.
protected function setLast() {
if($this->iCurrent == $this->iTotal) {
$this->aPagination['last'] = false;
} else {
$this->aPagination['last'] = $this->iTotal;
}
}
// set previous page in the pagination array.
protected function setPrevious() {
if($this->iCurrent == 0) {
$this->aPagination['prev'] = false;
} else {
$this->aPagination['prev'] = $this->iCurrent-1;
}
}
// set first page in the pagination array.
protected function setFirst() {
if($this->iCurrent == 0) {
$this->aPagination['first'] = false;
} else {
$this->aPagination['first'] = 0;
}
}
// set points in the pagination array.
protected function setSpaces() {
$iNumbersRound = ceil($this->iShowNumbers / 2);
if(($this->iCurrent - $iNumbersRound) <= 0) {
$this->aPagination['space_before'] = false;
} else {
$this->aPagination['space_before'] = $this->sSpace;
}
if(($this->iCurrent + $iNumbersRound) >= $this->iTotal) {
$this->aPagination['space_after'] = false;
} else {
$this->aPagination['space_after'] = $this->sSpace;
}
}
// set the pagination numbers in the pagination array.
protected function setNumbers() {
$iNumbersRound = floor($this->iShowNumbers / 2);
$iStart = ($this->iCurrent - $iNumbersRound) <= 0 ? 0 : ($this->iCurrent - $iNumbersRound);
$iEnd = ($this->iCurrent + $iNumbersRound) >= $this->iTotal ? $this->iTotal : ($this->iCurrent + $iNumbersRound);
for($i=$iStart;$i<=$iEnd;$i++) {
if($i == $this->iCurrent)
$this->aPagination['current'] = $i;
$this->aPagination['numbers'][] = $i;
}
}
// set everything by using the six functions above.
protected function setNavigation() {
$this->setFirst();
$this->setLast();
$this->setNext();
$this->setPrevious();
$this->setSpaces();
$this->setNumbers();
}
// zip everything to a string, can be used by the inexperienced programmer.
// there is also an expert version; use the stringReplacement methods to set up your own style.
public function __toString() {
if(!empty($this->sToStringRegex)) {
$sString = $this->sToStringRegex;
foreach($this->aToStringReplacements as $sRegex => $sReplace) {
$sString = str_replace($sRegex, $sReplace, $sString);
}
} else {
$aPags = NavigationOutput::getNavigation();
$sString = '';
if(is_bool(NavigationOutput::getFirst()))
$sString .= $aWords['first'];
else
$sString .= '<a href="?'.$this->sGetParameter.'='.$aPags['first'].'">'.$aWords['first'].'</a> | ';
if(is_bool(NavigationOutput::getPrevious()))
$sString .= $aWords['prev'];
else
$sString .= '<a href="?'.$this->sGetParameter.'='.$aPags['prev'].'">'.$aWords['prev'].'</a> | ';
$sString .= NavigationOutput::getSpaceBefore();
foreach($aPags['numbers'] as $iNum) {
if($iNum == NavigationOutput::getCurrent()) {
$sString .= ' ['.($iNum+1).'] ';
} else {
$sString .= ' <a href="?'.$this->sGetParameter.'='.$iNum.'">'.($iNum+1).'</a> ';
}
}
$sString .= NavigationOutput::getSpaceAfter();
if(is_bool(NavigationOutput::getNext()))
$sString .= $aWords['next'];
else
$sString .= ' | <a href="?'.$this->sGetParameter.'='.$aPags['next'].'">'.$aWords['next'].'</a>';
if(is_bool(NavigationOutput::getLast()))
$sString .= $aWords['last'];
else
$sString .= ' | <a href="?'.$this->sGetParameter.'='.$aPags['last'].'">'.$aWords['last'].'</a>';
}
return $sString;
}
}
class NavigationDatabase extends Navigation {
protected $oParent;
public function __construct($oNavigation) {
$this->oParent = $oNavigation;
}
// only use setQuery() and setQueryCountField()...
public function setQuery($sQuery) {
$this->oParent->sCountQuery = trim($sQuery);
}
// set fieldname which contains the query count result, standard value: aantal.
public function setQueryCountField($sField) {
$this->oParent->sQueryCountField = trim($sField);
}
// ...or setQueryTable() and setQueryWhereStatement()
public function setQueryTable($sTable) {
$this->oParent->sTable = trim($sTable);
}
// set where statement for the query to be created with createQuery. If there's anything elses like JOINs used in the query, please use setQuery instead of these, cause setQuery is much more flexible to use with that kinds of queries.
public function setQueryWhereStatement($sWhere) {
$this->oParent->sWhereStatement = trim($sWhere);
}
// only execute when setQuery() isn't used.
protected function createQuery() {
$this->oParent->sCountQuery = "SELECT COUNT(*) AS ".$this->oParent->sQueryCountField." FROM ".$this->oParent->sTable;
if(!empty($this->oParent->sWhereStatement)) {
$this->oParent->sCountQuery .= " WHERE ".$this->oParent->sWhereStatement;
}
}
// calculate total pages amount.
protected function calculateTotalPages() {
$sResult = mysql_query($this->oParent->sCountQuery);
if($sResult) {
if(mysql_num_rows($sResult) > 0) {
$sRij = mysql_fetch_assoc($sResult);
$this->oParent->iTotal = ceil($sRij[$this->oParent->sQueryCountField]/$this->oParent->iShowRecords)-1;
} else {
$this->oParent->iTotal = 0;
}
} else {
$this->oParent->iTotal = 0;
}
}
// return the array, if everything completes, containing the results of the query of the showed page.
public function getQueryResult($sQuery) {
$sResult = mysql_query($sQuery);
if($sResult) {
$aReturnArray = array();
if(mysql_num_rows($sResult) > 0) {
while($sRij = mysql_fetch_assoc($sResult)) {
$aReturnArray[] = $sRij;
}
}
} else {
throw new Exception(mysql_error().' in query: '.$sQuery);
}
return $aReturnArray;
}
// when you create the query by your own, but not knowing how to use the LIMIT-statement, we make it for you.
public function getLimit() {
return ' LIMIT '.$this->oParent->iStart.','.$this->oParent->iShowRecords;
}
}
class NavigationOutput extends Navigation {
protected $oParent;
public function __construct($oNavigation) {
$this->oParent = $oNavigation;
}
// get the pagination array. this can also be used by the user of this class.
public function getNavigation() {
return $this->oParent->aPagination;
}
public function getCurrent() {
return str_replace('*c', ($this->oParent->iCurrent+1), $this->oParent->sCurrentStringRegex);
}
// get next page in the array.
public function getNext() {
if($this->oParent->aPagination['next'] === false) {
return $this->oParent->aWords['next'];
} else {
return '<a href="?'.$this->oParent->sGetParameter.'='.$this->oParent->aPagination['next'].'">'.$this->oParent->aWords['next'].'</a>';
}
}
// get last page in the array.
public function getLast() {
if($this->oParent->aPagination['last'] === false) {
return $this->oParent->aWords['last'];
} else {
return '<a href="?'.$this->oParent->sGetParameter.'='.$this->oParent->aPagination['last'].'">'.$this->oParent->aWords['last'].'</a>';
}
}
// get previous page in the array.
public function getPrevious() {
if($this->oParent->aPagination['prev'] === false) {
return $this->oParent->aWords['prev'];
} else {
return '<a href="?'.$this->oParent->sGetParameter.'='.$this->oParent->aPagination['prev'].'">'.$this->oParent->aWords['prev'].'</a>';
}
}
// get first page in the array.
public function getFirst() {
if($this->oParent->aPagination['first'] === false) {
return $this->oParent->aWords['first'];
} else {
return '<a href="?'.$this->oParent->sGetParameter.'='.$this->oParent->aPagination['first'].'">'.$this->oParent->aWords['first'].'</a>';
}
}
// get the pagination numbers in the array.
public function getNumbers() {
if(empty($this->oParent->aPagination['numbers'])) {
return '';
} else {
$sString = '';
foreach($this->oParent->aPagination['numbers'] as $sKey => $sVal) {
if($this->oParent->iCurrent == $sVal)
$sString .= $this->getCurrent().' ';
else
$sString .= '<a href="?'.$this->oParent->sGetParameter.'='.$sVal.'">'.($sVal+1).'</a> ';
}
return trim($sString);
}
}
// get the points before the numbers in the array.
public function getSpaceBefore() {
return $this->oParent->aPagination['space_before'];
}
// get the points after the numbers in the array.
public function getSpaceAfter() {
return $this->oParent->aPagination['space_after'];
}
}
try {
if(isset($_GET['pag'])) {
$iPage = (int)$_GET['pag'];
} else {
$iPage = 0;
}
$oPagination = new Navigation($iPage, 5, 4);
$oDatabase = new NavigationDatabase($oPagination);
$oOutput = new NavigationOutput($oPagination);
$oDatabase->setQueryTable('alfabet');
$oPagination->doExecute();
$oPagination->setGetParameter('pag');
$oPagination->setWord('first', 'eerste');
$oPagination->setWord('previous', 'vorige');
$oPagination->setWord('next', 'volgende');
$oPagination->setWord('last', 'laatste');
$oPagination->setCurrentStringRegex('[*c]');
$oPagination->setToStringRegex('*f | *p | *sb *n *sa | *x | *l',
array(
'*f'=>$oOutput->getFirst(),
'*p'=>$oOutput->getPrevious(),
'*sb'=>$oOutput->getSpaceBefore(),
'*n'=>$oOutput->getNumbers(),
'*sa'=>$oOutput->getSpaceAfter(),
'*x'=>$oOutput->getNext(),
'*l'=>$oOutput->getLast(),
'*c'=>$oOutput->getCurrent()
)
);
$sQuery = "SELECT * FROM alfabet".$oDatabase->getLimit();
$sArray = $oDatabase->getQueryResult($sQuery);
if(is_array($sArray)) {
if(!empty($sArray)) {
foreach($sArray as $iKey => $aValue) {
echo $aValue['id'].'. '.$aValue['letter'].'<br />';
}
echo '<div>'.$oPagination.'</div>';
} else {
echo 'Geen records gevonden.';
}
}
} catch(Exception $e) {
echo $e->getMessage() . $e->getLine();
}
?>
En voorbeeldje: http://www.dzjemo.nl/test-classnavigation.php :-).
Gewijzigd op 01/01/1970 01:00:00 door Mark PHP