Documentation TYPO3 par Ameos |
00001 <?php 00002 00003 /* 00004 V4.93 10 Oct 2006 (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved. 00005 Released under both BSD license and Lesser GPL library license. 00006 Whenever there is any discrepancy between the two licenses, 00007 the BSD license will take precedence. 00008 Set tabs to 4 for best viewing. 00009 00010 This class provides recordset pagination with 00011 First/Prev/Next/Last links. 00012 00013 Feel free to modify this class for your own use as 00014 it is very basic. To learn how to use it, see the 00015 example in adodb/tests/testpaging.php. 00016 00017 "Pablo Costa" <pablo@cbsp.com.br> implemented Render_PageLinks(). 00018 00019 Please note, this class is entirely unsupported, 00020 and no free support requests except for bug reports 00021 will be entertained by the author. 00022 00023 */ 00024 class ADODB_Pager { 00025 var $id; // unique id for pager (defaults to 'adodb') 00026 var $db; // ADODB connection object 00027 var $sql; // sql used 00028 var $rs; // recordset generated 00029 var $curr_page; // current page number before Render() called, calculated in constructor 00030 var $rows; // number of rows per page 00031 var $linksPerPage=10; // number of links per page in navigation bar 00032 var $showPageLinks; 00033 00034 var $gridAttributes = 'width=100% border=1 bgcolor=white'; 00035 00036 // Localize text strings here 00037 var $first = '<code>|<</code>'; 00038 var $prev = '<code><<</code>'; 00039 var $next = '<code>>></code>'; 00040 var $last = '<code>>|</code>'; 00041 var $moreLinks = '...'; 00042 var $startLinks = '...'; 00043 var $gridHeader = false; 00044 var $htmlSpecialChars = true; 00045 var $page = 'Page'; 00046 var $linkSelectedColor = 'red'; 00047 var $cache = 0; #secs to cache with CachePageExecute() 00048 00049 //---------------------------------------------- 00050 // constructor 00051 // 00052 // $db adodb connection object 00053 // $sql sql statement 00054 // $id optional id to identify which pager, 00055 // if you have multiple on 1 page. 00056 // $id should be only be [a-z0-9]* 00057 // 00058 function ADODB_Pager(&$db,$sql,$id = 'adodb', $showPageLinks = false) 00059 { 00060 global $PHP_SELF; 00061 00062 $curr_page = $id.'_curr_page'; 00063 if (empty($PHP_SELF)) $PHP_SELF = htmlspecialchars($_SERVER['PHP_SELF']); // htmlspecialchars() to prevent XSS attacks 00064 00065 $this->sql = $sql; 00066 $this->id = $id; 00067 $this->db = $db; 00068 $this->showPageLinks = $showPageLinks; 00069 00070 $next_page = $id.'_next_page'; 00071 00072 if (isset($_GET[$next_page])) { 00073 $_SESSION[$curr_page] = (integer) $_GET[$next_page]; 00074 } 00075 if (empty($_SESSION[$curr_page])) $_SESSION[$curr_page] = 1; ## at first page 00076 00077 $this->curr_page = $_SESSION[$curr_page]; 00078 00079 } 00080 00081 //--------------------------- 00082 // Display link to first page 00083 function Render_First($anchor=true) 00084 { 00085 global $PHP_SELF; 00086 if ($anchor) { 00087 ?> 00088 <a href="<?php echo $PHP_SELF,'?',$this->id;?>_next_page=1"><?php echo $this->first;?></a> 00089 <?php 00090 } else { 00091 print "$this->first "; 00092 } 00093 } 00094 00095 //-------------------------- 00096 // Display link to next page 00097 function render_next($anchor=true) 00098 { 00099 global $PHP_SELF; 00100 00101 if ($anchor) { 00102 ?> 00103 <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() + 1 ?>"><?php echo $this->next;?></a> 00104 <?php 00105 } else { 00106 print "$this->next "; 00107 } 00108 } 00109 00110 //------------------ 00111 // Link to last page 00112 // 00113 // for better performance with large recordsets, you can set 00114 // $this->db->pageExecuteCountRows = false, which disables 00115 // last page counting. 00116 function render_last($anchor=true) 00117 { 00118 global $PHP_SELF; 00119 00120 if (!$this->db->pageExecuteCountRows) return; 00121 00122 if ($anchor) { 00123 ?> 00124 <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->LastPageNo() ?>"><?php echo $this->last;?></a> 00125 <?php 00126 } else { 00127 print "$this->last "; 00128 } 00129 } 00130 00131 //--------------------------------------------------- 00132 // original code by "Pablo Costa" <pablo@cbsp.com.br> 00133 function render_pagelinks() 00134 { 00135 global $PHP_SELF; 00136 $pages = $this->rs->LastPageNo(); 00137 $linksperpage = $this->linksPerPage ? $this->linksPerPage : $pages; 00138 for($i=1; $i <= $pages; $i+=$linksperpage) 00139 { 00140 if($this->rs->AbsolutePage() >= $i) 00141 { 00142 $start = $i; 00143 } 00144 } 00145 $numbers = ''; 00146 $end = $start+$linksperpage-1; 00147 $link = $this->id . "_next_page"; 00148 if($end > $pages) $end = $pages; 00149 00150 00151 if ($this->startLinks && $start > 1) { 00152 $pos = $start - 1; 00153 $numbers .= "<a href=$PHP_SELF?$link=$pos>$this->startLinks</a> "; 00154 } 00155 00156 for($i=$start; $i <= $end; $i++) { 00157 if ($this->rs->AbsolutePage() == $i) 00158 $numbers .= "<font color=$this->linkSelectedColor><b>$i</b></font> "; 00159 else 00160 $numbers .= "<a href=$PHP_SELF?$link=$i>$i</a> "; 00161 00162 } 00163 if ($this->moreLinks && $end < $pages) 00164 $numbers .= "<a href=$PHP_SELF?$link=$i>$this->moreLinks</a> "; 00165 print $numbers . ' '; 00166 } 00167 // Link to previous page 00168 function render_prev($anchor=true) 00169 { 00170 global $PHP_SELF; 00171 if ($anchor) { 00172 ?> 00173 <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() - 1 ?>"><?php echo $this->prev;?></a> 00174 <?php 00175 } else { 00176 print "$this->prev "; 00177 } 00178 } 00179 00180 //-------------------------------------------------------- 00181 // Simply rendering of grid. You should override this for 00182 // better control over the format of the grid 00183 // 00184 // We use output buffering to keep code clean and readable. 00185 function RenderGrid() 00186 { 00187 global $gSQLBlockRows; // used by rs2html to indicate how many rows to display 00188 include_once(ADODB_DIR.'/tohtml.inc.php'); 00189 ob_start(); 00190 $gSQLBlockRows = $this->rows; 00191 rs2html($this->rs,$this->gridAttributes,$this->gridHeader,$this->htmlSpecialChars); 00192 $s = ob_get_contents(); 00193 ob_end_clean(); 00194 return $s; 00195 } 00196 00197 //------------------------------------------------------- 00198 // Navigation bar 00199 // 00200 // we use output buffering to keep the code easy to read. 00201 function RenderNav() 00202 { 00203 ob_start(); 00204 if (!$this->rs->AtFirstPage()) { 00205 $this->Render_First(); 00206 $this->Render_Prev(); 00207 } else { 00208 $this->Render_First(false); 00209 $this->Render_Prev(false); 00210 } 00211 if ($this->showPageLinks){ 00212 $this->Render_PageLinks(); 00213 } 00214 if (!$this->rs->AtLastPage()) { 00215 $this->Render_Next(); 00216 $this->Render_Last(); 00217 } else { 00218 $this->Render_Next(false); 00219 $this->Render_Last(false); 00220 } 00221 $s = ob_get_contents(); 00222 ob_end_clean(); 00223 return $s; 00224 } 00225 00226 //------------------- 00227 // This is the footer 00228 function RenderPageCount() 00229 { 00230 if (!$this->db->pageExecuteCountRows) return ''; 00231 $lastPage = $this->rs->LastPageNo(); 00232 if ($lastPage == -1) $lastPage = 1; // check for empty rs. 00233 if ($this->curr_page > $lastPage) $this->curr_page = 1; 00234 return "<font size=-1>$this->page ".$this->curr_page."/".$lastPage."</font>"; 00235 } 00236 00237 //----------------------------------- 00238 // Call this class to draw everything. 00239 function Render($rows=10) 00240 { 00241 global $ADODB_COUNTRECS; 00242 00243 $this->rows = $rows; 00244 00245 if ($this->db->dataProvider == 'informix') $this->db->cursorType = IFX_SCROLL; 00246 00247 $savec = $ADODB_COUNTRECS; 00248 if ($this->db->pageExecuteCountRows) $ADODB_COUNTRECS = true; 00249 if ($this->cache) 00250 $rs = &$this->db->CachePageExecute($this->cache,$this->sql,$rows,$this->curr_page); 00251 else 00252 $rs = &$this->db->PageExecute($this->sql,$rows,$this->curr_page); 00253 $ADODB_COUNTRECS = $savec; 00254 00255 $this->rs = &$rs; 00256 if (!$rs) { 00257 print "<h3>Query failed: $this->sql</h3>"; 00258 return; 00259 } 00260 00261 if (!$rs->EOF && (!$rs->AtFirstPage() || !$rs->AtLastPage())) 00262 $header = $this->RenderNav(); 00263 else 00264 $header = " "; 00265 00266 $grid = $this->RenderGrid(); 00267 $footer = $this->RenderPageCount(); 00268 00269 $this->RenderLayout($header,$grid,$footer); 00270 00271 $rs->Close(); 00272 $this->rs = false; 00273 } 00274 00275 //------------------------------------------------------ 00276 // override this to control overall layout and formating 00277 function RenderLayout($header,$grid,$footer,$attributes='border=1 bgcolor=beige') 00278 { 00279 echo "<table ".$attributes."><tr><td>", 00280 $header, 00281 "</td></tr><tr><td>", 00282 $grid, 00283 "</td></tr><tr><td>", 00284 $footer, 00285 "</td></tr></table>"; 00286 } 00287 } 00288 00289 00290 ?>