3 V4.20 22 Feb 2004 (c) 2000-2004 John Lim (jlim@natsoft.com.my). All rights reserved.
4 Released under both BSD license and Lesser GPL library license.
5 Whenever there is any discrepancy between the two licenses,
6 the BSD license will take precedence.
7 Set tabs to 4 for best viewing.
9 This class provides recordset pagination with
10 First/Prev/Next/Last links.
12 Feel free to modify this class for your own use as
13 it is very basic. To learn how to use it, see the
14 example in adodb/tests/testpaging.php.
16 "Pablo Costa" <pablo@cbsp.com.br> implemented Render_PageLinks().
18 Please note, this class is entirely unsupported,
19 and no free support requests except for bug reports
20 will be entertained by the author.
24 var $id; // unique id for pager (defaults to 'adodb')
25 var $qs; // extra parameters to pass on the query line
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, $qs="")
60 global $HTTP_SERVER_VARS,$PHP_SELF,$HTTP_SESSION_VARS,$HTTP_GET_VARS;
62 $curr_page = $id.'_curr_page';
63 if (empty($PHP_SELF)) $PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF'];
66 //hack to allow extra junk onto the query string
70 $this->showPageLinks
= $showPageLinks;
72 $next_page = $id.'_next_page';
73 if (isset($HTTP_GET_VARS[$next_page])) {
74 $HTTP_SESSION_VARS[$curr_page] = $HTTP_GET_VARS[$next_page];
76 if (empty($HTTP_SESSION_VARS[$curr_page])) $HTTP_SESSION_VARS[$curr_page] = 1; ## at first page
78 $this->curr_page
= $HTTP_SESSION_VARS[$curr_page];
82 //---------------------------
83 // Display link to first page
84 function Render_First($anchor=true)
89 <a href
="<?php echo $PHP_SELF,'?',$this->id;?>_next_page=1&<?=$this->qs?>"><?php
echo $this->first
;?
></a
>  
;
92 print "$this->first ";
96 //--------------------------
97 // Display link to next page
98 function render_next($anchor=true)
104 <a href
="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() + 1 ?>&<?=$this->qs?>"><?php
echo $this->next
;?
></a
>  
;
107 print "$this->next ";
114 // for better performance with large recordsets, you can set
115 // $this->db->pageExecuteCountRows = false, which disables
116 // last page counting.
117 function render_last($anchor=true)
121 if (!$this->db
->pageExecuteCountRows
) return;
125 <a href
="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->LastPageNo() ?>&<?=$this->qs?>"><?php
echo $this->last
;?
></a
>  
;
128 print "$this->last ";
132 //---------------------------------------------------
133 // original code by "Pablo Costa" <pablo@cbsp.com.br>
134 function render_pagelinks()
137 $pages = $this->rs
->LastPageNo();
138 $linksperpage = $this->linksPerPage ?
$this->linksPerPage
: $pages;
139 for($i=1; $i <= $pages; $i+
=$linksperpage)
141 if($this->rs
->AbsolutePage() >= $i)
147 $end = $start+
$linksperpage-1;
148 $link = $this->id
. "_next_page";
149 if($end > $pages) $end = $pages;
152 if ($this->startLinks
&& $start > 1) {
154 $numbers .= "<a href=$PHP_SELF?$link=$pos>$this->startLinks</a> ";
157 for($i=$start; $i <= $end; $i++
) {
158 if ($this->rs
->AbsolutePage() == $i)
159 $numbers .= "<font color=$this->linkSelectedColor><b>$i</b></font> ";
161 $numbers .= "<a href=$PHP_SELF?$link=$i>$i</a> ";
164 if ($this->moreLinks
&& $end < $pages)
165 $numbers .= "<a href=$PHP_SELF?$link=$i>$this->moreLinks</a> ";
166 print $numbers . ' ';
168 // Link to previous page
169 function render_prev($anchor=true)
174 <a href
="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() - 1 ?>&<?=$this->qs?>"><?php
echo $this->prev
;?
></a
>  
;
177 print "$this->prev ";
181 //--------------------------------------------------------
182 // Simply rendering of grid. You should override this for
183 // better control over the format of the grid
185 // We use output buffering to keep code clean and readable.
186 function RenderGrid()
188 global $gSQLBlockRows; // used by rs2html to indicate how many rows to display
189 include_once(ADODB_DIR
.'/tohtml.inc.php');
191 $gSQLBlockRows = $this->rows
;
192 rs2html($this->rs
,$this->gridAttributes
,$this->gridHeader
,$this->htmlSpecialChars
);
193 $s = ob_get_contents();
198 //-------------------------------------------------------
201 // we use output buffering to keep the code easy to read.
205 if (!$this->rs
->AtFirstPage()) {
206 $this->Render_First();
207 $this->Render_Prev();
209 $this->Render_First(false);
210 $this->Render_Prev(false);
212 if ($this->showPageLinks
){
213 $this->Render_PageLinks();
215 if (!$this->rs
->AtLastPage()) {
216 $this->Render_Next();
217 $this->Render_Last();
219 $this->Render_Next(false);
220 $this->Render_Last(false);
222 $s = ob_get_contents();
227 //-------------------
228 // This is the footer
229 function RenderPageCount()
231 if (!$this->db
->pageExecuteCountRows
) return '';
232 $lastPage = $this->rs
->LastPageNo();
233 if ($lastPage == -1) $lastPage = 1; // check for empty rs.
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 $savec = $ADODB_COUNTRECS;
247 if ($this->db
->pageExecuteCountRows
) $ADODB_COUNTRECS = true;
249 $rs = &$this->db
->CachePageExecute($this->cache
,$this->sql
,$rows,$this->curr_page
);
251 $sqls = split(";",$this->sql
);
252 foreach ($sqls as $sql) {
253 if (strlen($sql) > 1) {
254 $rs = &$this->db
->PageExecute($sql,$rows,$this->curr_page
);
258 $ADODB_COUNTRECS = $savec;
262 print "<h3>Query failed: $this->sql, " . $this->db
->ErrorMsg() . "</h3>";
266 if (!$rs->EOF
&& (!$rs->AtFirstPage() ||
!$rs->AtLastPage()))
267 $header = $this->RenderNav();
271 $grid = $this->RenderGrid();
272 $footer = $this->RenderPageCount();
276 $this->RenderLayout($header,$grid,$footer);
279 //------------------------------------------------------
280 // override this to control overall layout and formating
281 function RenderLayout($header,$grid,$footer,$attributes='border=1')
283 echo "<table ".$attributes."><tr><td>",
285 "</td></tr><tr><td>",
287 "</td></tr><tr><td>",
289 "</td></tr></table>";