* @version 02-02-05 * @package Pager * * Copyright (C) 2003 - 2005 phpScheduleIt * License: GPL, see LICENSE * / /** * To actually print out page links, call printPages() function * * In order for this object to work correctly, total records * must be set in either the constructor or by calling * setTotRecords() * * + Warning - The printPages() function cannot be called * from within a form * * === EXAMPLE OF HOW TO USE PAGER === * // Initialize new Pager object with default values * $pager = new Pager(); * * // Get total # of pages * $query = "SELECT COUNT(*) as num FROM table"; * $result = $db->query($query); * $rs = $result->fetchRow(); * $num = $rs['num']; // # of records * * $pager->setTotRecords($num); * $recordset_offset = $pager->getOffset(); * $limit = $pager->getLimit(); * * // Execute Query (using $offset and $limit values) // * * $pager->printPages(); * ========================================== * */ // Should we use the Link class? $use_link = true; if ($use_link) { //include_once('Link.class.php'); $link = new Link(); } class Pager { // Application set variables var $cur_page; var $query_string; var $tot_pages; var $page_var; var $limit_var; // Application variables with user modify option var $limit; var $tot_records; var $print_limit_select = true; // User modifiable variables var $prev_link = '«'; var $next_link = '»'; var $limits = array(10, 25, 50, 100); var $view_pages = 3; var $table_width = '100%'; var $table_align = 'center'; var $link_class; var $tb_class; var $tb_style; var $text_class; var $text_style; /** * Pager Constructor * Sets up Pager variables and initializes values * * - All parameters are optional and have default values of: * $tot_records = 0 * $limit = 25 * $page_var = "page" * $limit_var = "limit" * * @param int $tot_records optional total number of records * @param int $limit optional limit of recordset * @param string $page_var optional name of var to use in querystring for page value * @param string $limit_var optional name of var to use in querystring for limit value */ function Pager($tot_records=0, $limit=25, $page_var='page', $limit_var='limit') { $this->tot_records = $tot_records; $this->limit = $limit; $this->page_var = $page_var; $this->limit_var = $limit_var; // Call all system setter functions $this->initCurPage(); $this->initLimit(); $this->initTotPages(); $this->initQueryString(); } /** * Print out the pages as links * Prints out a table of all the pages as links * and a jump menu to change the number of records * per page * * setCurPage() and setTotPages() must be called * before this function can be called * * @param none * @see printPrev() * @see printLink() * @see printPage() * @see printNext() * @see printTotal() * @see startTable() * @see startPagesCell() * @see endPagesCell() * @see printLimitCell() * @see endTable() */ function printPages() { $p = $this->view_pages; // How many pages to view $cur_page = $this->cur_page; // Current page $tot_pages = $this->tot_pages; // Total pages // Open up the HTML table $this->startTable(); // Open up cell for page links $this->startPagesCell(); // Page to start printing bulk of links $start = ($cur_page > $p) ? $cur_page - $p : 1; // Page to end printing bulk of links $end = ($cur_page + $p) < $tot_pages ? $cur_page + $p : $tot_pages; // Print 'prev' link $this->printPrev(); // Print link to first page, if not already there if ($start != 1) { $this->printPage(1); } // Print '...' if necessary (with link to center page) if ($cur_page > $p+2) { $this->printLink(ceil( ($start+1)/2 ), '...'); } // Print links to pages before current page (up to first page) // Print current page // Print links to pages after current page (up to last page) for ($pg = $start; $pg <= $end; $pg++) { $this->printPage($pg); } // Print '...' if necessary (with link to center page) if ( $cur_page < ($tot_pages - ($p+1)) ) { $this->printLink(ceil( ($tot_pages+$end)/2 ), '...' ); } // Print link to last page, if not already there if ($end != $tot_pages) { $this->printPage($tot_pages); } // Print 'next' link $this->printNext(); // Print total records $this->printTotal(); // Close page links cell $this->endPagesCell(); // Print out cell with limit jump menu if ($this->print_limit_select) { $this->printLimitCell(); } // Close table $this->endTable(); } //----------------------------------------- // Application setter functions //----------------------------------------- /** * Sets current page variable * @param none */ function initCurPage() { $this->cur_page = isset($_GET[$this->page_var]) ? intval($_GET[$this->page_var]) : 1; } /** * Sets the limit variable if it is passed from querystring * @param none */ function initLimit() { if (isset($_GET[$this->limit_var])) $this->limit = intval($_GET[$this->limit_var]); if (isset($_POST[$this->limit_var])) $this->limit = intval($_POST[$this->limit_var]); } /** * Pull page information from query string and set $query_string * * setLimit() must be called before this function for it to work correctly * @param none */ function initQueryString() { if (isset($_SERVER['QUERY_STRING'])) { // Remove page from query string and convert all "&" to "&" $this->query_string = str_replace('&', '&', preg_replace("/(&|&)?$this->page_var=\d*/",'',$_SERVER['QUERY_STRING'])); // Insert limit into querystring, if it's not there if ( !strstr($this->query_string, "$this->limit_var=") ) $this->query_string .= "&$this->limit_var=" . $this->limit; } else { $this->query_string = ''; } } /** * Sets the tot_pages variable * * tot_records must be set and setLimit() must be called before * this function can be called * * @param none */ function initTotPages() { $this->tot_pages = ceil($this->tot_records/$this->limit); } //=========================================== //------------------------------------------- // Output functions //------------------------------------------- /** * Print out link to a page * @param int $p page number to print */ function printPage($p) { if ($p == $this->cur_page) { echo " [$p] "; } else { $this->printLink($p, $p); } } /** * Print 'prev' link, if necessary * @param none */ function printPrev() { $cur_page = $this->cur_page; if ($cur_page > 1) $this->printLink($cur_page-1, $this->prev_link); } /** * Print 'next' link, if necessary * @param none */ function printNext() { $cur_page = $this->cur_page; if ($cur_page < $this->tot_pages && $this->tot_records > 0) $this->printLink($cur_page+1, $this->next_link); } /** * Print out link to a certain page * @param int $page page to link to * @param string $text link text */ function printLink($page, $text) { global $link; global $use_link; if ($use_link) { $link->doLink( $_SERVER['PHP_SELF'] . "?$this->page_var=$page&" . $this->query_string . '"', $text, $this->link_class, '', 'Page ' . $page ); } else { echo ' page_var=$page&" . $this->query_string . '"' . ' class="$this->class"' . '>' . $text . ' '; } } /** * Prints out opening table tag * @param none */ function startTable() { echo "
' . translate('Page') . ' '; } /** * Closes cell containing page links * @param none */ function endPagesCell() { echo ' | '; } /** * Prints out cell containing limit jump menu * @param none */ function printLimitCell() { $limits = $this->limits; echo "\n" . "\n" . " | \n"; } /** * Prints out the closing row and table HTML tags * @param none */ function endTable() { echo "