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();
}
}