日期:2010-08-28 浏览次数:20692 次
任务经常会用到的一个分页类..记录一下.
/**
Example:
$page = (isset($this->params[named][page])) ? $this->params[named][page] : 1;
// 导入分页类
App::import(Vendor, page_class);
// 获取公告总数
$total = $this->Bulletin->getBulletinTotal();
$perpage = ITEMPREPAGE;
$offset = $perpage * ($page - 1);
$pagebar = new Pagebar($total, $page, $offset, $perpage);
$pagenav = $pagebar->construct_page_nav($total, $page, SYSTEMURL . /bulletins/index, null, null);
*/
perpage = $perpage;
$numpages = ceil($numresults / $this->perpage);
if ($numpages == 0)
{
$numpages = 1;
}
if ($page < 1)
{
$page = 1;
}
else if ($page > $numpages)
{
$page = $numpages;
}
$limitlower = ($page - 1) * $this->perpage;
$limitupper = ($page) * $this->perpage;
if ($limitupper > $numresults)
{
$limitupper = $numresults;
if ($limitlower > $numresults)
{
$limitlower = ($numresults - $this->perpage) - 1;
}
}
if ($limitlower < 0)
{
$limitlower = 0;
}
}
/**
* Returns the HTML for multi-page navigation
*
* @param integer Total number of items found
* @param integer Page number being displayed
* @param string Base address for links eg: q.php?t=99{&page=4}
* @param string Ending portion of address for links
*
* @return string Page navigation HTML
*/
function construct_page_nav($results, $pagenumber, $address, $address2 = , $option = array())
{
$perpage = $this->perpage;
$show = array();
$curpage = 0;
$pagenav = ;
$firstlink = ;
$prevlink = ;
$lastlink = ;
$nextlink = ;
if ($results <= $perpage)
{
$show[pagenav] = false;
return ;
}
$show[pagenav] = true;
$total = number_format($results);
$totalpages = ceil($results / $perpage);
$show[prev] = false;
$show[next] = false;
$show[first] = false;
$show[last] = false;
if ($pagenumber > 1)
{
$prevpage = $pagenumber - 1;
$prevnumbers = $this->fetch_start_end_total_array($prevpage, $perpage, $results);
$show[prev] = true;
}
if ($pagenumber < $totalpages)
{
$nextpage = $pagenumber + 1;
$nextnumbers = $this->fetch_start_end_total_array($nextpage, $perpage, $results);
$show[next] = true;
}
// create array of possible relative links that we might have (eg. +10, +20, +50, etc.)
$pagenavsarr = array(10, 50, 100, 500, 1000);
$pagenavpages = 5;
while ($curpage++ < $totalpages)
{
if (abs($curpage - $pagenumber) >= $pagenavpages AND $pagenavpages != 0)
{
if ($curpage == 1)
{
$firstnumbers = $this->fetch_start_end_total_array(1, $perpage, $results);
$show[first] = true;
}
if ($curpage == $totalpages)
{
$lastnumbers = $this->fetch_start_end_total_array($totalpages, $perpage, $results);
$show[last] = true;
}
// generate relative links (eg. +10,etc).
if (in_array(abs($curpage - $pagenumber), $pagenavsarr) AND $curpage != 1 AND $curpage != $totalpages)
{
$pagenumbers = $this->fetch_start_end_total_array($curpage, $perpage, $results);
$relpage = $curpage - $pagenumber;
if ($relpage > 0)
{
$relpage = + . $relpage;
}
$pagenav .= " $curpage ";
}
}
else
{
if ($curpage == $pagenumber)
{
$numbers = $this->fetch_start_end_total_array($curpage, $perpage, $results);
$pagenav .= " $curpage ";
}
else
{
$pagenumbers = $this->fetch_start_end_total_array($curpage, $perpage, $re