cvs: pear /DB_Pager Pager.php package.xml

From: Date: Sun, 12 May 2002 13:59:41 +0000
Subject: cvs: pear /DB_Pager Pager.php package.xml
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-3777@lists.php.net to get a copy of this message
cox Sun May 12 09:59:41 2002 EDT Modified files: /pear/DB_Pager Pager.php package.xml Log: Version 0.7: - Marked as deprecated constructor, build(), fetchInto() and fetchRow(). Use the native PEAR DB row limit support instead (the limitQuery() method) - Implemented the Google algorithm for restricting the number of pages to show (the new fourth param of DB_Pager::getData()) - Added more info in the data array: maxpages, firstpage, lastpage - Updated in-line doc with usage example Index: pear/DB_Pager/Pager.php diff -u pear/DB_Pager/Pager.php:1.2 pear/DB_Pager/Pager.php:1.3 --- pear/DB_Pager/Pager.php:1.2 Sat Nov 17 12:16:12 2001 +++ pear/DB_Pager/Pager.php Sun May 12 09:59:40 2002 @@ -1,9 +1,9 @@ <?php // -// Pear DB Pager - Retrieve and return information of database +// Pear DB Pager - Retrieve and return information of databases // result sets // -// Copyright (C) 2001 Tomas Von Veschler Cox <cox@idecnet.com> +// Copyright (C) 2001 Tomas Von Veschler Cox <cox@idecnet.com> // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -20,7 +20,7 @@ // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // -// $Id: Pager.php,v 1.2 2001/11/17 17:16:12 cox Exp $ +// $Id: Pager.php,v 1.3 2002/05/12 13:59:40 cox Exp $ require_once 'PEAR.php'; require_once 'DB.php'; @@ -29,9 +29,33 @@ * This class handles all the stuff needed for displaying paginated results * from a database query of Pear DB, in a very easy way. * Documentation and examples of use, can be found in: -* http://vulcanonet.com/soft/pager/ +* http://vulcanonet.com/soft/pager/ (could be outdated) * -* @version 0.6 (beta 1) +* IMPORTANT! +* Since PEAR DB already support native row limit (more fast and avaible in +* all the drivers), there is no more need to use $pager->build() or +* the $pager->fetch*() methods. +* +* Usage example: +* +*< ?php +* require_once 'DB/Pager.php'; +* $db = DB::connect('your DSN string'); +* $from = 0; // The row to start to fetch from (you might want to get this +* // param from the $_GET array +* $limit = 10; // The number of results per page +* $maxpages = 10; // The number of pages for displaying in the pager (optional) +* $res = $db->limitQuery($sql, $from, $limit); +* $nrows = 0; // Alternative you could use $res->numRows() +* while ($row = $res->fetchrow()) { +* // XXX code for building the page here +* $nrows++; +* } +* $data = DB_Pager::getData($from, $limit, $nrows, $maxpages); +* // XXX code for building the pager here +* ? > +* +* @version 0.7 * @author Tomas V.V.Cox <cox@idecnet.com> * @see http://vulcanonet.com/soft/pager/ */ @@ -49,6 +73,7 @@ * find this param if is not given. If your Pear_DB backend extension * doesn't support numrows(), you can manually calculate it * and supply later to the constructor + * @deprecated */ function DB_Pager (&$res, $from, $limit, $numrows = null) { @@ -64,8 +89,9 @@ * @return mixed An assoc array with all the data (see getData) * or DB_Error on error * @see DB_Pager::getData + * @deprecated */ - function build () + function build() { // if there is no numrows given, calculate it if ($this->numrows === null) { @@ -83,6 +109,9 @@ return $data; } + /** + * @deprecated + */ function fetchRow($mode=DB_FETCHMODE_DEFAULT) { $this->current++; @@ -92,6 +121,9 @@ return $this->res->fetchRow($mode, $this->current); } + /** + * @deprecated + */ function fetchInto(&$arr, $mode=DB_FETCHMODE_DEFAULT) { $this->current++; @@ -112,10 +144,13 @@ * 'next' => X, // row number where next page starts * 'prev' => X, // row number where prev page starts * 'remain' => X, // number of results remaning *in next page* - * 'numpages'=> X, // total number of pages + * 'numpages'=> X, // total number of pages * 'from' => X, // the row to start fetching - * 'to' => X, // the row to stop fetching - * 'limit' => X, // How many results per page + * 'to' => X, // the row to stop fetching + * 'limit' => X, // how many results per page + * 'maxpages' => X, // how many pages to show (google style) + * 'firstpage' => X, // the row number of the first page + * 'lastpage' => X, // the row number where the last page starts * 'pages' => array( // assoc with page "number => start row" * 1 => X, * 2 => X, @@ -129,7 +164,7 @@ * @return array associative array with data or DB_error on error * */ - function &getData($from, $limit, $numrows) + function &getData($from, $limit, $numrows, $maxpages = false) { if (empty($numrows) || ($numrows < 0)) { return null; @@ -138,13 +173,17 @@ if ($limit <= 0) { return PEAR::raiseError (null, 'wrong "limit" param', null, - null, null, 'DB_Error', true); + null, null, 'DB_Error', true); } // Total number of pages $pages = ceil($numrows/$limit); $data['numpages'] = $pages; + // first & last page + $data['firstpage'] = 1; + $data['lastpage'] = $pages; + // Build pages array $data['pages'] = array(); for ($i=1; $i <= $pages; $i++) { @@ -158,7 +197,27 @@ } if (!isset($data['current'])) { return PEAR::raiseError (null, 'wrong "from" param', null, - null, null, 'DB_Error', true); + null, null, 'DB_Error', true); + } + + // Limit number of pages (goole algoritm) + if ($maxpages) { + $radio = floor($maxpages/2); + $minpage = $data['current'] - $radio; + if ($minpage < 1) { + $minpage = 1; + } + $maxpage = $data['current'] + $radio - 1; + if ($maxpage > $data['numpages']) { + $maxpage = $data['numpages']; + } + foreach (range($minpage, $maxpage) as $page) { + $tmp[$page] = $data['pages'][$page]; + } + $data['pages'] = $tmp; + $data['maxpages'] = $maxpages; + } else { + $data['maxpages'] = null; } // Prev link Index: pear/DB_Pager/package.xml diff -u pear/DB_Pager/package.xml:1.4 pear/DB_Pager/package.xml:1.5 --- pear/DB_Pager/package.xml:1.4 Sun Feb 17 09:47:23 2002 +++ pear/DB_Pager/package.xml Sun May 12 09:59:40 2002 @@ -1,5 +1,4 @@ <?xml version="1.0" encoding="ISO-8859-1"?> -<!DOCTYPE package SYSTEM "../package.dtd"> <!-- do not use the "Type" attribute here, that one is only for generated package.xml files --> <package version="1.0"> @@ -20,15 +19,27 @@ </maintainers> <release> - <version>0.6</version> - <date>2001-10-31</date> - <notes>First release as a PEAR Package (only the class name has changed)</notes> + <version>0.7</version> + <date>2002-05-12</date> + <notes> +- Marked as deprecated constructor, build(), fetchInto() and fetchRow(). Use the native PEAR DB row limit support instead (the limitQuery() method) +- Implemented the Google algorithm for restricting the number of pages to show (the new fourth param of DB_Pager::getData()) +- Added more info in the data array: maxpages, firstpage, lastpage +- Updated in-line doc with usage example + </notes> <state>stable</state> - <filelist> <dir name="/" baseinstalldir="DB"> <file role="php">Pager.php</file> </dir> </filelist> </release> + + <changelog> + <release> + <version>0.6</version> + <date>2001-10-31</date> + <notes>First release as a PEAR Package (only the class name has changed)</notes> + <state>stable</state> + </changelog> </package>

« previous php.pear.cvs (#3777) next »