首先要搞清楚的就是ThinkPHP3.2.3的分页类已经被移到了Think\Page.class.php,这是跟以前的版本有些不一样的,使用起来还是跟以前版本差不多,但是默认的效果不敢恭维,所以最好是自己加些样式。
Application/Home/View/Work/works.html
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
<style type="text/css">
/*分页css*/
.tp_pages{width: 100%;height: 53px;clear: both;position: relative;text-align: center;margin: 50px auto 0 auto;}
.tp_pages .upPage{display: inline-block;width: 64px;height: 53px;line-height: 53px;margin: 0 5px;border-radius: 5px;background: #999999 url(../img/upPage.png) no-repeat center;transition: ease .5s;}
.tp_pages .downPage{display: inline-block;width: 144px;height: 53px;line-height: 53px;margin: 0 5px;border-radius: 5px;background: #999999 url(../img/downPage.png) no-repeat center;transition: ease .2s;}
.tp_pages .upPage:hover,.tp_pages .downPage:hover{background-color: rgb(255,233,0);}
/*/ 分页css*/
</style>
</head>
<body>
<!-- 作品 -->
<div class="iso-box-wrapper">
<volist name="works" id="vo">
{$vo.id}
</volist>
</div>
<!-- / 作品 -->
<!-- 分页 -->
<div class="tp_pages">
{$page}
</div>
<!-- / 分页 -->
</body>
</html>
Application/Home/Controller/WorkController.php
<?php
namespace Home\Controller;
use Think\Controller;
class WorkController extends Controller {
public function works() {
$Work = M('Work');
$count = $Work->count(); // 总数
$p = getpage($count,16); // 18,16
$works = $Work->field(true)->order('id')->limit($p->firstRow, $p->listRows)->select();
$this->assign('works', $works); // 赋值数据集
$this->assign('page', $p->show()); // 赋值分页输出
$this->display();
}
}
?>
Application/Common/Common/function.php
<?php
/**
* TODO 基础分页的相同代码封装,使前台的代码更少
* @param $count 要分页的总记录数
* @param int $pagesize 每页查询条数
* @return \Think\Page
*/
function getpage($count, $pagesize = 10) {
$p = new Think\Page($count, $pagesize);
$p->setConfig('header', '<li class="rows">共<b>%TOTAL_ROW%</b>条记录 第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
$p->setConfig('prev', '上一页');
$p->setConfig('next', '下一页');
$p->setConfig('theme', '%upPage% %downPage%');
$p->lastSuffix = false;//最后一页不显示为总页数
return $p;
}
?>
效果:可以正确跳转页数以及显示对应的内容