4 V5.14 8 Sept 2011 (c) 2000-2011 John Lim (jlim#natsoft.com). All rights reserved.
5 Released under both BSD license and Lesser GPL library license.
6 Whenever there is any discrepancy between the two licenses,
7 the BSD license will take precedence.
8 Set tabs to 4 for best viewing.
10 This class provides recordset pagination with
11 First/Prev/Next/Last links.
13 Feel free to modify this class for your own use as
14 it is very basic. To learn how to use it, see the
15 example in adodb/tests/testpaging.php.
17 "Pablo Costa" <pablo@cbsp.com.br> implemented Render_PageLinks().
19 Please note, this class is entirely unsupported,
20 and no free support requests except for bug reports
21 will be entertained by the author.
25 var $id; // unique id for pager (defaults to 'adodb')
26 var $db; // ADODB connection object
28 var $rs; // recordset generated
29 var $curr_page; // current page number before Render() called, calculated in constructor
30 var $rows; // number of rows per page
31 var $linksPerPage=10; // number of links per page in navigation bar
34 var $gridAttributes = 'width=100% border=1 bgcolor=white';
36 // Localize text strings here
37 var $first = '<code>|<</code>';
38 var $prev = '<code><<</code>';
39 var $next = '<code>>></code>';
40 var $last = '<code>>|</code>';
41 var $moreLinks = '...';
42 var $startLinks = '...';
43 var $gridHeader = false;
44 var $htmlSpecialChars = true;
46 var $linkSelectedColor = 'red';
47 var $cache = 0; #secs to cache with CachePageExecute()
49 //----------------------------------------------
52 // $db adodb connection object
54 // $id optional id to identify which pager,
55 // if you have multiple on 1 page.
56 // $id should be only be [a-z0-9]*
58 function ADODB_Pager(&$db,$sql,$id = 'adodb', $showPageLinks = false)
62 $curr_page = $id.'_curr_page';
63 if (!empty($PHP_SELF)) $PHP_SELF = htmlspecialchars($_SERVER['PHP_SELF']); // htmlspecialchars() to prevent XSS attacks
68 $this->showPageLinks
= $showPageLinks;
70 $next_page = $id.'_next_page';
72 if (isset($_GET[$next_page])) {
73 $_SESSION[$curr_page] = (integer) $_GET[$next_page];
75 if (empty($_SESSION[$curr_page])) $_SESSION[$curr_page] = 1; ## at first page
77 $this->curr_page
= $_SESSION[$curr_page];
81 //---------------------------
82 // Display link to first page
83 function Render_First($anchor=true)
88 <a href
="<?php echo $PHP_SELF,'?',$this->id;?>_next_page=1"><?php
echo $this->first
;?
></a
>  
;
91 print "$this->first ";
95 //--------------------------
96 // Display link to next page
97 function render_next($anchor=true)
103 <a href
="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() + 1 ?>"><?php
echo $this->next
;?
></a
>  
;
106 print "$this->next ";
113 // for better performance with large recordsets, you can set
114 // $this->db->pageExecuteCountRows = false, which disables
115 // last page counting.
116 function render_last($anchor=true)
120 if (!$this->db
->pageExecuteCountRows
) return;
124 <a href
="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->LastPageNo() ?>"><?php
echo $this->last
;?
></a
>  
;
127 print "$this->last ";
131 //---------------------------------------------------
132 // original code by "Pablo Costa" <pablo@cbsp.com.br>
133 function render_pagelinks()
136 $pages = $this->rs
->LastPageNo();
137 $linksperpage = $this->linksPerPage ?
$this->linksPerPage
: $pages;
138 for($i=1; $i <= $pages; $i+
=$linksperpage)
140 if($this->rs
->AbsolutePage() >= $i)
146 $end = $start+
$linksperpage-1;
147 $link = $this->id
. "_next_page";
148 if($end > $pages) $end = $pages;
151 if ($this->startLinks
&& $start > 1) {
153 $numbers .= "<a href=$PHP_SELF?$link=$pos>$this->startLinks</a> ";
156 for($i=$start; $i <= $end; $i++
) {
157 if ($this->rs
->AbsolutePage() == $i)
158 $numbers .= "<font color=$this->linkSelectedColor><b>$i</b></font> ";
160 $numbers .= "<a href=$PHP_SELF?$link=$i>$i</a> ";
163 if ($this->moreLinks
&& $end < $pages)
164 $numbers .= "<a href=$PHP_SELF?$link=$i>$this->moreLinks</a> ";
165 print $numbers . ' ';
167 // Link to previous page
168 function render_prev($anchor=true)
173 <a href
="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() - 1 ?>"><?php
echo $this->prev
;?
></a
>  
;
176 print "$this->prev ";
180 //--------------------------------------------------------
181 // Simply rendering of grid. You should override this for
182 // better control over the format of the grid
184 // We use output buffering to keep code clean and readable.
185 function RenderGrid()
187 global $gSQLBlockRows; // used by rs2html to indicate how many rows to display
188 include_once(ADODB_DIR
.'/tohtml.inc.php');
190 $gSQLBlockRows = $this->rows
;
191 rs2html($this->rs
,$this->gridAttributes
,$this->gridHeader
,$this->htmlSpecialChars
);
192 $s = ob_get_contents();
197 //-------------------------------------------------------
200 // we use output buffering to keep the code easy to read.
204 if (!$this->rs
->AtFirstPage()) {
205 $this->Render_First();
206 $this->Render_Prev();
208 $this->Render_First(false);
209 $this->Render_Prev(false);
211 if ($this->showPageLinks
){
212 $this->Render_PageLinks();
214 if (!$this->rs
->AtLastPage()) {
215 $this->Render_Next();
216 $this->Render_Last();
218 $this->Render_Next(false);
219 $this->Render_Last(false);
221 $s = ob_get_contents();
226 //-------------------
227 // This is the footer
228 function RenderPageCount()
230 if (!$this->db
->pageExecuteCountRows
) return '';
231 $lastPage = $this->rs
->LastPageNo();
232 if ($lastPage == -1) $lastPage = 1; // check for empty rs.
233 if ($this->curr_page
> $lastPage) $this->curr_page
= 1;
234 return "<font size=-1>$this->page ".$this->curr_page
."/".$lastPage."</font>";
237 //-----------------------------------
238 // Call this class to draw everything.
239 function Render($rows=10)
241 global $ADODB_COUNTRECS;
245 if ($this->db
->dataProvider
== 'informix') $this->db
->cursorType
= IFX_SCROLL
;
247 $savec = $ADODB_COUNTRECS;
248 if ($this->db
->pageExecuteCountRows
) $ADODB_COUNTRECS = true;
250 $rs = $this->db
->CachePageExecute($this->cache
,$this->sql
,$rows,$this->curr_page
);
252 $rs = $this->db
->PageExecute($this->sql
,$rows,$this->curr_page
);
253 $ADODB_COUNTRECS = $savec;
257 print "<h3>Query failed: $this->sql</h3>";
261 if (!$rs->EOF
&& (!$rs->AtFirstPage() ||
!$rs->AtLastPage()))
262 $header = $this->RenderNav();
266 $grid = $this->RenderGrid();
267 $footer = $this->RenderPageCount();
269 $this->RenderLayout($header,$grid,$footer);
275 //------------------------------------------------------
276 // override this to control overall layout and formating
277 function RenderLayout($header,$grid,$footer,$attributes='border=1 bgcolor=beige')
279 echo "<table ".$attributes."><tr><td>",
281 "</td></tr><tr><td>",
283 "</td></tr><tr><td>",
285 "</td></tr></table>";