]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/pear/DB/Pager.php
Remove svn:keywords
[SourceForge/phpwiki.git] / lib / pear / DB / Pager.php
1 <?php
2 //
3 //  Pear DB Pager - Retrieve and return information of databases
4 //                  result sets
5 //
6 //  Copyright (C) 2001  Tomas Von Veschler Cox <cox@idecnet.com>
7 //
8 //  This library is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU Lesser General Public
10 //  License as published by the Free Software Foundation; either
11 //  version 2.1 of the License, or (at your option) any later version.
12 //
13 //  This library is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 //  Lesser General Public License for more details.
17 //
18 //  You should have received a copy of the GNU Lesser General Public
19 //  License along with this library; if not, write to the Free Software
20 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21 //
22 //
23 ///
24 // Based on DB_Pager 0.7 from the pear.php.net repository.
25 // The only modifications made have been modification of the include paths.
26 //
27 // From Pear CVS: Id: Pager.php,v 1.3 2002/05/12 13:59:40 cox Exp
28
29 require_once 'PEAR.php';
30 require_once 'DB.php';
31
32 /**
33 * This class handles all the stuff needed for displaying paginated results
34 * from a database query of Pear DB, in a very easy way.
35 * Documentation and examples of use, can be found in:
36 * http://vulcanonet.com/soft/pager/ (could be outdated)
37 *
38 * IMPORTANT!
39 * Since PEAR DB already support native row limit (more fast and avaible in
40 * all the drivers), there is no more need to use $pager->build() or
41 * the $pager->fetch*() methods.
42 *
43 * Usage example:
44 *
45 *< ?php
46 * require_once 'DB/Pager.php';
47 * $db = DB::connect('your DSN string');
48 * $from = 0;   // The row to start to fetch from (you might want to get this
49 *              // param from the $_GET array
50 * $limit = 10; // The number of results per page
51 * $maxpages = 10; // The number of pages for displaying in the pager (optional)
52 * $res = $db->limitQuery($sql, $from, $limit);
53 * $nrows = 0; // Alternative you could use $res->numRows()
54 * while ($row = $res->fetchrow()) {
55 *    // XXX code for building the page here
56 *     $nrows++;
57 * }
58 * $data = DB_Pager::getData($from, $limit, $nrows, $maxpages);
59 * // XXX code for building the pager here
60 * ? >
61 *
62 * @version 0.7
63 * @author Tomas V.V.Cox <cox@idecnet.com>
64 * @see http://vulcanonet.com/soft/pager/
65 */
66
67 class DB_Pager extends PEAR
68 {
69
70     /**
71     * Constructor
72     *
73     * @param object $res  A DB_result object from Pear_DB
74     * @param int    $from  The row to start fetching
75     * @param int    $limit  How many results per page
76     * @param int    $numrows Pager will automatically
77     *    find this param if is not given. If your Pear_DB backend extension
78     *    doesn't support numrows(), you can manually calculate it
79     *    and supply later to the constructor
80     * @deprecated
81     */
82     function DB_Pager (&$res, $from, $limit, $numrows = null)
83     {
84         $this->res = $res;
85         $this->from = $from;
86         $this->limit = $limit;
87         $this->numrows = $numrows;
88     }
89
90     /**
91     * Calculates all the data needed by Pager to work
92     *
93     * @return mixed An assoc array with all the data (see getData)
94     *    or DB_Error on error
95     * @see DB_Pager::getData
96     * @deprecated
97     */
98     function build()
99     {
100         // if there is no numrows given, calculate it
101         if ($this->numrows === null) {
102             $this->numrows = $this->res->numrows();
103             if (DB::isError($this->numrows)) {
104                 return $this->numrows;
105             }
106         }
107         $data = $this->getData($this->from, $this->limit, $this->numrows);
108         if (DB::isError($data)) {
109             return $data;
110         }
111         $this->current = $this->from - 1;
112         $this->top = $data['to'];
113         return $data;
114     }
115
116     /**
117     * @deprecated
118     */
119     function fetchRow($mode=DB_FETCHMODE_DEFAULT)
120     {
121         $this->current++;
122         if ($this->current >= $this->top) {
123             return null;
124         }
125         return $this->res->fetchRow($mode, $this->current);
126     }
127
128     /**
129     * @deprecated
130     */
131     function fetchInto(&$arr, $mode=DB_FETCHMODE_DEFAULT)
132     {
133         $this->current++;
134         if ($this->current >= $this->top) {
135             return null;
136         }
137         return $this->res->fetchInto($arr, $mode, $this->current);
138     }
139
140     /*
141     * Gets all the data needed to paginate results
142     * This is an associative array with the following
143     * values filled in:
144     *
145     * array(
146     *    'current' => X,    // current page you are
147     *    'numrows' => X,    // total number of results
148     *    'next'    => X,    // row number where next page starts
149     *    'prev'    => X,    // row number where prev page starts
150     *    'remain'  => X,    // number of results remaning *in next page*
151     *    'numpages'=> X,    // total number of pages
152     *    'from'    => X,    // the row to start fetching
153     *    'to'      => X,    // the row to stop fetching
154     *    'limit'   => X,    // how many results per page
155     *    'maxpages'   => X, // how many pages to show (google style)
156     *    'firstpage'  => X, // the row number of the first page
157     *    'lastpage'   => X, // the row number where the last page starts
158     *    'pages'   => array(    // assoc with page "number => start row"
159     *                1 => X,
160     *                2 => X,
161     *                3 => X
162     *                )
163     *    );
164     * @param int $from    The row to start fetching
165     * @param int $limit   How many results per page
166     * @param int $numrows Number of results from query
167     *
168     * @return array associative array with data or DB_error on error
169     *
170     */
171     function &getData($from, $limit, $numrows, $maxpages = false)
172     {
173         if (empty($numrows) || ($numrows < 0)) {
174             return null;
175         }
176         $from = (empty($from)) ? 0 : $from;
177
178         if ($limit <= 0) {
179             return PEAR::raiseError (null, 'wrong "limit" param', null,
180                                      null, null, 'DB_Error', true);
181         }
182
183         // Total number of pages
184         $pages = ceil($numrows/$limit);
185         $data['numpages'] = $pages;
186
187         // first & last page
188         $data['firstpage'] = 1;
189         $data['lastpage']  = $pages;
190
191         // Build pages array
192         $data['pages'] = array();
193         for ($i=1; $i <= $pages; $i++) {
194             $offset = $limit * ($i-1);
195             $data['pages'][$i] = $offset;
196             // $from must point to one page
197             if ($from == $offset) {
198                 // The current page we are
199                 $data['current'] = $i;
200             }
201         }
202         if (!isset($data['current'])) {
203             return PEAR::raiseError (null, 'wrong "from" param', null,
204                                      null, null, 'DB_Error', true);
205         }
206
207         // Limit number of pages (goole algoritm)
208         if ($maxpages) {
209             $radio = floor($maxpages/2);
210             $minpage = $data['current'] - $radio;
211             if ($minpage < 1) {
212                 $minpage = 1;
213             }
214             $maxpage = $data['current'] + $radio - 1;
215             if ($maxpage > $data['numpages']) {
216                 $maxpage = $data['numpages'];
217             }
218             foreach (range($minpage, $maxpage) as $page) {
219                 $tmp[$page] = $data['pages'][$page];
220             }
221             $data['pages'] = $tmp;
222             $data['maxpages'] = $maxpages;
223         } else {
224             $data['maxpages'] = null;
225         }
226
227         // Prev link
228         $prev = $from - $limit;
229         $data['prev'] = ($prev >= 0) ? $prev : null;
230
231         // Next link
232         $next = $from + $limit;
233         $data['next'] = ($next < $numrows) ? $next : null;
234
235         // Results remaining in next page & Last row to fetch
236         if ($data['current'] == $pages) {
237             $data['remain'] = 0;
238             $data['to'] = $numrows;
239         } else {
240             if ($data['current'] == ($pages - 1)) {
241                 $data['remain'] = $numrows - ($limit*($pages-1));
242             } else {
243                 $data['remain'] = $limit;
244             }
245             $data['to'] = $data['current'] * $limit;
246         }
247         $data['numrows'] = $numrows;
248         $data['from']    = $from + 1;
249         $data['limit']   = $limit;
250
251         return $data;
252     }
253 }
254 ?>