paginanummering.class.php

Gesponsorde koppelingen

PHP script bestanden

  1. index.php
  2. paginanummering.class.php

« 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
<?php
/**
 * Klasse voor paginanummering van MySQL resultaten
 *
 * @author Roel van de Water
 */

class Paginanummering
{
    /*
     * Query voor het ophalen van resultaten
     *
     * @var string
     */

    private $_sQuery;
    
    /*
     * Het aantal resultaten per pagina
     *
     * @var integer
     */

    private $_iResultaten;
    
    /*
     * De naam van de Query String van het nummer van de pagina
     *
     * @var string
     */

    private $_sQueryString;
    
    /*
     * Aantal navigatieitems voor de huidige pagina
     *
     * @var integer
     */

    private $_iAantalVoor;
    
    /*
     * Aantal navigatieitems na de huidige pagina
     *
     * @var integer
     */

    private $_iAantalNa;
    
    /*
     * Het totaal aantal resultaten van de query
     *
     * @var integer
     */

    private $_iTotaalResultaten;
    
    /*
     * Constructor voor de klasse met de verplichte parameters
     *
     * @param string $sQuery
     * @param string $sQueryString
     * @param integer $iResultaten
     * @param integer $iAantalVoor
     * @param integer $iAantalNa
     */

    public function __construct($sQuery, $sQueryString, $iResultaten, $iAantalVoor = 3, $iAantalNa = 3)
    {

        $this->_sQuery = (string) $sQuery;
        $this->_sQueryString = (string) $sQueryString;
        $this->_iResultaten = (int) $iResultaten;
        $this->_iAantalVoor = (int) $iAantalVoor;
        $this->_iAantalNa = (int) $iAantalNa;
        
        // Query uitvoeren om het totaal aantal resultaten op te halen
        $qSql = mysql_query($sQuery);
        if ($qSql === false)
        {

            throw new Exception('Het totaal aantal resultaten kon niet opgehaald worden!');
        }

        else
        {
            $this->_iTotaalResultaten = (int) mysql_num_rows($qSql);
        }
    }

    
    /*
     * Functie voor het ophalen van het huidige paginanummer
     *
     * @return integer
     */

    private function getPagina()
    {

        // Controleren of de Query String geldig is - anders 0 terugsturen
        return isset($_GET[$this->_sQueryString]) && ctype_digit($_GET[$this->_sQueryString]) && $_GET[$this->_sQueryString] >= 0 && $_GET[$this->_sQueryString] < ceil($this->_iTotaalResultaten / $this->_iResultaten) ? $_GET[$this->_sQueryString] : 0;
    }


    /*
     * Functie voor het ophalen van het totaal aantal resultaten
     *
     * @return integer
     */

    public function getTotaalResultaten()
    {

         return (int) $this->iTotaalResultaten;
    }

    
    /*
     * Functie voor het ophalen van de resultaten
     *
     * @return resource
     */

    public function getResultaten()
    {

        if ($this->getPagina() > floor($this->_iTotaalResultaten / $this->_iResultaten) || !isset($_GET[$this->_sQueryString]))
        {

            // De opgevraagde pagina is groter dan het totaal aantal resultaten of er is geen Query String opgegeven - geef de standaard resultaten
            $sQuery = $this->_sQuery . " LIMIT " . $this->_iResultaten;
        }

        else
        {
            // Geef de resultaten terug van de huidige pagina
            $sQuery = $this->_sQuery . " LIMIT " . ($this->getPagina() * $this->_iResultaten) . ", " . $this->_iResultaten;
        }

        
        $qSql = mysql_query($sQuery);
        if ($qSql === false)
        {

            throw new Exception('De resultaten konden niet opgehaald worden!');
        }

        else
        {
            return $qSql;
        }
    }

    
    /*
     * Navigatie opbouwen en terugsturen
     *
     * @return string
     */

    public function getNavigatie()
    {

        // Array met de navigatieitems erin
        $aItems = array();
        
        // Controleren of er meerdere pagina's zijn
        if (floor($this->_iTotaalResultaten / ($this->_iResultaten + 1)) > 0)
        {

            $iEerste = ($this->getPagina() - $this->_iAantalVoor >= 0) ? $this->getPagina() - $this->_iAantalVoor : 0;
            $iLaatste = ($this->getPagina() + $this->_iAantalNa < ceil($this->_iTotaalResultaten / $this->_iResultaten)) ? $this->getPagina() + ($this->_iAantalNa + 1) : ceil($this->_iTotaalResultaten / $this->_iResultaten);
            
            // Niet de eerste pagina, dus maak een 'vorige' knop
            if ($this->getPagina() > 0)
            {

                $aItems[] = array
                            (
                                'soort' => 'vorige',
                                'nummer' => $this->getPagina() - 1,
                                'schermnummer' => $this->getPagina() - 1
                            );
            }

            
            // Niet de eerste pagina, dus maak een 'eerste' knop
            if ($iEerste > 0)
            {

                $aItems[] = array
                            (
                                'soort' => 'eerste',
                                'nummer' => 0,
                                'schermnummer' => 1
                            );
            }

            
            // Loop door de paginanummers heen en controleer of het de huidige pagina is
            for ($i = $iEerste; $i < $iLaatste; $i++)
            {

                if ($i == $this->getPagina())
                {

                    $aItems[] = array
                                (
                                    'soort' => 'huidig',
                                    'nummer' => $i,
                                    'schermnummer' => $i + 1
                                );
                }

                else
                {
                    $aItems[] = array
                                (
                                    'soort' => 'pagina',
                                    'nummer' => $i,
                                    'schermnummer' => $i + 1
                                );
                }
            }

            
            // Het is niet de laatste pagina, dus maak een 'laatste' knop
            if ($iLaatste < ceil($this->_iTotaalResultaten / $this->_iResultaten))
            {

                $aItems[] = array
                            (
                                'soort' => 'laatste',
                                'nummer' => ceil($this->_iTotaalResultaten / $this->_iResultaten) - 1,
                                'schermnummer' => ceil($this->_iTotaalResultaten / $this->_iResultaten)
                            );
            }

            
            // Het is niet de laatste pagina, dus maak een 'volgende' knop
            if ($this->getPagina() + 1 < ceil($this->_iTotaalResultaten / $this->_iResultaten))
            {

                $aItems[] = array
                            (
                                'soort' => 'volgende',
                                'nummer' => $this->getPagina() + 1,
                                'schermnummer' => $this->getPagina() + 1
                            );
            }
        }

        
        // Array met alle navigatieitems terugsturen
        return $aItems;
    }
}

?>

 
 

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.