00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 class ADODB_Pager {
00025 var $id;
00026 var $db;
00027 var $sql;
00028 var $rs;
00029 var $curr_page;
00030 var $rows;
00031 var $linksPerPage=10;
00032 var $showPageLinks;
00033
00034 var $gridAttributes = 'width=100% border=1 bgcolor=white';
00035
00036
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
00051
00052
00053
00054
00055
00056
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']);
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
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
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
00112
00113
00114
00115
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
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
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
00182
00183
00184
00185 function RenderGrid()
00186 {
00187 global $gSQLBlockRows;
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
00199
00200
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
00228 function RenderPageCount()
00229 {
00230 if (!$this->db->pageExecuteCountRows) return '';
00231 $lastPage = $this->rs->LastPageNo();
00232 if ($lastPage == -1) $lastPage = 1;
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
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
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 ?>