3 * Recordset pagination with First/Prev/Next/Last links
5 * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
8 * @link https://adodb.org Project's web site and documentation
9 * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
11 * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
12 * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
13 * any later version. This means you can use it in proprietary products.
14 * See the LICENSE.md file distributed with this source code for details.
15 * @license BSD-3-Clause
16 * @license LGPL-2.1-or-later
18 * @copyright 2000-2013 John Lim
19 * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
23 var $id; // unique id for pager (defaults to 'adodb')
24 var $db; // ADODB connection object
26 var $rs; // recordset generated
27 var $curr_page; // current page number before Render() called, calculated in constructor
28 var $rows; // number of rows per page
29 var $linksPerPage=10; // number of links per page in navigation bar
32 var $gridAttributes = 'width=100% border=1 bgcolor=white';
34 // Localize text strings here
35 var $first = '<code>|<</code>';
36 var $prev = '<code><<</code>';
37 var $next = '<code>>></code>';
38 var $last = '<code>>|</code>';
39 var $moreLinks = '...';
40 var $startLinks = '...';
41 var $gridHeader = false;
42 var $htmlSpecialChars = true;
44 var $linkSelectedColor = 'red';
45 var $cache = 0; #secs to cache with CachePageExecute()
47 //----------------------------------------------
50 // $db adodb connection object
52 // $id optional id to identify which pager,
53 // if you have multiple on 1 page.
54 // $id should be only be [a-z0-9]*
56 function __construct(&$db,$sql,$id = 'adodb', $showPageLinks = false)
60 $curr_page = $id.'_curr_page';
61 if (!empty($PHP_SELF)) $PHP_SELF = htmlspecialchars($_SERVER['PHP_SELF']); // htmlspecialchars() to prevent XSS attacks
66 $this->showPageLinks
= $showPageLinks;
68 $next_page = $id.'_next_page';
70 if (isset($_GET[$next_page])) {
71 $_SESSION[$curr_page] = (integer) $_GET[$next_page];
73 if (empty($_SESSION[$curr_page])) $_SESSION[$curr_page] = 1; ## at first page
75 $this->curr_page
= $_SESSION[$curr_page];
79 //---------------------------
80 // Display link to first page
81 function Render_First($anchor=true)
86 <a href
="<?php echo $PHP_SELF,'?',$this->id;?>_next_page=1"><?php
echo $this->first
;?
></a
>  
;
89 print "$this->first ";
93 //--------------------------
94 // Display link to next page
95 function render_next($anchor=true)
101 <a href
="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() + 1 ?>"><?php
echo $this->next
;?
></a
>  
;
104 print "$this->next ";
111 // for better performance with large recordsets, you can set
112 // $this->db->pageExecuteCountRows = false, which disables
113 // last page counting.
114 function render_last($anchor=true)
118 if (!$this->db
->pageExecuteCountRows
) return;
122 <a href
="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->LastPageNo() ?>"><?php
echo $this->last
;?
></a
>  
;
125 print "$this->last ";
129 //---------------------------------------------------
130 // original code by "Pablo Costa" <pablo@cbsp.com.br>
131 function render_pagelinks()
134 $pages = $this->rs
->LastPageNo();
135 $linksperpage = $this->linksPerPage ?
$this->linksPerPage
: $pages;
136 for($i=1; $i <= $pages; $i+
=$linksperpage)
138 if($this->rs
->AbsolutePage() >= $i)
144 $end = $start+
$linksperpage-1;
145 $link = $this->id
. "_next_page";
146 if($end > $pages) $end = $pages;
149 if ($this->startLinks
&& $start > 1) {
151 $numbers .= "<a href=$PHP_SELF?$link=$pos>$this->startLinks</a> ";
154 for($i=$start; $i <= $end; $i++
) {
155 if ($this->rs
->AbsolutePage() == $i)
156 $numbers .= "<font color=$this->linkSelectedColor><b>$i</b></font> ";
158 $numbers .= "<a href=$PHP_SELF?$link=$i>$i</a> ";
161 if ($this->moreLinks
&& $end < $pages)
162 $numbers .= "<a href=$PHP_SELF?$link=$i>$this->moreLinks</a> ";
163 print $numbers . ' ';
165 // Link to previous page
166 function render_prev($anchor=true)
171 <a href
="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() - 1 ?>"><?php
echo $this->prev
;?
></a
>  
;
174 print "$this->prev ";
178 //--------------------------------------------------------
179 // Simply rendering of grid. You should override this for
180 // better control over the format of the grid
182 // We use output buffering to keep code clean and readable.
183 function RenderGrid()
185 global $gSQLBlockRows; // used by rs2html to indicate how many rows to display
186 include_once(ADODB_DIR
.'/tohtml.inc.php');
188 $gSQLBlockRows = $this->rows
;
189 rs2html($this->rs
,$this->gridAttributes
,$this->gridHeader
,$this->htmlSpecialChars
);
190 $s = ob_get_contents();
195 //-------------------------------------------------------
198 // we use output buffering to keep the code easy to read.
202 if (!$this->rs
->AtFirstPage()) {
203 $this->Render_First();
204 $this->Render_Prev();
206 $this->Render_First(false);
207 $this->Render_Prev(false);
209 if ($this->showPageLinks
){
210 $this->Render_PageLinks();
212 if (!$this->rs
->AtLastPage()) {
213 $this->Render_Next();
214 $this->Render_Last();
216 $this->Render_Next(false);
217 $this->Render_Last(false);
219 $s = ob_get_contents();
224 //-------------------
225 // This is the footer
226 function RenderPageCount()
228 if (!$this->db
->pageExecuteCountRows
) return '';
229 $lastPage = $this->rs
->LastPageNo();
230 if ($lastPage == -1) $lastPage = 1; // check for empty rs.
231 if ($this->curr_page
> $lastPage) $this->curr_page
= 1;
232 return "<font size=-1>$this->page ".$this->curr_page
."/".$lastPage."</font>";
235 //-----------------------------------
236 // Call this class to draw everything.
237 function Render($rows=10)
239 global $ADODB_COUNTRECS;
243 if ($this->db
->dataProvider
== 'informix') $this->db
->cursorType
= IFX_SCROLL
;
245 $savec = $ADODB_COUNTRECS;
246 if ($this->db
->pageExecuteCountRows
) $ADODB_COUNTRECS = true;
248 $rs = $this->db
->CachePageExecute($this->cache
,$this->sql
,$rows,$this->curr_page
);
250 $rs = $this->db
->PageExecute($this->sql
,$rows,$this->curr_page
);
251 $ADODB_COUNTRECS = $savec;
255 print "<h3>Query failed: $this->sql</h3>";
259 if (!$rs->EOF
&& (!$rs->AtFirstPage() ||
!$rs->AtLastPage()))
260 $header = $this->RenderNav();
264 $grid = $this->RenderGrid();
265 $footer = $this->RenderPageCount();
267 $this->RenderLayout($header,$grid,$footer);
273 //------------------------------------------------------
274 // override this to control overall layout and formatting
275 function RenderLayout($header,$grid,$footer,$attributes='border=1 bgcolor=beige')
277 echo "<table ".$attributes."><tr><td>",
279 "</td></tr><tr><td>",
281 "</td></tr><tr><td>",
283 "</td></tr></table>";