home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / adodb-pager.inc.php < prev    next >
Encoding:
PHP Script  |  2004-03-20  |  8.1 KB  |  285 lines

  1. <?php
  2. /*
  3.     V4.21 20 Mar 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.
  8.  
  9.       This class provides recordset pagination with 
  10.     First/Prev/Next/Last links. 
  11.     
  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.
  15.     
  16.     "Pablo Costa" <pablo@cbsp.com.br> implemented Render_PageLinks().
  17.     
  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.
  21.  
  22. */
  23. class ADODB_Pager {
  24.     var $id;     // unique id for pager (defaults to 'adodb')
  25.     var $db;     // ADODB connection object
  26.     var $sql;     // sql used
  27.     var $rs;    // recordset generated
  28.     var $curr_page;    // current page number before Render() called, calculated in constructor
  29.     var $rows;        // number of rows per page
  30.     var $linksPerPage=10; // number of links per page in navigation bar
  31.     var $showPageLinks; 
  32.  
  33.     var $gridAttributes = 'width=100% border=1 bgcolor=white';
  34.     
  35.     // Localize text strings here
  36.     var $first = '<code>|<</code>';
  37.     var $prev = '<code><<</code>';
  38.     var $next = '<code>>></code>';
  39.     var $last = '<code>>|</code>';
  40.     var $moreLinks = '...';
  41.     var $startLinks = '...';
  42.     var $gridHeader = false;
  43.     var $htmlSpecialChars = true;
  44.     var $page = 'Page';
  45.     var $linkSelectedColor = 'red';
  46.     var $cache = 0;  #secs to cache with CachePageExecute()
  47.     
  48.     //----------------------------------------------
  49.     // constructor
  50.     //
  51.     // $db    adodb connection object
  52.     // $sql    sql statement
  53.     // $id    optional id to identify which pager, 
  54.     //        if you have multiple on 1 page. 
  55.     //        $id should be only be [a-z0-9]*
  56.     //
  57.     function ADODB_Pager(&$db,$sql,$id = 'adodb', $showPageLinks = false)
  58.     {
  59.     global $HTTP_SERVER_VARS,$PHP_SELF,$HTTP_SESSION_VARS,$HTTP_GET_VARS;
  60.     
  61.         $curr_page = $id.'_curr_page';
  62.         if (empty($PHP_SELF)) $PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF'];
  63.         
  64.         $this->sql = $sql;
  65.         $this->id = $id;
  66.         $this->db = $db;
  67.         $this->showPageLinks = $showPageLinks;
  68.         
  69.         $next_page = $id.'_next_page';    
  70.         
  71.         if (isset($HTTP_GET_VARS[$next_page])) {
  72.             $HTTP_SESSION_VARS[$curr_page] = $HTTP_GET_VARS[$next_page];
  73.         }
  74.         if (empty($HTTP_SESSION_VARS[$curr_page])) $HTTP_SESSION_VARS[$curr_page] = 1; ## at first page
  75.         
  76.         $this->curr_page = $HTTP_SESSION_VARS[$curr_page];
  77.         
  78.     }
  79.     
  80.     //---------------------------
  81.     // Display link to first page
  82.     function Render_First($anchor=true)
  83.     {
  84.     global $PHP_SELF;
  85.         if ($anchor) {
  86.     ?>
  87.         <a href="<?php echo $PHP_SELF,'?',$this->id;?>_next_page=1"><?php echo $this->first;?></a>   
  88.     <?php
  89.         } else {
  90.             print "$this->first   ";
  91.         }
  92.     }
  93.     
  94.     //--------------------------
  95.     // Display link to next page
  96.     function render_next($anchor=true)
  97.     {
  98.     global $PHP_SELF;
  99.     
  100.         if ($anchor) {
  101.         ?>
  102.         <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() + 1 ?>"><?php echo $this->next;?></a>   
  103.         <?php
  104.         } else {
  105.             print "$this->next   ";
  106.         }
  107.     }
  108.     
  109.     //------------------
  110.     // Link to last page
  111.     // 
  112.     // for better performance with large recordsets, you can set
  113.     // $this->db->pageExecuteCountRows = false, which disables
  114.     // last page counting.
  115.     function render_last($anchor=true)
  116.     {
  117.     global $PHP_SELF;
  118.     
  119.         if (!$this->db->pageExecuteCountRows) return;
  120.         
  121.         if ($anchor) {
  122.         ?>
  123.             <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->LastPageNo() ?>"><?php echo $this->last;?></a>   
  124.         <?php
  125.         } else {
  126.             print "$this->last   ";
  127.         }
  128.     }
  129.     
  130.     //---------------------------------------------------
  131.     // original code by "Pablo Costa" <pablo@cbsp.com.br> 
  132.         function render_pagelinks()
  133.         {
  134.         global $PHP_SELF;
  135.             $pages        = $this->rs->LastPageNo();
  136.             $linksperpage = $this->linksPerPage ? $this->linksPerPage : $pages;
  137.             for($i=1; $i <= $pages; $i+=$linksperpage)
  138.             {
  139.                 if($this->rs->AbsolutePage() >= $i)
  140.                 {
  141.                     $start = $i;
  142.                 }
  143.             }
  144.             $numbers = '';
  145.             $end = $start+$linksperpage-1;
  146.             $link = $this->id . "_next_page";
  147.             if($end > $pages) $end = $pages;
  148.             
  149.             
  150.             if ($this->startLinks && $start > 1) {
  151.                 $pos = $start - 1;
  152.                 $numbers .= "<a href=$PHP_SELF?$link=$pos>$this->startLinks</a>  ";
  153.             } 
  154.             
  155.             for($i=$start; $i <= $end; $i++) {
  156.                 if ($this->rs->AbsolutePage() == $i)
  157.                     $numbers .= "<font color=$this->linkSelectedColor><b>$i</b></font>  ";
  158.                 else 
  159.                      $numbers .= "<a href=$PHP_SELF?$link=$i>$i</a>  ";
  160.             
  161.             }
  162.             if ($this->moreLinks && $end < $pages) 
  163.                 $numbers .= "<a href=$PHP_SELF?$link=$i>$this->moreLinks</a>  ";
  164.             print $numbers . '   ';
  165.         }
  166.     // Link to previous page
  167.     function render_prev($anchor=true)
  168.     {
  169.     global $PHP_SELF;
  170.         if ($anchor) {
  171.     ?>
  172.         <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() - 1 ?>"><?php echo $this->prev;?></a>   
  173.     <?php 
  174.         } else {
  175.             print "$this->prev   ";
  176.         }
  177.     }
  178.     
  179.     //--------------------------------------------------------
  180.     // Simply rendering of grid. You should override this for
  181.     // better control over the format of the grid
  182.     //
  183.     // We use output buffering to keep code clean and readable.
  184.     function RenderGrid()
  185.     {
  186.     global $gSQLBlockRows; // used by rs2html to indicate how many rows to display
  187.         include_once(ADODB_DIR.'/tohtml.inc.php');
  188.         ob_start();
  189.         $gSQLBlockRows = $this->rows;
  190.         rs2html($this->rs,$this->gridAttributes,$this->gridHeader,$this->htmlSpecialChars);
  191.         $s = ob_get_contents();
  192.         ob_end_clean();
  193.         return $s;
  194.     }
  195.     
  196.     //-------------------------------------------------------
  197.     // Navigation bar
  198.     //
  199.     // we use output buffering to keep the code easy to read.
  200.     function RenderNav()
  201.     {
  202.         ob_start();
  203.         if (!$this->rs->AtFirstPage()) {
  204.             $this->Render_First();
  205.             $this->Render_Prev();
  206.         } else {
  207.             $this->Render_First(false);
  208.             $this->Render_Prev(false);
  209.         }
  210.         if ($this->showPageLinks){
  211.             $this->Render_PageLinks();
  212.         }
  213.         if (!$this->rs->AtLastPage()) {
  214.             $this->Render_Next();
  215.             $this->Render_Last();
  216.         } else {
  217.             $this->Render_Next(false);
  218.             $this->Render_Last(false);
  219.         }
  220.         $s = ob_get_contents();
  221.         ob_end_clean();
  222.         return $s;
  223.     }
  224.     
  225.     //-------------------
  226.     // This is the footer
  227.     function RenderPageCount()
  228.     {
  229.         if (!$this->db->pageExecuteCountRows) return '';
  230.         $lastPage = $this->rs->LastPageNo();
  231.         if ($lastPage == -1) $lastPage = 1; // check for empty rs.
  232.         return "<font size=-1>$this->page ".$this->curr_page."/".$lastPage."</font>";
  233.     }
  234.     
  235.     //-----------------------------------
  236.     // Call this class to draw everything.
  237.     function Render($rows=10)
  238.     {
  239.     global $ADODB_COUNTRECS;
  240.     
  241.         $this->rows = $rows;
  242.         
  243.         $savec = $ADODB_COUNTRECS;
  244.         if ($this->db->pageExecuteCountRows) $ADODB_COUNTRECS = true;
  245.         if ($this->cache)
  246.             $rs = &$this->db->CachePageExecute($this->cache,$this->sql,$rows,$this->curr_page);
  247.         else
  248.             $rs = &$this->db->PageExecute($this->sql,$rows,$this->curr_page);
  249.         $ADODB_COUNTRECS = $savec;
  250.         
  251.         $this->rs = &$rs;
  252.         if (!$rs) {
  253.             print "<h3>Query failed: $this->sql</h3>";
  254.             return;
  255.         }
  256.         
  257.         if (!$rs->EOF && (!$rs->AtFirstPage() || !$rs->AtLastPage())) 
  258.             $header = $this->RenderNav();
  259.         else
  260.             $header = " ";
  261.         
  262.         $grid = $this->RenderGrid();
  263.         $footer = $this->RenderPageCount();
  264.         $rs->Close();
  265.         $this->rs = false;
  266.         
  267.         $this->RenderLayout($header,$grid,$footer);
  268.     }
  269.     
  270.     //------------------------------------------------------
  271.     // override this to control overall layout and formating
  272.     function RenderLayout($header,$grid,$footer,$attributes='border=1 bgcolor=beige')
  273.     {
  274.         echo "<table ".$attributes."><tr><td>",
  275.                 $header,
  276.             "</td></tr><tr><td>",
  277.                 $grid,
  278.             "</td></tr><tr><td>",
  279.                 $footer,
  280.             "</td></tr></table>";
  281.     }
  282. }
  283.  
  284.  
  285. ?>