PHP代码实例:常用的PHP分页类_PHP技巧_黑客防线网安服务器维护基地--Powered by WWW.RONGSEN.COM.CN

PHP代码实例:常用的PHP分页类

作者:黑客防线网安PHP教程基地 来源:黑客防线网安PHP教程基地 浏览次数:0

本篇关键词:常用代码实例perpage
黑客防线网安网讯:  工作经常会用到的一个分页类..记录一下.    /**    Example:    $page=(isset($this->params[named]))?$this->params[named]:1;    //导入分页类    App::import(V...
  工作经常会用到的一个分页类..记录一下.
  
  /**
  
  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));
  
  }
  
  }
  
  ?>
    黑客防线网安服务器维护方案本篇连接:http://www.rongsen.com.cn/show-17679-1.html
网站维护教程更新时间:2012-09-21 05:23:25  【打印此页】  【关闭
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer