paginanummering-klasse

Gesponsorde koppelingen

PHP script bestanden

  1. paginanummering-klasse

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?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 $aToCurrentReplacements = array('[*c]', '*c');
    protected $sSpace = '...';
    
    // construction
    public function __construct($iCurrent, $iShowRecords = 20, $iShowNumbers = 6, $sGetParameter) {
        $this->iCurrent = ($iCurrent > 1) ? (int)$iCurrent : 1;
        $this->iShowRecords = ($iShowRecords > 0) ? (int)$iShowRecords : 20;
        $this->iShowNumbers = ($iShowRecords > 0) ? (int)$iShowNumbers : 6;
        $this->iStart = ($iCurrent-1) * $iShowRecords;
        $this->sGetParameter = (string)trim($sGetParameter);
    }

    
    public function setCurrentStringRegex($sRegex, $sReplace) {
        $this->aToCurrentReplacements = array($sRegex, $sReplace);
    }

    
    // set style for the toString function.
    public function setToStringRegex($sRegex, $aReplacements) {
        $this->sToStringRegex = (string)trim($sRegex);
        $this->aToStringReplacements = $aReplacements;
    }

    
    // 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 <= 1) {
            $this->aPagination['prev'] = false;
        }
else {
            $this->aPagination['prev'] = $this->iCurrent-1;
        }
    }

    
    // set first page in the pagination array.
    protected function setFirst() {
        if($this->iCurrent <= 1) {
            $this->aPagination['first'] = false;
        }
else {
            $this->aPagination['first'] = 1;
        }
    }

    
    // set points in the pagination array.
    protected function setSpaces() {
        $iNumbersRound = ceil($this->iShowNumbers / 2);
        
        if(($this->iCurrent - $iNumbersRound) <= 1) {
            $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) <= 1 ? 1 : ($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.'] ';
                }
else {
                    $sString .= ' <a href="?'.$this->sGetParameter.'='.$iNum.'">'.$iNum.'</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);
            }
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($this->oParent->aToCurrentReplacements[1], $this->oParent->iCurrent, $this->oParent->aToCurrentReplacements[0]);
    }

    
    // 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.'</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 = 1;
    }

    
    $oPagination = new Navigation($iPage, 5, 4, 'pag');
    $oDatabase = new NavigationDatabase($oPagination);
    $oOutput = new NavigationOutput($oPagination);
    
    $oDatabase->setQueryTable('alfabet');
    $oPagination->doExecute();
    
    $oPagination->setWord('first', 'eerste');
    $oPagination->setWord('previous', 'vorige');
    $oPagination->setWord('next', 'volgende');
    $oPagination->setWord('last', 'laatste');
    
    $oPagination->setCurrentStringRegex('[*c]', '*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()
        )
    );

    
    $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();
}

?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.