getLink("first") . ' | ' . $pager->getLink("previous"); * $pager->printPager(); * print $pager->getLink("next") . ' | ' . $pager->getLink("last"); * $this_page = $pager->getPage(); */ class SqlPager { // Public Variables var $query; /// The SQL Query to be paged. var $items_per_page; /// The Number of items that should be shown in 1 page var $page; /// The current page number var $total_items; /// Total number of records var $total_pages; /// Total number of pages var $page_link = ""; /// The file that must be used while making links to pages. /** * This is the template used to create links when the getLink() is called. * %%PAGE_LINK%% - current page url & sp_page=PageNumber . Example: demo.php?param=value&sp_page=3 */ var $link_template = '%%TEXT%%'; /** * This is the template used to create links when the getStatus() is called. Possible substitutions... * %%PAGE%% * %%FROM%% * %%TO%% * %%TOTAL_PAGES%% * %%TOTAL_ITEMS%% */ var $status_template = 'Viewing %%FROM%% - %%TO%% of %%TOTAL_ITEMS%%'; ///The current mode of the system. Possible values are 'd' = Development Mode, 't' = Testing Mode, 'p' = Production Mode var $mode = 'd'; var $opt = array( 'links_count' => 10, // If 0, shows all links ); ///The text used in the pager is customizable. HTML is allowed. var $text = array( 'previous' => '<' , 'next' => '>', //Next and Previous Texts 'first' => '<<', 'last' => '>>', //First and Last Links 'current_page_indicator' => array( 'left' => '[', 'right' => ']' ), 'link_seperator' => " " //The text between the paging links(Page numbers). ); ///Private Variables var $_sql_resource; var $_pager_query; /** * Constructor * Argument : $query - The query that should be used for paging * $items_per_page - The default number of items per page - defaults to 10 [OPTIONAL] */ function SqlPager($query, $items_per_page = 10) { $query = preg_replace('/ LIMIT .+/i','',$query);//Remove the 'limitation' if there is one $this->query = $query; //Before anything, get all the necessary data. //Get the value of $items_per_page from the query or from the parameter $this->items_per_page = ($this->_getParam('sp_items_per_page')) ? $this->_getParam('sp_items_per_page') : $items_per_page; //Get the current page number $this->page = ($this->_getParam('sp_page')) ? $this->_getParam('sp_page') : 1; $this->page_link = basename($_SERVER['PHP_SELF']); if($_SERVER["QUERY_STRING"]) $this->page_link .= '?' + $_SERVER["QUERY_STRING"]; $offset = ($this->page - 1) * $this->items_per_page; $this->_pager_query = $query . " LIMIT $offset," . $this->items_per_page; global $sql; $total_items_sql = $sql->getSql($this->query); $this->total_items = $sql->fetchNumRows($total_items_sql); $this->total_pages = ceil($this->total_items / $this->items_per_page); } /** * Returns the SQL resource of the pager. * Return : The SQL resource of the pager. */ function getSql() { global $sql; $this->_sql_resource = $sql->getSql($this->_pager_query); return $this->_sql_resource; } /** * Returns all the items for one page in an array. * Returns : All the items for one page in a list. */ function getPage() { global $sql; return $sql->getAll($this->_pager_query); } //////////////////////////////////// Functions that print /////////////////////////////////////// /** * Prints the pager. Shows links to pages as numbers - except for the current page. The number of links to be shown * is decided by the $opt['links_count'] member variable. If that is 0, all links are shown. If its 5, then * 5 links are shown. */ function printPager($return_data = false) { if($this->total_pages == 1) return; //Just 1 page - don't need pager. $from = 1; $to = $this->total_pages; // Decides many page numbers should be shown if($this->opt['links_count']) { $difference = intval($this->opt['links_count'] / 2); $from = $this->page - $difference + 1; $to = $this->page + $difference; //Make sure the numbers are in range. if($from < 1) { //The numbers that cannot be put in the right side can be put in the left side $to = $to + (-$from);//$from is negetive $from = 1; } if($to > $this->total_pages) { $from = $from - ($to - $this->total_pages); $to = $this->total_pages; if($from < 1) $from = 1; } } $to_print = ''; for($i = $from; $i <= $to; $i++) { if($i == $this->page) { //Current Page $to_print .= $this->text['current_page_indicator']['left'] . $i . $this->text['current_page_indicator']['right']; } else { $to_print .= ''.$i.""; } $to_print .= $this->text['link_seperator']; } if($return_data) return $to_print; else print $to_print; } /** * Returns the link to the page of the given page number * Argument: $page - The page number of the page of which's link you want. * Return: The url of the page - index.php?sp_page=2 */ function getLinkToPage($page) { if($page > $this->total_pages) return ''; return $this->_getLinkParameters($this->page_link,array('sp_page'=>$page)); } /** * Return the Next/Previous and First/Last Link based on the argument. The link will be formated * using the $link_template member variable of this class. * Arguments : $dir(String) - the direction of the link you want. Possible values are... * - 'next' get the next link * - 'previous' get the previous link. * - 'first' link to the first * - 'last' links to last page. * Returns: The HTML of the link - based on $link_template member variable */ function getLink($dir) { $dir = strtolower($dir); //We just make the template here. Depending on the value of '$dir' the values to be inserted in // the place of the replacement texts(like %%PAGE%%) will change. $replace_these = array("%%PAGE%%","%%CLASS%%","%%TEXT%%", "%%PAGE_LINK%%"); //Stuff to replace if($dir === "previous" or $dir === "back" or $dir === 'prev') { //Get the back link $back = $this->page-1; $back = ($back > 0) ? $back : 1; //Never let the value of $back be lesser than 1(first page) $with_these = array($back,"previous",$this->text['previous']); } elseif($dir === "next" or $dir === "forward") { //Get the forward link $next = $this->page+1; $next = ($next > $this->total_pages) ? $this->total_pages : $next; //Never let the value of $next go beyond the total number of pages $with_these = array($next,"next",$this->text['next']); } elseif($dir === "first" or $dir === "start" or $dir === 'beginning') { //Get the first link $with_these = array(1,"first",$this->text['first']); } elseif($dir === "last" or $dir === "end") { //Get the last link. $with_these = array($this->total_pages,"last",$this->text['last']); } $page_link = $this->_getLinkParameters($this->page_link, array('sp_page'=> $with_these[0])); array_push($with_these, $page_link); //Add the page link at the beginning of the array. $link = str_replace($replace_these, $with_these, $this->link_template); //Replace the texts return $link; } /** * Create and return a link with the parameters given as the function argument. */ function makeLink($params) { return $this->_getLinkParameters($this->page_link, $params); } //////////////////////////////////////// HTML/JavaScript Blocks /////////////////////////////////// /** * Returns the status of the current page. Use the $status_template member variable as a template. */ function getStatus() { $from = ($this->page * $this->items_per_page) - $this->items_per_page + 1; $to = $from + $this->items_per_page - 1; $to = ($to > $this->total_items) ? $this->total_items : $to; $replace_these = array('%%FROM%%', '%%TO%%', '%%TOTAL_ITEMS%%', '%%PAGE%%', '%%TOTAL_PAGES%%'); $with_these = array($from, $to, $this->total_items, $this->page, $this->total_pages); $status = str_replace($replace_these, $with_these, $this->status_template); return $status; } /** * Shows a Dropdown menu with values 5,10,20, 25, 50, 100. On selecting one option, that much items will be * shown in one page. */ function printItemsPerPageDropDown() { $page_link = $this->_getLinkParameters($this->page_link, array('sp_items_per_page'=>'__ITEMS_PER_PAGE__','sp_page'=>1));//Go to 1st page when items_per_page is changed ?>
_printHiddenFields(array('sp_items_per_page', 'sp_page', 'action')); ?> Show items per page.
total_pages < 10) $size = 1; else if($this->total_pages < 100) $size = 2; else if($this->total_pages < 1000) $size = 3; $this->_printGoToPageJsFunction(); ?>
_printHiddenFields(array('sp_page', 'action')); ?> Page of total_pages?>
_printGoToPageJsFunction(); ?>
_printHiddenFields(array('sp_page', 'action')); ?> Page
$value) { if(!in_array($key, $except)) { print "\n"; } } } /** * Creates a goToPage javascript function. This is used by 2 different methods - * the printGoToInput() and printGoToDropDown(). I am moving it to its own method * to make sure it appears only once. */ function _printGoToPageJsFunction() { static $shown = 0; if($shown) return; //Makes sure this script is inserted only once $shown = 1; $page_link = $this->_getLinkParameters($this->page_link, array('sp_page'=>'__PAGE__')); ?> 7,'sp_items_per_page'=>5)) */ function _getLinkParameters($url,$params=array(),$use_existing_arguments=false) { return getLink($url, $params, $use_existing_arguments); } /** * Shows an error message based on mode. */ function _error($message) { if($this->mode == 'd') die($message); elseif($this->mode == 't') print($message); } }