TP笔记--分页
显示效果
html部分
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>thinkphp学习之路之分页跳转的实现</title>
<style>
.pagination li{float:left;list-style:none;padding:5px 10px;}
</style>
<link rel="stylesheet" type="text/css" href="http://cdn.bootcss.com/bootstrap/4.0.0-alpha.5/css/bootstrap-flex.css"/>
</head>
<body>
<!--验证码的输出-->
<!--<div><img src="{:captcha_src()}" alt="captcha" /></div>-->
<!--通过拓展的插件实现验证码的展现-->
<!--会员信息的输出-->
<div class="table-responsive">
<table class="table table-hover"> <!--border="1" cellspacing="0" cellpadding="0"-->
<theader>
<tr>
<th>ID</th>
<th>用户名</th>
<th>用户类型</th>
<th>用户状态</th>
<th>注册时间</th>
</tr>
</theader>
{foreach $result as $vo}
<tr>
<td>{$vo.id}</td>
<td>{$vo.user_login}</td>
<td>
{switch name="$vo.user_type"}
{case value="1"}管理员{/case}
{case value="2"}普通会员{/case}
{/switch}
</td>
<td>
{switch name="$vo.user_status"}
{case value="0"}禁止登录{/case}
{case value="1"}正 常{/case}
{case value="2"}未验证{/case}
{/switch}
</td>
<td>{$vo.create_time}</td>
</tr>
{/foreach}
</table>
</div>
<div>
<form class="form-inline" role="form" method="post"><!--post和get需要和后台控制器保持一致否则无效-->
<div class="form-group">
{$page}
</div>
<div class="form-group">
<input class="form-control" type="number" min="1" max="{$count}" name="pagelist" placeholder="请输入页码"/>
<input class="btn btn-danger" type="submit" value="确定" />
</div>
</form>
</div>
</body>
</html>
PHP控制器部分
<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
class Index extends Controller
{
public function index()
{
$num=input('post.pagelist');//post和get需要和前台提交的时候保持一致否则无效
//dump($num);//传来的数值
$currnum=5;//设置每页显示的数据条数
$count= Db::name('users')->count();//获取数据的总数量
//dump($count);//数据的总数量
$data = Db::name('users')->paginate($currnum,$count/*false*/,[//数字10为每页显示的总条数,true为去掉中间的页码部分,false为显示分页的页码
'type' => 'bootstrap',//分页类名
'var_page' => 'page',//分页变量
'page' => $num,//传入跳转值给当前页
]);
$pages=$count/$currnum;//计算出总页数
$page=$data->render();//获取分页显示
$this->assign('result',$data);//模板变量赋值与result,使其在前台可以使用$result变量
$this->assign('count',$pages);
$this->assign('page',$page);
//dump($page);
return $this->fetch();
}
}