Codice PHP:
class pager {
/**
* Number of results to show per one page
* @var int
*/
var $per_page;
/**
* Currently active page
* @var int
*/
var $page;
/**
* Page text in query string
* @var string
*/
var $page_identifier;
/**
* NEXT button html code template
* "{query_string}" and "{page}" will be automatically replaced by the appropriate values
* @var string
*/
var $but_next = "<a href=\"{query_string}&page={page}\">next</a>";
/**
* PREVIOUS button html code template
* "{query_string}" and "{page}" will be automatically replaced by the appropriate values
* @var string
*/
var $but_prev = "<a href=\"{query_string}&page={page}\">prev</a>";
/**
* Page ( ie.: 1, 2, 3, or 4, etc. ) button html code template
* "{query_string}" and "{page}" will be automatically replaced by the appropriate values
* @var string
*/
var $but_page = "<a href=\"{query_string}&page={page}\">{page}</a>";
/**
* Currently active page button html code template
* "{query_string}" and "{page}" will be automatically replaced by the appropriate values
* @var string
*/
var $but_page_this = "[b]{page}[/b]";
/**
* Separator template
* Will be included among page buttons in page list
* @var string
*/
var $separator = " - ";
/**
* Range display template
* "{from}", "{to}" and "{total}" will be automatically replaced by the appropriate values
* @var string
*/
var $range_display = "{to} of {total} ";
/**
* Total number of results {from} -
* @var int
*/
var $num_results;
/**
* Number of pages currently available
* @var int
*/
var $page_list_size;
/**
* Initial query string for use in button links
* Including "file.php?" (Ie.: "index.php?location=heaven")
* @var string
*/
var $query_string;
/**
* Pager initialization
*
* @param int $num_results total number of results ( you should get this from database before constructing the pager object )
* @param int $per_page number of results to display in one page
* @param string $query_string initial query string ( if you are using this pager to display search results, $query_string should arrive dynamically according to search query )
* @param string $page_id page identifier variable in query string (ie.: "pg");
*/
function pager($num_results, $per_page = 10, $query_string = '', $page_id = "page") {
$this->page = $_REQUEST[$page_id] ? $_REQUEST[$page_id] : 1;
$this->page_identifier = $page_id;
$this->per_page = $per_page;
$this->num_results = $num_results;
$this->query_string = $query_string;
$this->page_list_size = ceil( $this->num_results / $this->per_page );
}
/**
* Gets the result offset for a page.
* NOTICE: If you want to use this function to
* show the logically correct value for output in html (Ie.: "showing results 1 - 10"),
* you should increase the result of this function by 1 (because in real life results
* start from 1, not from 0)
*
* @param int $page the page you want to get the offset for
* @returns int
*/
function getPageFrom($page) {
if($page <= 0) return 0;
if($page > $this->page_list_size) return 0;
$result = ($page-1) * $this->per_page;
$result = ($result >= $this->num_results) ? ($this->num_results - 1) : $result;
return $result;
}
/**
* Gets the tail value of results shown in a page.
* NOTICE: Do NOT use the results of
* this function in database query, it's for outputting the range only.
* (Ie.: "showing results 1 - 10")
*
* @param int $page the page you want to get the tail for
* @returns int
*/
function getPageTo($page) {
if($page <= 0) return 0;
if($page > $this->page_list_size) return 0;
$result = ($page-1) * $this->per_page + $this->per_page;
$result = ($result >= $this->num_results) ? $this->num_results : $result;
return $result;
}
/**
* Generates the NEXT button html code from the predefined template
* (check initial vars for more information)
*
* @param int $page the page you want to get the NEXT button for ( usually the active page )
* @returns string
*/
function getButNext($page) {
$page++;
if($page > $this->page_list_size) return "";
if($page < 1) return "";
$temp = ereg_replace("{query_string}", $this->query_string, $this->but_next);
$result = ereg_replace("{page}", (string)$page, $temp);
return $result;
}
/**
* Generates the PREVIOUS button html code from the predefined template
* (check initial vars for more information)
*
* @param int $page the page you want to get the PREVIOUS button for ( usually the active page )
* @returns string
*/
function getButPrev($page) {
$page--;
if($page < 1) return "";
if($page > $this->page_list_size) return "";
$temp = ereg_replace("{query_string}", $this->query_string, $this->but_prev);
$result = ereg_replace("{page}", (string)$page, $temp);
return $result;
}
/**
* Generates the PAGE button html code from the predefined templates
* (check initial vars for more information)
*
* @param int $page the page you want to get the PAGE button for
* @returns string
*/
function getButPage($page) {
if($page < 1) return "";
if($page > $this->page_list_size) return "";
$source = ($page == $this->page) ? $this->but_page_this : $this->but_page;
$temp = ereg_replace("{query_string}", $this->query_string, $source);
$result = ereg_replace("{page}", (string)$page, $temp);
return $result;
}
/**
* Generates the Google style PAGE button list html code from the predefined templates
* (ie.: "1 - 2 - 3 - 4 - 5", check initial vars for more information)
*
* @param int $range number of page buttons to be shown before and after tbe current page
* @returns string
*/
function getButList($range = 9) {
$range++;
unset($result);
for($i = ($this->page - $range); $i <= ($this->page + $range); $i++) {
if($this->page < $range) {
$result .= $this->getButPage($i);
if ($i == $range) break;
}
else $result .= $this->getButPage($i);
if($this->getButPage($i+1) && $result && $i < $this->page + $range)
$result .= $this->separator;
}
return $result;
}
/**
* Generates the result range information text from the predefined template
* (ie.: "Displaying 1 - 3 of 24", check initial vars for more information)
*
* @returns string
*/
function getRangeInfo() {
$from = $this->getPageFrom($this->page) + 1;
$to = $this->getPageTo($this->page);
$temp = ereg_replace("{from}", (string)$from, $this->range_display);
$temp = ereg_replace("{to}", (string)$to, $temp);
$result = ereg_replace("{total}", (string)$this->num_results, $temp);
return $result;
}
}