Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.86 KB | None | 0 0
  1. <?php
  2. // SAMPLE
  3. //
  4. // $q = $db->execute("SELECT COUNT(*) FROM table");
  5. // $r = $db->fetchrow($q);
  6. // Pagination::initialize($numrows = $r[0], 10);
  7. // echo Pagination::show("SELECT column FROM table", "<div></div>");
  8.  
  9. class Pagination
  10. {
  11.     private static $numrows;
  12.     private static $rowsperpage;
  13.    
  14.     private static $curPage;
  15.    
  16.     private static $curRow;
  17.    
  18.     private static $firstPage = 1;
  19.     private static $lastPage;
  20.  
  21.     public static function initialize($numrows, $rowsperpage = 10)
  22.     {
  23.         self::$numrows = $numrows;
  24.         self::$rowsperpage = $rowsperpage;
  25.  
  26.         self::$lastPage = ceil($totalRows / $rowsPerPage) - 1;
  27.        
  28.         self::$curPage = self::getCurrentPage();
  29.        
  30.         self::$curRow = (self::$curPage * self::$rowsPerPage);
  31.     }
  32.  
  33.     public static function show($sql, $html)
  34.     {
  35.         global $db;
  36.        
  37.         $links = generateLinks();
  38.        
  39.         $q = $db->execute($sql . " LIMIT " . self::$curRow . ", " . self::$rowsPerPage);
  40.        
  41.         $show = '';
  42.        
  43.         while($r = $db->fetchassoc($q))
  44.         {
  45.                 $show .= $html;
  46.         }
  47.        
  48.         return($links . $show . $links);
  49.     }
  50.    
  51.     private static function getCurrentPage()
  52.     {
  53.         if(!isset($_GET['page']) || $_GET['page'] < self::$firstPage)
  54.         {
  55.             return(self::$firstPage);
  56.         }
  57.         else if($_GET['page'] > self::$lastPage)
  58.         {
  59.             return(self::$lastPage);
  60.         }
  61.         else
  62.         {
  63.             return($_GET['page']);
  64.         }
  65.     }
  66.    
  67.     private static function generateLinks()
  68.     {
  69.         $links = '';
  70.        
  71.         if((self::$curPage - 5) > self::$firstPage)
  72.             $links .= "<a href=\"?page=$firstPage\">First</a> ... ";   
  73.            
  74.         for($i = (self::$curPage - 5); $i <= (self::$curPage + 5); $i++)
  75.         {
  76.             if($i >= self::$firstPage && $i <= self::$lastPage)
  77.             {
  78.                 $links .= "<a href=\"?page=$i\">" . (($i == self::$curPage) ? "[$i]" : $i) . "</a>&nbsp;";
  79.             }
  80.         }
  81.        
  82.         if(($currentPage + 5) < $lastPage)
  83.             $links = " ... <a href=\"?page=$lastPage\">Last</a>";
  84.            
  85.         return($links);
  86.     }
  87. }
  88. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement