thinkphp分页实现效果
对于thinkphp分页的实现效果,一共分为两种一种是一种调用公共函数中的函数方法,而另一种是模型中书写分页的方法,下面就给需要的朋友来整理一下。
一、分页方法
/** *TODO基础分页的相同代码封装,使前台的代码更少 *@param$m模型,引用传递 *@param$where查询条件 *@paramint$pagesize每页查询条数 *@return\Think\Page */ functiongetpage(&$m,$where,$pagesize=10){ $m1=clone$m;//浅复制一个模型 $count=$m->where($where)->count();//连惯操作后会对join等操作进行重置 $m=$m1;//为保持在为定的连惯操作,浅复制一个模型 $p=newThink\Page($count,$pagesize); $p->lastSuffix=false; $p->setConfig('header','<liclass="rows">共<b>%TOTAL_ROW%</b>条记录 每页<b>%LIST_ROW%</b>条 第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>'); $p->setConfig('prev','上一页'); $p->setConfig('next','下一页'); $p->setConfig('last','末页'); $p->setConfig('first','首页'); $p->setConfig('theme','%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%'); $p->parameter=I('get.'); $m->limit($p->firstRow,$p->listRows); return$p; }
getpage方法可以放在TP框架的Application/Common/Common/function.php,这个文档可以专门放置一些通用的方法,在哪里都可以调用(如:Controller文件,View文件等)。
二、调用分页方法
$m=M('products'); $p=getpage($m,$where,10); $list=$m->field(true)->where($where)->order('iddesc')->select(); $this->list=$list; $this->page=$p->show();
再是View代码
<divclass="pagination"> {$page} </div>
三、最后就是分页的样式了,这个有些乱,因后台框架网上下载的,样式还没来的及整理,这个样式也可以自己实现,简单的。
.paginationul{ display:inline-block; margin-bottom:0; margin-left:0; -webkit-border-radius:3px; -moz-border-radius:3px; border-radius:3px; -webkit-box-shadow:01px2pxrgba(0,0,0,0.05); -moz-box-shadow:01px2pxrgba(0,0,0,0.05); box-shadow:01px2pxrgba(0,0,0,0.05); } .paginationulli{ display:inline; } .paginationulli.rows{ line-height:30px; padding-left:5px; } .paginationulli.rowsb{color:#f00} .paginationullia,.paginationullispan{ float:left; padding:4px12px; line-height:20px; text-decoration:none; background-color:#fff; background:url('../images/bottom_bg.png')0px0px; border:1pxsolid#d3dbde; /*border-left-width:0;*/ margin-left:2px; color:#08c; } .paginationullia:hover{ color:red; background:#0088cc; } .paginationulli.first-childa,.paginationulli.first-childspan{ border-left-width:1px; -webkit-border-bottom-left-radius:3px; border-bottom-left-radius:3px; -webkit-border-top-left-radius:3px; border-top-left-radius:3px; -moz-border-radius-bottomleft:3px; -moz-border-radius-topleft:3px; } .paginationul.disabledspan,.paginationul.disableda,.paginationul.disableda:hover{ color:#999; cursor:default; background-color:transparent; } .paginationul.activea,.paginationul.activespan{ color:#999; cursor:default; } .paginationullia:hover,.paginationul.activea,.paginationul.activespan{ background-color:#f0c040; } .paginationulli.last-childa,.paginationulli.last-childspan{ -webkit-border-top-right-radius:3px; border-top-right-radius:3px; -webkit-border-bottom-right-radius:3px; border-bottom-right-radius:3px; -moz-border-radius-topright:3px; -moz-border-radius-bottomright:3px; } .paginationulli.currenta{color:#f00;font-weight:bold;background:#ddd}