分页
_currentpage = max((int)@$http_get_vars['pageid'], 1);
$this->_perpage = 8;
$this->_itemdata = array();
foreach ($params as $name => $value) {
$this->{'_' . $name} = $value;
}
$this->_totalitems = count($this->_itemdata);
}
/**
* returns an array of current pages data
*
* @param $pageid desired page id (optional)
* @return array page data
*/
function getpagedata($pageid = null)
{
if (isset($pageid)) {
if (!empty($this->_pagedata[$pageid])) {
return $this->_pagedata[$pageid];
} else {
return false;
}
}
if (!isset($this->_pagedata)) {
$this->_generatepagedata();
}
return $this->getpagedata($this->_currentpage);
}
/**
* returns pageid for given offset
*
* @param $index offset to get pageid for
* @return int pageid for given offset
*/
function getpageidbyoffset($index)
{
if (!isset($this->_pagedata)) {
$this->_generatepagedata();
}
if (($index % $this->_perpage) > 0) {
$pageid = ceil((float)$index / (float)$this->_perpage);
} else {
$pageid = $index / $this->_perpage;
}
return $pageid;
}
/**
* returns offsets for given pageid. eg, if you
* pass it pageid one and your perpage limit is 10
* it will return you 1 and 10. pageid of 2 would
* give you 11 and 20.
*
* @params pageid pageid to get offsets for
* @return array first and last offsets
*/
function getoffsetbypageid($pageid = null)
{
$pageid = isset($pageid) ? $pageid : $this->_currentpage;
if (!isset($this->_pagedata)) {
$this->_generatepagedata();
}
if (isset($this->_pagedata[$pageid])) {
return array(($this->_perpage * ($pageid - 1)) + 1, min($this->_totalitems, $this->_perpage * $pageid));
} else {
return array(0,0);
}
}
/**
* returns back/next and page links
*
* @param $back_html html to put inside the back link
* @param $next_html html to put inside the next link
* @return array back/pages/next links
*/
function getlinks($back_html = '>')
{
$url = $this->_getlinksurl();
$back = $this->_getbacklink($url, $back_html);
$pages = $this->_getpagelinks($url);
$next = $this->_getnextlink($url, $next_html);
return array($back, $pages, $next, 'back' => $back, 'pages' => $pages, 'next' => $next);
}
/**
* returns number of pages
*
* @return int number of pages
*/
function numpages()
{
return $this->_totalpages;
}
/**
* returns whether current page is first page
*
* @return bool first page or not
*/
function isfirstpage()
{
return ($this->_currentpage == 1);
}
/**
* returns whether current page is last page
*
* @return bool last page or not
*/
function islastpage()
{
return ($this->_currentpage == $this->_totalpages);
}
/**
* returns whether last page is complete
*
* @return bool last age complete or not
*/
function islastpagecomplete()
{
return !($this->_totalitems % $this->_perpage);
}
/**
* calculates all page data
*/
function _generatepagedata()
{
$this->_totalitems = count($this->_itemdata);
$this->_totalpages = ceil((float)$this->_totalitems / (float)$this->_perpage);
$i = 1;
if (!empty($this->_itemdata)) {
foreach ($this->_itemdata as $value) {
$this->_pagedata[$i][] = $value;
if (count($this->_pagedata[$i]) >= $this->_perpage) {
$i++;
}
}
} else {
$this->_pagedata = array();
}
}
/**
* returns the correct link for the back/pages/next links
*
* @return string url
*/
function _getlinksurl()
{
global $http_server_vars;
// sort out query string to prevent messy urls
$querystring = array();
if (!empty($http_server_vars['query_string'])) {
$qs = explode('&', $http_server_vars['query_string']);
for ($i = 0, $cnt = count($qs); $i list($name, $value) = explode('=', $qs[$i]);
if ($name != 'pageid') {
$qs[$name] = $value;
}
unset($qs[$i]);
}
}
if(is_array($qs)){
foreach ($qs as $name => $value) {
$querystring[] = $name . '=' . $value;
}
}
return $http_server_vars['script_name'] . '?' . implode('&', $querystring) . (!empty($querystring) ? '&' : '') . 'pageid=';
}
/**
* returns back link
*
* @param $url url to use in the link
* @param $link html to use as the link
* @return string the link
*/
function _getbacklink($url, $link = ' {
// back link
if ($this->_currentpage > 1) {
$back = '_currentpage%20-%201)%20.%20'>' . $link . '';
} else {
$back = '';
}
return $back;
}
/**
* returns pages link
*
* @param $url url to use in the link
* @return string links
*/
function _getpagelinks($url)
{
// create the range
$params['itemdata'] = range(1, max(1, $this->_totalpages));
$pager =& new pager($params);
$links = $pager->getpagedata($pager->getpageidbyoffset($this->_currentpage));
for ($i=0; $i if ($links[$i] != $this->_currentpage) {
$links[$i] = '_getlinksurl()%20.%20%24links%5b%24i%5d%20.%20'>' . $links[$i] . '';
}
}
return implode(' ', $links);
}
/**
* returns next link
*
* @param $url url to use in the link
* @param $link html to use as the link
* @return string the link
*/
function _getnextlink($url, $link = 'next >>')
{
if ($this->_currentpage _totalpages) {
$next = '_currentpage%20+%201)%20.%20'>' . $link . '';
} else {
$next = '';
}
return $next;
}
}
?>