4 @version v5.20.16 12-Jan-2020
5 @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
6 @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
7 Released under both BSD license and Lesser GPL library license.
8 Whenever there is any discrepancy between the two licenses,
9 the BSD license will take precedence.
10 Set tabs to 4 for best viewing.
12 This class provides recordset pagination with
13 First/Prev/Next/Last links.
15 Feel free to modify this class for your own use as
16 it is very basic. To learn how to use it, see the
17 example in adodb/tests/testpaging.php.
19 "Pablo Costa" <pablo@cbsp.com.br> implemented Render_PageLinks().
21 Please note, this class is entirely unsupported,
22 and no free support requests except for bug reports
23 will be entertained by the author.
27 var $id; // unique id for pager (defaults to 'adodb')
28 var $db; // ADODB connection object
30 var $rs; // recordset generated
31 var $curr_page; // current page number before Render() called, calculated in constructor
32 var $rows; // number of rows per page
33 var $linksPerPage=10; // number of links per page in navigation bar
36 var $gridAttributes = 'width=100% border=1 bgcolor=white';
38 // Localize text strings here
39 var $first = '<code>|<</code>';
40 var $prev = '<code><<</code>';
41 var $next = '<code>>></code>';
42 var $last = '<code>>|</code>';
43 var $moreLinks = '...';
44 var $startLinks = '...';
45 var $gridHeader = false;
46 var $htmlSpecialChars = true;
48 var $linkSelectedColor = 'red';
49 var $cache = 0; #secs to cache with CachePageExecute()
51 //----------------------------------------------
54 // $db adodb connection object
56 // $id optional id to identify which pager,
57 // if you have multiple on 1 page.
58 // $id should be only be [a-z0-9]*
60 function __construct(&$db,$sql,$id = 'adodb', $showPageLinks = false)
64 $curr_page = $id.'_curr_page';
65 if (!empty($PHP_SELF)) $PHP_SELF = htmlspecialchars($_SERVER['PHP_SELF']); // htmlspecialchars() to prevent XSS attacks
70 $this->showPageLinks
= $showPageLinks;
72 $next_page = $id.'_next_page';
74 if (isset($_GET[$next_page])) {
75 $_SESSION[$curr_page] = (integer) $_GET[$next_page];
77 if (empty($_SESSION[$curr_page])) $_SESSION[$curr_page] = 1; ## at first page
79 $this->curr_page
= $_SESSION[$curr_page];
83 //---------------------------
84 // Display link to first page
85 function Render_First($anchor=true)
90 <a href
="<?php echo $PHP_SELF,'?',$this->id;?>_next_page=1"><?php
echo $this->first
;?
></a
>  
;
93 print "$this->first ";
97 //--------------------------
98 // Display link to next page
99 function render_next($anchor=true)
105 <a href
="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() + 1 ?>"><?php
echo $this->next
;?
></a
>  
;
108 print "$this->next ";
115 // for better performance with large recordsets, you can set
116 // $this->db->pageExecuteCountRows = false, which disables
117 // last page counting.
118 function render_last($anchor=true)
122 if (!$this->db
->pageExecuteCountRows
) return;
126 <a href
="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->LastPageNo() ?>"><?php
echo $this->last
;?
></a
>  
;
129 print "$this->last ";
133 //---------------------------------------------------
134 // original code by "Pablo Costa" <pablo@cbsp.com.br>
135 function render_pagelinks()
138 $pages = $this->rs
->LastPageNo();
139 $linksperpage = $this->linksPerPage ?
$this->linksPerPage
: $pages;
140 for($i=1; $i <= $pages; $i+
=$linksperpage)
142 if($this->rs
->AbsolutePage() >= $i)
148 $end = $start+
$linksperpage-1;
149 $link = $this->id
. "_next_page";
150 if($end > $pages) $end = $pages;
153 if ($this->startLinks
&& $start > 1) {
155 $numbers .= "<a href=$PHP_SELF?$link=$pos>$this->startLinks</a> ";
158 for($i=$start; $i <= $end; $i++
) {
159 if ($this->rs
->AbsolutePage() == $i)
160 $numbers .= "<font color=$this->linkSelectedColor><b>$i</b></font> ";
162 $numbers .= "<a href=$PHP_SELF?$link=$i>$i</a> ";
165 if ($this->moreLinks
&& $end < $pages)
166 $numbers .= "<a href=$PHP_SELF?$link=$i>$this->moreLinks</a> ";
167 print $numbers . ' ';
169 // Link to previous page
170 function render_prev($anchor=true)
175 <a href
="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() - 1 ?>"><?php
echo $this->prev
;?
></a
>  
;
178 print "$this->prev ";
182 //--------------------------------------------------------
183 // Simply rendering of grid. You should override this for
184 // better control over the format of the grid
186 // We use output buffering to keep code clean and readable.
187 function RenderGrid()
189 global $gSQLBlockRows; // used by rs2html to indicate how many rows to display
190 include_once(ADODB_DIR
.'/tohtml.inc.php');
192 $gSQLBlockRows = $this->rows
;
193 rs2html($this->rs
,$this->gridAttributes
,$this->gridHeader
,$this->htmlSpecialChars
);
194 $s = ob_get_contents();
199 //-------------------------------------------------------
202 // we use output buffering to keep the code easy to read.
206 if (!$this->rs
->AtFirstPage()) {
207 $this->Render_First();
208 $this->Render_Prev();
210 $this->Render_First(false);
211 $this->Render_Prev(false);
213 if ($this->showPageLinks
){
214 $this->Render_PageLinks();
216 if (!$this->rs
->AtLastPage()) {
217 $this->Render_Next();
218 $this->Render_Last();
220 $this->Render_Next(false);
221 $this->Render_Last(false);
223 $s = ob_get_contents();
228 //-------------------
229 // This is the footer
230 function RenderPageCount()
232 if (!$this->db
->pageExecuteCountRows
) return '';
233 $lastPage = $this->rs
->LastPageNo();
234 if ($lastPage == -1) $lastPage = 1; // check for empty rs.
235 if ($this->curr_page
> $lastPage) $this->curr_page
= 1;
236 return "<font size=-1>$this->page ".$this->curr_page
."/".$lastPage."</font>";
239 //-----------------------------------
240 // Call this class to draw everything.
241 function Render($rows=10)
243 global $ADODB_COUNTRECS;
247 if ($this->db
->dataProvider
== 'informix') $this->db
->cursorType
= IFX_SCROLL
;
249 $savec = $ADODB_COUNTRECS;
250 if ($this->db
->pageExecuteCountRows
) $ADODB_COUNTRECS = true;
252 $rs = $this->db
->CachePageExecute($this->cache
,$this->sql
,$rows,$this->curr_page
);
254 $rs = $this->db
->PageExecute($this->sql
,$rows,$this->curr_page
);
255 $ADODB_COUNTRECS = $savec;
259 print "<h3>Query failed: $this->sql</h3>";
263 if (!$rs->EOF
&& (!$rs->AtFirstPage() ||
!$rs->AtLastPage()))
264 $header = $this->RenderNav();
268 $grid = $this->RenderGrid();
269 $footer = $this->RenderPageCount();
271 $this->RenderLayout($header,$grid,$footer);
277 //------------------------------------------------------
278 // override this to control overall layout and formating
279 function RenderLayout($header,$grid,$footer,$attributes='border=1 bgcolor=beige')
281 echo "<table ".$attributes."><tr><td>",
283 "</td></tr><tr><td>",
285 "</td></tr><tr><td>",
287 "</td></tr></table>";