Advertisement
Guest User

Untitled

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