Source for file adodb-pager.inc.php
Documentation is available at adodb-pager.inc.php
V4.97 22 Jan 2008 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved.
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence.
Set tabs to 4 for best viewing.
This class provides recordset pagination with
First/Prev/Next/Last links.
Feel free to modify this class for your own use as
it is very basic. To learn how to use it, see the
example in adodb/tests/testpaging.php.
"Pablo Costa" <pablo@cbsp.com.br> implemented Render_PageLinks().
Please note, this class is entirely unsupported,
and no free support requests except for bug reports
will be entertained by the author.
var $id; // unique id for pager (defaults to 'adodb')
var $db; // ADODB connection object
var $rs; // recordset generated
var $curr_page; // current page number before Render() called, calculated in constructor
var $rows; // number of rows per page
var $linksPerPage= 10; // number of links per page in navigation bar
// Localize text strings here
var $first = '<code>|<</code>';
var $prev = '<code><<</code>';
var $next = '<code>>></code>';
var $last = '<code>>|</code>';
var $cache = 0; #secs to cache with CachePageExecute()
//----------------------------------------------
// $db adodb connection object
// $id optional id to identify which pager,
// if you have multiple on 1 page.
// $id should be only be [a-z0-9]*
function ADODB_Pager(&$db,$sql,$id = 'adodb', $showPageLinks = false)
$curr_page = $id. '_curr_page';
if (empty($PHP_SELF)) $PHP_SELF = htmlspecialchars($_SERVER['PHP_SELF']); // htmlspecialchars() to prevent XSS attacks
$next_page = $id. '_next_page';
if (isset ($_GET[$next_page])) {
$_SESSION[$curr_page] = (integer) $_GET[$next_page];
if (empty($_SESSION[$curr_page])) $_SESSION[$curr_page] = 1; ## at first page
//---------------------------
// Display link to first page
<a href=" <?php echo $PHP_SELF,'?',$this->id;?>_next_page=1"> <?php echo $this->first;?></a>
print "$this->first ";
//--------------------------
// Display link to next page
<a href=" <?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() + 1 ?>"> <?php echo $this->next;?></a>
print "$this->next ";
// for better performance with large recordsets, you can set
// $this->db->pageExecuteCountRows = false, which disables
if (!$this->db->pageExecuteCountRows) return;
<a href=" <?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->LastPageNo() ?>"> <?php echo $this->last;?></a>
print "$this->last ";
//---------------------------------------------------
// original code by "Pablo Costa" <pablo@cbsp.com.br>
$pages = $this->rs->LastPageNo();
for($i= 1; $i <= $pages; $i+= $linksperpage)
if($this->rs->AbsolutePage() >= $i)
$end = $start+ $linksperpage- 1;
$link = $this->id . "_next_page";
if($end > $pages) $end = $pages;
$numbers .= "<a href=$PHP_SELF?$link=$pos>$this->startLinks</a> ";
for($i= $start; $i <= $end; $i++ ) {
if ($this->rs->AbsolutePage() == $i)
$numbers .= "<a href=$PHP_SELF?$link=$i>$i</a> ";
$numbers .= "<a href=$PHP_SELF?$link=$i>$this->moreLinks</a> ";
print $numbers . ' ';
<a href=" <?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() - 1 ?>"> <?php echo $this->prev;?></a>
print "$this->prev ";
//--------------------------------------------------------
// Simply rendering of grid. You should override this for
// better control over the format of the grid
// We use output buffering to keep code clean and readable.
global $gSQLBlockRows; // used by rs2html to indicate how many rows to display
$gSQLBlockRows = $this->rows;
//-------------------------------------------------------
// we use output buffering to keep the code easy to read.
if (!$this->rs->AtFirstPage()) {
$this->Render_Prev(false);
$this->Render_PageLinks();
if (!$this->rs->AtLastPage()) {
$this->Render_Next(false);
$this->Render_Last(false);
if (!$this->db->pageExecuteCountRows) return '';
$lastPage = $this->rs->LastPageNo();
if ($lastPage == - 1) $lastPage = 1; // check for empty rs.
return "<font size=-1>$this->page ". $this->curr_page. "/". $lastPage. "</font>";
//-----------------------------------
// Call this class to draw everything.
if ($this->db->dataProvider == 'informix') $this->db->cursorType = IFX_SCROLL;
$savec = $ADODB_COUNTRECS;
if ($this->db->pageExecuteCountRows) $ADODB_COUNTRECS = true;
$ADODB_COUNTRECS = $savec;
print "<h3>Query failed: $this->sql</h3>";
if (!$rs->EOF && (!$rs->AtFirstPage() || !$rs->AtLastPage()))
//------------------------------------------------------
// override this to control overall layout and formating
function RenderLayout($header,$grid,$footer,$attributes= 'border=1 bgcolor=beige')
echo "<table ". $attributes. "><tr><td>",
|