Paginanummering Klasse

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Embedded Software Developer Games

Functie omschrijving Heb jij affiniteit met hardware en wil jij kleuren binnen een Qt framework? Spreek jij de talen C en of C ++? Dan ben ik wellicht opzoek naar jou! Voor een super gave opdrachtgever in omgeving Delft is er namelijk plek voor een nieuwe kracht! Dit bedrijf is gespecialiseerd in het ontwerpen van software voor een unieke game industrie. Wil jij betrokken worden bij een proces dat loopt van ontwikkeling tot installatie? Waarbij je bezig zult zijn met perfecte systemen die geleverd worden aan binnen en buitenland? Je zult in een team, samen met vier ontwikkelaars, de mooiste

Bekijk vacature »

Randstad B.V.- Freelance Senior Fullstack Develope

Startdatum: 01.05.2023 Richttarief: € 75,00 - €85,00 Duur van de opdracht: 1 jaar Uren per week: 40 Werkmodel: Hybride, dinsdag en donderdag aanwezig op kantoor in Diemen en meer wanneer dit nodig is. Functieomschrijving: De ideale kandidaat gaat onderdeel uitmaken van een junior team binnen het foundation domein. Vanuit het foundation domein werkt dit team samen met andere foundation teams en teams uit het online domein (professionals B2B en B2C) voor het bouwen en integreren van HRM functionaliteiten (verlof en benefits) in de persoonlijke portal van Interim Professionals. Er is meer backend werk dan frontend, maar kandidaat moet beiden leuk

Bekijk vacature »

Software Developer

Dit ga je doen Ontwerpen, ontwikkelen en onderhouden van (mobiele) internettoepassingen; Ontwikkelen en onderhouden van Microservices; Ontwerpen en optimaliseren van databases; Identificeren van nieuwe trends/ontwikkelingen binnen de branche. Hier ga je werken Deze marktleider op gebied van fietsen en fietservaring is gevestigd in twee provincies, verspreid over meerdere locaties. Jij zult voornamelijk in regio Joure aan de slag gaan. De organisatie doelt zich op het leveren van kwalitatief hoogwaardige producten aan alle hun klanten. De organisatie telt circa 4.000 medewerkers in meer dan 10 verschillende landen. Momenteel is de organisatie op zoek naar een Software Developer wilt meewerken aan het

Bekijk vacature »

Cymer Patch Server Developer

Vacature details Vakgebied: Software/IT Opleiding: Senior Werklocatie: Veldhoven Vacature ID: 12919 Introductie This new patch server will be built on Python and Django ReST and GraphQL services with a React frontend, it will consist of several microservices and run on a Kubernetes cluster. It will be supported by several middleware applications such as ElasticSearch, Redis, RabbitMQ, Oracle and Artifactory. Functieomschrijving The Patch Admin team always aim to deliver software at a high quality, we avoid sacrifices here to maintain our velocity. Practically this means that we practice test driven development and perform end-to-end automated testing on our software. This means

Bekijk vacature »

Java Developer

Dit ga je doen Het ontwikkelen van nieuwe software; Het ombouwen van de bestaande software; Zowel back- als front-end software ontwikkelen; Het testen; Het implementeren van de volledig geteste software; Het verzorgen van nazorg en het oplossen van bugs; Het opstellen en bijhouden van procesdocumentatie; Je draagt bij aan het agile/scrum processen van het team. Hier ga je werken Ongetwijfeld heb je een of meerdere producten van deze organisatie in huis. Het proces erachter, daar ben je ongetwijfeld minder bekend mee. Deze opdracht geeft je de kans meer over dit proces te weten te komen en een mooie bijdrage te

Bekijk vacature »

Front-end Developer

