Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // SAMPLE
- //
- // $q = $db->execute("SELECT COUNT(*) FROM table");
- // $r = $db->fetchrow($q);
- //
- // Pagination::initialize($numrows = $r[0], 10);
- //
- // echo Pagination::generateLinks();
- //
- // $results = Pagination::results("SELECT column FROM table", $db);
- // foreach($results as $res)
- // {
- // echo $res['column'] . '<br />';
- // }
- class Pagination
- {
- private static $numrows;
- private static $rowsperpage;
- private static $curPage;
- private static $curRow;
- private static $firstPage = 1;
- private static $lastPage;
- public static function initialize($numrows, $rowsperpage = 10)
- {
- self::$numrows = $numrows;
- self::$rowsperpage = $rowsperpage;
- self::$lastPage = ceil($totalRows / $rowsPerPage) - 1;
- self::$curPage = self::getCurrentPage();
- self::$curRow = (self::$curPage * self::$rowsPerPage);
- }
- public static function results($sql, $db)
- {
- $q = $db->execute($sql . " LIMIT " . self::$curRow . ", " . self::$rowsPerPage);
- $results = array();
- while($r = $db->fetchassoc($q))
- {
- $results[] = $r;
- }
- return($results);
- }
- private static function getCurrentPage()
- {
- if(!isset($_GET['page']) || $_GET['page'] < self::$firstPage)
- {
- return(self::$firstPage);
- }
- else if($_GET['page'] > self::$lastPage)
- {
- return(self::$lastPage);
- }
- else
- {
- return($_GET['page']);
- }
- }
- public static function generateLinks()
- {
- $links = '';
- if((self::$curPage - 5) > self::$firstPage)
- $links .= "<a href=\"?page=$firstPage\">First</a> ... ";
- for($i = (self::$curPage - 5); $i <= (self::$curPage + 5); $i++)
- {
- if($i >= self::$firstPage && $i <= self::$lastPage)
- {
- $links .= "<a href=\"?page=$i\">" . (($i == self::$curPage) ? "[$i]" : $i) . "</a> ";
- }
- }
- if(($currentPage + 5) < $lastPage)
- $links = " ... <a href=\"?page=$lastPage\">Last</a>";
- return($links);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement