Source for file Pager_Wrapper.php
Documentation is available at Pager_Wrapper.php
// Ready-to-use wrappers for paging the result of a query,
// when fetching the whole resultset is NOT an option.
// This is a performance- and memory-savvy method
// to use PEAR::Pager with a database.
// With this approach, the network load can be
// consistently smaller than with PEAR::DB_Pager.
// Three wrappers are provided, one for each
// PEAR db abstraction layer (DB, MDB and MDB2).
// $query = 'SELECT this, that FROM mytable';
// require_once 'Pager_Wrapper.php'; //this file
// $pagerOptions = array(
// $paged_data = Pager_Wrapper_MDB2($db, $query, $pagerOptions);
// //$paged_data['data']; //paged data
// //$paged_data['links']; //xhtml links for page navigation
// //$paged_data['page_numbers']; //array('current', 'total');
* @param object PEAR::DB instance
* @param array PEAR::Pager options
* @param boolean Disable pagination (get all results)
* @return array with links and paged data
function Pager_Wrapper_DB(&$db, $query, $pager_options = array (), $disabled = false )
// be smart and try to guess the total number of records
if (stristr($query, 'GROUP BY') === false ) {
//no GROUP BY => do a fast COUNT(*) on the rewritten query
$queryCount = 'SELECT COUNT(*)'. stristr($query, ' FROM ');
list ($queryCount, ) = spliti('ORDER BY ', $queryCount);
list ($queryCount, ) = spliti('LIMIT ', $queryCount);
$totalItems = $db->getOne ($queryCount);
if (DB ::isError ($totalItems)) {
//GROUP BY => fetch the whole resultset and count the rows returned
$res = & $db->query ($query);
$totalItems = (int) $res->numRows ();
$pager_options['totalItems'] = $totalItems;
require_once 'Pager/Pager.php';
$page['totalItems'] = $pager_options['totalItems'];
$page['links'] = $pager->links;
$page['page_numbers'] = array (
'current' => $pager->getCurrentPageID (),
'total' => $pager->numPages ()
list ($page['from'], $page['to']) = $pager->getOffsetByPageId ();
? $db->limitQuery ($query, 0 , $totalItems)
: $db->limitQuery ($query, $page['from']-1 , $pager_options['perPage']);
while ($res->fetchInto ($row, DB_FETCHMODE_ASSOC )) {
$page['page_numbers'] = array (
* @param object PEAR::MDB instance
* @param array PEAR::Pager options
* @param boolean Disable pagination (get all results)
* @return array with links and paged data
function Pager_Wrapper_MDB(&$db, $query, $pager_options = array (), $disabled = false )
//be smart and try to guess the total number of records
if (stristr($query, 'GROUP BY') === false ) {
//no GROUP BY => do a fast COUNT(*) on the rewritten query
$queryCount = 'SELECT COUNT(*)'. stristr($query, ' FROM ');
list ($queryCount, ) = spliti('ORDER BY ', $queryCount);
list ($queryCount, ) = spliti('LIMIT ', $queryCount);
$totalItems = $db->queryOne ($queryCount);
if (MDB ::isError ($totalItems)) {
//GROUP BY => fetch the whole resultset and count the rows returned
$res = $db->query ($query);
if (MDB ::isError ($res)) {
$totalItems = (int) $db->numRows ($res);
$pager_options['totalItems'] = $totalItems;
require_once 'Pager/Pager.php';
$page['totalItems'] = $pager_options['totalItems'];
$page['links'] = $pager->links;
$page['page_numbers'] = array (
'current' => $pager->getCurrentPageID (),
'total' => $pager->numPages ()
list ($page['from'], $page['to']) = $pager->getOffsetByPageId ();
? $db->limitQuery ($query, null , 0 , $totalItems)
: $db->limitQuery ($query, null , $page['from']-1 , $pager_options['perPage']);
if (MDB ::isError ($res)) {
while ($row = $db->fetchInto ($res, MDB_FETCHMODE_ASSOC )) {
$page['page_numbers'] = array (
* @param object PEAR::MDB2 instance
* @param array PEAR::Pager options
* @param boolean Disable pagination (get all results)
* @return array with links and paged data
//be smart and try to guess the total number of records
if (stristr($query, 'GROUP BY') === false ) {
//no GROUP BY => do a fast COUNT(*) on the rewritten query
$queryCount = 'SELECT COUNT(*)'. stristr($query, ' FROM ');
list ($queryCount, ) = spliti('ORDER BY ', $queryCount);
list ($queryCount, ) = spliti('LIMIT ', $queryCount);
$totalItems = $db->queryOne ($queryCount);
if (MDB2 ::isError ($totalItems)) {
//GROUP BY => fetch the whole resultset and count the rows returned
$res = & $db->query ($query);
if (MDB2 ::isError ($res)) {
$totalItems = (int) $res->numRows ();
$pager_options['totalItems'] = $totalItems;
require_once 'Pager/Pager.php';
$page['links'] = $pager->links;
$page['totalItems'] = $pager_options['totalItems'];
$page['page_numbers'] = array (
'current' => $pager->getCurrentPageID (),
'total' => $pager->numPages ()
list ($page['from'], $page['to']) = $pager->getOffsetByPageId ();
$page['limit'] = $page['to'] - $page['from'] +1;
$db->setLimit ($pager_options['perPage'], $page['from']-1 );
$page['data'] = $db->queryAll ($query, null , MDB2_FETCHMODE_ASSOC );
if (MDB2 ::isError ($page['data'])) {
$page['page_numbers'] = array (
Documentation generated on Mon, 11 Mar 2019 13:58:53 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|