Source for file Pager.php
Documentation is available at Pager.php
// Pear DB Pager - Retrieve and return information of databases
// 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
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// $Id: Pager.php,v 1.6 2005/07/15 14:16:48 quipo Exp $
* 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/ (could be outdated)
* 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.
* 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
* $data = DB_Pager::getData($from, $limit, $nrows, $maxpages);
* // XXX code for building the pager here
* @author Tomas V.V.Cox <cox@idecnet.com>
* @see http://vulcanonet.com/soft/pager/
class DB_Pager extends PEAR
* @param object $res A DB_result object from Pear_DB
* @param int $from The row to start fetching
* @param int $limit How many results per page
* @param int $numrows Pager will automatically
* 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
function DB_Pager (&$res, $from, $limit, $numrows = null )
$this->numrows = $numrows;
* Calculates all the data needed by Pager to work
* @return mixed An assoc array with all the data (see getData)
// if there is no numrows given, calculate it
if ($this->numrows === null ) {
$this->numrows = $this->res->numrows ();
if (DB ::isError ($this->numrows)) {
$data = $this->getData ($this->from, $this->limit, $this->numrows);
if (DB ::isError ($data)) {
$this->current = $this->from - 1;
$this->top = $data['to'];
function fetchRow ($mode=DB_FETCHMODE_DEFAULT )
if ($this->current >= $this->top) {
return $this->res->fetchRow ($mode, $this->current);
function fetchInto (&$arr, $mode=DB_FETCHMODE_DEFAULT )
if ($this->current >= $this->top) {
return $this->res->fetchInto ($arr, $mode, $this->current);
* Gets all the data needed to paginate results
* This is an associative array with the following
* 'current' => X, // current page you are
* 'numrows' => X, // total number of results
* '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
* 'from' => X, // the row to start fetching
* '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"
* @param int $from The row to start fetching
* @param int $limit How many results per page
* @param int $numrows Number of results from query
* @return array associative array with data or DB_error on error
function &getData ($from, $limit, $numrows, $maxpages = false )
if (empty ($numrows) || ($numrows < 0 )) {
$from = (empty ($from)) ? 0 : $from;
$err = PEAR ::raiseError (null , 'wrong "limit" param', null ,
null , null , 'DB_Error', true );
$pages = ceil($numrows/ $limit);
$data['numpages'] = $pages;
$data['lastpage'] = $pages;
$data['pages'] = array ();
for ($i=1; $i <= $pages; $i++ ) {
$offset = $limit * ($i-1 );
$data['pages'][$i] = $offset;
// $from must point to one page
// The current page we are
if (!isset ($data['current'])) {
$err = PEAR ::raiseError (null , 'wrong "from" param', null ,
null , null , 'DB_Error', true );
// Limit number of pages (Google algorithm)
$radio = floor($maxpages/2 );
$minpage = $data['current'] - $radio;
$maxpage = $data['current'] + $radio - 1;
if ($maxpage > $data['numpages']) {
$maxpage = $data['numpages'];
if ($data['numpages'] < $maxpages) {
$maxpage = $data['numpages'];
// if we're at the top of the range, move minpage down...
if ($data['lastpage'] - $minpage < $maxpages) {
$minpage = $data['lastpage'] - $maxpages + 1;
// if we're at the bottom of the range, move maxpage up...
if ($maxpage < $maxpages) {
foreach (range($minpage, $maxpage) as $page) {
$tmp[$page] = $data['pages'][$page];
$data['maxpages'] = $maxpages;
$data['maxpages'] = null;
$data['prev'] = ($prev >= 0 ) ? $prev : null;
$data['next'] = ($next < $numrows) ? $next : null;
// Results remaining in next page & Last row to fetch
if ($data['current'] == $pages) {
if ($data['current'] == ($pages - 1 )) {
$data['remain'] = $numrows - ($limit* ($pages-1 ));
$data['remain'] = $limit;
$data['to'] = $data['current'] * $limit;
$data['numrows'] = $numrows;
$data['from'] = $from + 1;
Documentation generated on Mon, 11 Mar 2019 15:28:10 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|