Do you want to work with the latest technologies on the development of new systems and applications? Create elegant interfaces using VueJS for thousands of users? Get moving and strengthen Nederlandse Loterij as a Front-end Developer. Thanks to your efforts, our services are always presented in style. As a Front-end Developer you are responsible for website development and improving customer experience based on data analyze. In this way, you directly contribute to a happy, healthy and sporty Netherlands. As a Front-end Developer you score by: Writing elegant, testable components without side-effects to provide functionality to the users Website development, adding

Bekijk vacature »

Medior/senior PHP ontwikkelaar E-commerce

Functie Het software development team bestaat momenteel 5 scrum teams . Ieder team heeft een eigen SCRUM Master en eigen tester. Zij werken voornamelijk in PHP en met hun eigen geschreven framework wat Symfony based is . Jij bent samen met je collega’s verantwoordelijk voor het interne softwaresysteem en alle projecten die daar omheen lopen. Alles wat jij ontwikkelt, wordt direct toegepast en uitgerold (wereldwijd). Dit maakt jouw werk tastbaar en uitdagend! Een greep uit jouw werkzaamheden: Toevoegen en ontwikkelen van nieuwe functionaliteiten Logistieke software ontwikkelen voor intern gebruik Tientallen gigabytes aan data inzichtelijk maken Altijd op zoek gaan naar

Bekijk vacature »

Junior .NET developer

Functie Ons programma is voor afgestudeerde enthousiastelingen die het als een uitdaging zien om met een klein dynamisch team bij de grootste bedrijven van Nederland aan de slag te gaan. Tijdens jouw dienstverband word jij begeleid door een talent manager. Het ontwikkelen van jouw talent staat hierbij centraal. Het programma doorloop je met een team van circa 8 Mede- trainees. De eerste maand start je met een fulltime inhouse opleiding. Deze staat geheel in het teken van de werkzaamheden die jij verder in het programma zult uitvoeren. Na deze opleidingsmaand ga je aan de slag in een dynamische omgeving bij

Bekijk vacature »

Junior Software developer

Functie Als junior .NET ontwikkelaar start jij in een compact team met drie ervaren .NET ontwikkelaars. Wij werken op projectbasis en begeleiden zelf het hele traject van A tot Z. Wij bieden jou dan ook een brede functie aan met veel technische uitdaging! Ons traject ziet er als volgt uit: 1) Wij analyseren de behoefte van onze klant 2) Wij werken de behoefte uit en vertalen dit naar technische werkzaamheden en maken een uren/kosten schatting; 3) Wij gaan aan de slag met het ontwikkelen van het product met directe feedback van de klant, zodat datgene gerealiseerd wordt, wat gewenst is;

Bekijk vacature »

Senior Fullstack Developer (GOLang, TypeScript)

Bedrijfsomschrijving Our client is one of the large worldwide accounting firms. Functieomschrijving We are looking for a senior (all-round) developer (Project On Demand / Tax Technology) Uses as much as possible technology in support of the development process: Git, Jenkins, Docker, npm, skaffold, helm, etc. We are looking for a real hands-on developer; ie not a team lead or other managerial-style role; Acts with integrity both internally and externally and takes personal responsibility in this respect; Curious about the developments within their field and driven to make a difference with the team; Able to empathize with colleagues and stakeholders and

Bekijk vacature »

Ervaren PHP developer gezocht!

Functie Het team bestaat inmiddels uit zo’n 25 collega’s met specialisten op het gebied van development, data(analyse), marketing, infrastructuur en finance. Ze hebben een supermodern pand en bieden hiernaast veel vrijheid en verantwoordelijkheid. Ze doen er alles aan om jou op te gemak te stellen. Zo kun je je eigen werkplek inrichten naar persoonlijke wensen, maar gaan ze bijvoorbeeld ook jaarlijks met elkaar wintersporten en zijn er andere leuke uitjes. Als onderdeel van één van de scrumteams ga je aan de slag, samen ben je medeverantwoordelijk voor het doorontwikkelen van hun business applicatie waar het traffic team dagelijks mee werkt.

Bekijk vacature »

.NET developer

