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::show("SELECT column FROM table", "<div></div>");
- 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 show($sql, $html)
- {
- global $db;
- $links = generateLinks();
- $q = $db->execute($sql . " LIMIT " . self::$curRow . ", " . self::$rowsPerPage);
- $show = '';
- while($r = $db->fetchassoc($q))
- {
- $show .= $html;
- }
- return($links . $show . $links);
- }
- 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']);
- }
- }
- private 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