工作经常会用到的一个分页类..记录一下.
/**
Example:
$page=(isset($this->params[named]))?$this->params[named]:1;
//导入分页类
App::import(Vendor,page_class);
//获取公告总数
$total=$this->Bulletin->getBulletinTotal();
$perpage=ITEMPREPAGE;
$offset=$perpage*($page-1);
$pagebar=newPagebar($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;
}
elseif($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;
}
}
/**
*ReturnstheHTMLformulti-pagenavigation
*
*@param integer Totalnumberofitemsfound
*@param integer Pagenumberbeingdisplayed
*@param string Baseaddressforlinkseg:q.php?t=99{&page=4}
*@param string Endingportionofaddressforlinks
*
*@return string PagenavigationHTML
*/
functionconstruct_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;
}
//createarrayofpossiblerelativelinksthatwemighthave(eg.+10,+20,+50,etc.)
$pagenavsarr=array(10,50,100,500,1000);
$pagenavpages=5;
while($curpage++<$totalpages)
{
if(abs($curpage-$pagenumber)>=$pagenavpagesAND$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;
}
//generaterelativelinks(eg.+10,etc).
if(in_array(abs($curpage-$pagenumber),$pagenavsarr)AND$curpage!=1AND$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,$results);
$pagenav.="$curpage";
}
}
}
$pagenav="
".(($show[prev])?("上一页"):("上一页"))."
$pagenav
".(($show[next])?("下一页"):("下一页"))."
第$pagenumber/$totalpages页,共$total条记录";
return$pagenav;
}
/**
*ReturnsanarraysoyoucanprintShowingresults$arr[first]to$arr[last]of$totalresults
*
*@param integer Currentpagenumber
*@param integer Resultstoshowper-page
*@param integer Totalresultsfound
*
*@return array Intheformatof-array(first=>x,last=>y)
*/
functionfetch_start_end_total_array($pagenumber,$perpage,$total)
{
$first=$perpage*($pagenumber-1);
$last=$first+$perpage;
if($last>$total)
{
$last=$total;
}
$first++;
returnarray(first=>number_format($first),last=>number_format($last));
}
}
?>