Functie Voor jou als junior .NET ontwikkelaar staat er een flinke uitdaging klaar bij dit bedrijf waar jij veel van kan gaan leren. Zo willen zij een flinke uitbreiding doen op het webbased gedeelte dat zij nu hebben en willen zij het standaard deel gaan moderniseren. Jouw team is dan ook op zoek naar een junior .NET ontwikkelaar die het leuk vindt om op basis van research en development aan de slag te gaan. Jouw mening telt mee als het gaat om hoe en met wat deze applicaties gebouwd en herschreven gaan worden. Jouw functie bij dit bedrijf gaat dan

Bekijk vacature »

C++ Ontwikkelaar

Functieomschrijving Ben jij toe aan een nieuwe uitdaging en werk je graag en goed in C++ en C#? Dan zijn we op zoek naar jou! Dit bedrijf is dé specialist op het gebied van automatiseringssoftware voor een specifieke branche en ze zijn per direct op zoek naar versterking in hun development team. Wat jij gaat doen binnen jouw rol als C++ ontwikkelaar; Je vertaalt de wensen van gebruikers naar een functioneel ontwerp. Je houdt je bezig met het ontwerpen, programmeren en testen van product aanpassingen. Je gaat nieuwe product releases implementeren in de projectteams. Je gaat de effecten van nieuwe

Bekijk vacature »

Software developer - C Sharp

Functie omschrijving Heb jij interesse in het programmeren en ontwikkelen van software? En heb jij enige ervaring met Oracle databases en PL/SQL? Wij zijn voor een leuke opdrachtgever in omgeving Naaldwijk op zoek naar een software ontwikkelaar die graag werkt met C#, JAVA of Oracle. Wij zoeken iemand die breed inzetbaar is en die aan veel verschillende applicaties wilt werken. Als software developer werk je met je collega's samen in een leuk en informeel team aan het (her)ontwerpen van bedrijfssystemen. Je houdt je bezig met het ontwikkelen van REST API's en je onderhoudt applicaties in Oracle PL/SQL en APEX. Vind

Bekijk vacature »

Java Full Stack Developer

Java Full Stack developer What makes Cognizant a unique place to work? The combination of rapid growth and an international and innovative environment! This is creating a lot of opportunities for people like YOU — people with an entrepreneurial spirit who want to make a difference in this world. At Cognizant, together with your colleagues from all around the world, you will collaborate on creating solutions for the world's leading companies and help them become more flexible, more innovative and successful. And this is your chance to be part of the success story: we are looking for a (Senior) Java

Bekijk vacature »
Jesper Diovo

Jesper Diovo

17/05/2009 12:43:00
Quote Anchor link
Ik ben bezig naar de 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

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
<?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.';    
        }
    }
}


?>


Iemand een idee wat er aan de hand zou kunnen zijn?
 
PHP hulp

PHP hulp

25/04/2024 04:01:54
 
Jelmer -

Jelmer -

17/05/2009 13:34:00
Quote Anchor link
PHP Parse error: syntax error, unexpected T_DO, expecting T_STRING in /- on line 71

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
 
Jesper Diovo

Jesper Diovo

17/05/2009 13:38:00
Quote Anchor link
Hm, heb het veranderd in doExecute, maar dat haalt nog weinig uit. Wel dom van die ini_set, idd :P. Even over het hoofd gezien.

Hoe ben je trouwens aan die error gekomen? Ik krijg m namelijk maar niet op m'n scherm...
 
Jelmer -

Jelmer -

17/05/2009 13:42:00
Quote Anchor link
Volgende fout zit in regel 213:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
<?php
parent::sCountQuery = (string)trim($sQuery);
?>

wat je waarschijnlijk bedoelt is
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
<?php
parent::$sCountQuery = (string)trim($sQuery);
?>


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.
 
Ivo K

Ivo K

17/05/2009 13:54:00
Quote Anchor link
@Jelmer;

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
 
Jesper Diovo

