php技术博客
让天下没有搞不定的bug~

总结:ThinkPHP 分页详解及扩展

       数据分页是 Web 开发中一个常见的功能,ThinkPHP 内置了分页类(ThinkPHP 系统目录下 Lib/ORG/Util/Page.class.php),可以直接使用,不过tp的分页类还有个缺陷,就是没有固定页跳转,不过如果真正明白tp的分页类得话,写起来也不难,小涛来简单的写一下:

分页类语法:

Page(totalRows, listRows, parameter)
参数说明:
参数 说明
totalRows 必选,总的记录数
listRows 可选,每页显示记录数,默认 20
parameter

实例演示及拓展:

import(“ORG.Util.Page”); // 导入分页类

$pageUrl = ”; //用于搜索分页传递参数,保持条件进行分页

$cases = array();

$p = isset($_GET[‘p’])?intval($_GET[‘p’]):1;   //初始化分页

 $serchValue          = ”; //搜索的标题

//这里以搜索标题为例
  if( isset($_REQUEST[‘serchValue’]) && $_REQUEST[‘serchValue’]!=” )
  {
            $serchValue  = $_REQUEST[‘serchValue’];
            $cases[‘Articles.title’]  = array(‘like’,”%{$serchValue}%”);
            $pageUrl .= ‘/serchValue/’.$serchValue;
   }

  $Articles       = D(‘ArticleView’);
  $count         = $Articles->where($cases)->count();

 $Page         = new Page($count,5);
 $show         = $Page->showPage($pageUrl);
 $list         = $Articles->where($cases)->order($order)->page($p.’,5′)->select();

最后贴一下,拓展的的showPage方法:

/**

+———————————————————-

* 扩展后的分页显示输出

+———————————————————-

* auth: zone by 2011-03-15

* @access public

+———————————————————-

*/

public function showPage($and=’/’,$separated=’/’) {

if(0 == $this->totalRows) return ‘0 条记录’;

$p = C(‘VAR_PAGE’);

$nowCoolPage = ceil($this->nowPage/$this->rollPage);

$url = __ACTION__;

$parse = parse_url($url);

if(isset($parse[‘query’])) {

parse_str($parse[‘query’],$params);

unset($params[$p]);

$url = $parse[‘path’].’?’.http_build_query($params);

}

if( $and!=’/’ ) $and .= ‘/’;

//上下翻页字符串

$upRow = $this->nowPage-1;

$downRow = $this->nowPage+1;

if ($upRow>0){

$upPage=”<a href='”.$url.$and.$p.$separated.$upRow.”‘ target=’_self’>”.$this->config[‘prev’].”</a>”;

}else{

$upPage=””;

}

if ($downRow <= $this->totalPages){

$downPage=”<a href='”.$url.$and.$p.$separated.$downRow.”‘ target=’_self’>”.$this->config[‘next’].”</a>”;

}else{

$downPage=””;

}

// << < > >>

if($nowCoolPage == 1){

$theFirst = “”;

$prePage = “”;

}else{

$preRow = $this->nowPage-$this->rollPage;

$prePage = “<a href='”.$url.$and.$p.$separated.$upRow.”‘ target=’_self’>上”.$this->rollPage.”页</a>”;

$theFirst = “<a href='”.$url.$and.$p.$separated.”1′ target=’_self’>”.$this->config[‘first’].”</a>”;

}

if($nowCoolPage == $this->coolPages){

$nextPage = “”;

$theEnd=””;

}else{

$nextRow = $this->nowPage+$this->rollPage;

$theEndRow = $this->totalPages;

$nextPage = “<a href='”.$url.$and.$p.$separated.$nextRow.”‘ target=’_self’>下”.$this->rollPage.”页</a>”;

$theEnd = “<a href='”.$url.$and.$p.$separated.$theEndRow.”‘ target=’_self’>”.$this->config[‘last’].”</a>”;

}

// 1 2 3 4 5

$linkPage = “”;

for($i=$this->nowPage-5;$i<=$this->nowPage+5;$i++)

{

if($i<1) continue;

if($i!=$this->nowPage){

if($i<=$this->totalPages){

$linkPage .= “&nbsp;<a href='”.$url.$and.$p.$separated.$i.”‘ target=’_self’>&nbsp;”.$i.”&nbsp;</a>”;

}else{

break;

}

}else{

if($this->totalPages != 1){

$linkPage .= “&nbsp;<span class=’current’>”.$this->nowPage.”</span>”;

}

}

}

$pageStr = str_replace(

array(‘%header%’,’%nowPage%’,’%totalRow%’,’%totalPage%’,’%upPage%’,’%downPage%’,’%first%’,’%prePage%’,’%linkPage%’,’%nextPage%’,’%end%’),

array($this->config[‘header’],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd),$this->config[‘theme’]);

//$pageStr .= ‘&nbsp;<input type=”input” id=”ppp” style=”width:30px” onkeyup=”if(event.keyCode==\’13\’){window.location=\”.$url.’/p/\’+document.getElementById(\’ppp\’).value;}”>’;

return $pageStr;

}

 

 

      这里是拓展的原来分页类的show方法,原先是没有参数的,这里添加参数的主要目的是解决保持搜索条件进行翻页,用法就是把这个方法拷贝到原来的分页类里,我的是\ThinkPHP\Lib\ORG\Util\Page.class.php这样就可以用了,只需要传递参数的时候传递p或者其他的条件就可以了。这里就可以进行固定页数进行跳转了,可以让用户输入页数进行跳转,也可以让用户进行选择页数进行跳转,这里的接收分页的已经写好了,剩下的工作就可以自己来完成了,说一下大体思路:当选取下拉框触发方法传递分页参数

   好了,就说这些吧,这些已经很具体了,diy一下吧!

   技术分享,技术交流,小涛与您共同成长……

赞(0)
未经允许不得转载:PHP技术博客 » 总结:ThinkPHP 分页详解及扩展