Jesper Diovo

17/05/2009 13:56:00
Quote Anchor link
Oke. Weer iets wat ik door elkaar haal dan. Maar parent->sCountQuery kan niet?
 
Jesper Diovo

Jesper Diovo

18/05/2009 21:19:00
Quote Anchor link
bumpie.
 
Emmanuel Delay

Emmanuel Delay

19/05/2009 03:05:00
Quote Anchor link
Ik heb niet echt gelezen waarover dit gaat, maar...

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
 
Jesper Diovo

Jesper Diovo

20/05/2009 20:16:00
Quote Anchor link
Als ik dat doe zegt ie dat ik $this niet mag aanroepen in een niet-klasse omgeving..
 
Jelmer -

Jelmer -

20/05/2009 20:38:00
Quote Anchor link
"niet-klasse omgeving" je bedoelt dat je hem static aanroept, puur als functie dus, en niet als method van een object. Nee, dan heb je inderdaad geen $this. Maar wel self, en dat werkt net zo als parent, maar dan met het voordeel van $this dat hij naar de overgeërfde dingen luistert, maar ook naar de in de eigen class gedefinieerde static properties.


... 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 :)
 
Jesper Diovo

Jesper Diovo

22/05/2009 11:42:00
Quote Anchor link
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...
 
Mark PHP

Mark PHP

22/05/2009 11:55:00
Quote Anchor link
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.
 
Hipska BE

Hipska BE

22/05/2009 11:59:00
Quote Anchor link
Je moet jezelf eens goed gaan afvragen wanneer je een variabele of een methode als static of gewoon wilt 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
 
Emmanuel Delay

Emmanuel Delay

22/05/2009 12:03:00
Quote Anchor link
$this-> werkt als je object-georiënteerd werkt.

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)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
<?php
...
class gastenboek extends pagina
public function __construct()
{

parent::_construct();
// ... rest van de functie
...
?>


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
 
Jesper Diovo

Jesper Diovo

22/05/2009 12:16:00
Quote Anchor link
Hm, ik denk dat ik static nu beter snap inderdaad. Is dat niet een compleet losstaande functie die verder niets met de klasse te maken heeft? En dat ie daarom aangeeft dat je je niet in een object-omgeving bevindt, omdat die functie op zichzelf staat, maar verzamelt is in een klasse die verder geen gezamelijk punt kent.

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)
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
<?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.';    
        }
    }
}

?>
 
Emmanuel Delay

Emmanuel Delay

22/05/2009 12:52:00
Quote Anchor link
Toon vooral eens hoe je je klassen gebruikt.

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
 
Jesper Diovo

Jesper Diovo

22/05/2009 13:29:00
Quote Anchor link
Ja. Ik heb nog flink zitten stoeien, maar het is gelukt. Nu werkt 'ie helemaal volledig, misschien nog wel beter dan voorheen :-). Het enige is dat $iShowNumbers even moet zijn, dat kon je toch doen door $iShowNumbers % 2 == 0? Dat dacht ik in ieder geval, maar dat werkt dus niet. Dat is nog wel het enige wat er bij moet, de rest zit er in:

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

?>


En voorbeeldje: http://www.dzjemo.nl/test-classnavigation.php :-).
 
Mark PHP

Mark PHP

22/05/2009 13:45:00
Quote Anchor link
Nummering is verwarrend, op de eerste pagina is $_GET['pag'] 0 en bij de laatste (zesde) pagina 5. Het lijkt mij logisch om bij één te beginnen en bij zes te eindigen.
Gewijzigd op 01/01/1970 01:00:00 door Mark PHP
 
Jesper Diovo

Jesper Diovo

22/05/2009 13:55:00
Quote Anchor link
Eigenlijk wel. Maar in mijn oorspronkelijke script had ik dit al, daarom heb ik dat zo doorgewerkt. Desalniettemin: 't is aangepast.

Klik.
 



Overzicht Reageren

 
 

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.