1.系统自带的分页,适用于数据库查询出来的数据 ,其中paginate可提供简洁查询paginate(10,true);
// 查询状态为1的用户数据 并且每页显示10条数据
$list = Db::name('user')->where('status',1)->paginate(10);
// 把分页数据赋值给模板变量list
$this->assign('list', $list);
// 渲染模板输出
return $this->fetch();
<div>
<ul>
{volist name='list' id='user'}
<li> {$user.nickname}</li>
{/volist}
</ul>
</div>
{$list|raw}
2.自定义一个分页类 Page.php 放在框架 extend \lib 下,具体代码参考的 http://www.cnblogs.com/ZDPPU/p/6864276.html,其中的样式可以根据自己的需求来改写。
<?php
namespace lib;
class Page {
public $page; //当前页
public $total; //总记录数
public $listRows; //每页显示记录数
private $uri;//动态url
public $pageNum; //总页数
private $listNum=6;//显示页码按钮数量
public $render;//分页后的html模板
public $data;//分页后渲染到模板的数据
/*
* 初始化分页数据
*$sdata 待分页的数据
* $listRows 每页记录数
*/
public function __construct($sdata, $listRows=15){
$this->total=count($sdata);
$this->listRows=$listRows;
$this->uri=$this->getUri();
$this->page=!empty($_GET["page"]) ? $_GET["page"] : 1;
$this->pageNum=ceil($this->total/$this->listRows);
$this->render=$this->pageHtml();
$this->data=array_slice($sdata,($this->page-1)*$this->listRows,$listRows);
return $this->data;
}
//动态获取url
private function getUri(){
$url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"], '?')?'':"?");
$parse=parse_url($url);
if(isset($parse["query"])){
parse_str($parse['query'],$params);
unset($params["page"]);
$url=$parse['path'].'?'.http_build_query($params);
}
return $url;
}
//首页
private function first(){
$html = "";
if($this->page==1)
$html.=" <a style='magin=10px;' class='current btn disabled'>首 页</a>";
else
$html.=" <a class='btn btn-primary-outline' href='{$this->uri}&page=1'>首 页</a>";
return $html;
}
//上一页
private function prev(){
$html = "";
if($this->page==1)
$html.=" <a class='current btn disabled'>上一页</a>";
else
$html.=" <a class='btn btn-primary-outline' href='{$this->uri}&page=".($this->page-1)."'>上一页</a>";
return $html;
}
//页码按钮
private function pageList(){
$linkPage="";
$inum=floor($this->listNum/2);
for($i=$this->page-$inum;$i<=$this->page+$inum;$i++){
if($i<=0){
continue;
}
if($i>$this->pageNum){
continue;
}
if($i == $this->page){
$linkPage.=" <a class='current btn btn-secondary'>{$i}</a>";
}else{
$linkPage.=" <a class='btn btn-primary-outline' href='{$this->uri}&page={$i}'>{$i}</a>";
}
}
return $linkPage;
}
//下一页
private function next(){
$html = "";
if($this->page==$this->pageNum)
$html.=" <a class='current btn disabled'>下一页</a>";
else
$html.=" <a class='btn btn-primary-outline' href='{$this->uri}&page=".($this->page+1)."'>下一页</a>";
return $html;
}
//尾页
private function last(){
$html = "";
if($this->page==$this->pageNum)
$html.=" <a class='current btn disabled'>尾 页</a>";
else
$html.=" <a class='btn btn-primary-outline' href='{$this->uri}&page=".($this->pageNum)."'>尾 页</a>";
return $html;
}
//输入指定页码
private function goPage(){
return ' <input class="input-text" type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;location=\''.$this->uri.'&page=\'+page+\'\'}" value="'.$this->page.'" style="width:52px"><input class="btn btn-secondary" type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'&page=\'+page+\'\'"> ';
}
//选择指定页码
function selectPage(){
$inum=10;
$location = $this->uri.'&page=';
$selectPage ="<span class='va-m'>到第 </span> <span class='select-box' style='width:initial'><select class='select' name='topage' size='1' onchange='window.location=\"$location\"+this.value'>";
for($i=$this->page-$inum;$i<=$this->page+$inum;$i++){
if($i<=0){
continue;
}
if($i>$this->pageNum){
continue;
}
if($i == $this->page){
$selectPage .="<option value='$i' selected>$i</option>";
}else{
$selectPage .="<option value='$i'>$i</option>";
}
}
$selectPage .="</select></span> <span class='va-m'>页</span>";
return $selectPage;
}
//组装分页的html模板
function pageHtml(){
$html = "<div class='cl mt-20 text-c'>";
// $html .= "<span class='pr-20 va-m'>共有<b>{$this->total}</b>条记录</span>";
// $html .= "<span class='pr-20 va-m'>每页显示<b>{$this->listRows}</b>条</span>";
// $html .= "<span class='pr-20 va-m'><b>当前{$this->page}/{$this->pageNum}</b>页</span>";
$html .= $this->first();
$html .= $this->prev();
$html .= $this->pageList();
$html .= $this->next();
$html .= $this->last();
$html .= $this->goPage();
$html .= $this->selectPage();
$html .= '</div>';
return $html;
}
}
3. controller中引用
$table = 'collection_air';
$list = Db::connect('db_config1')->table($table)->whereNull('state')->order('id desc')->limit(200)->select();
// dump($list[0]);
// $page = $list->render();
$type = 22;
$map['type'] = $type;
$codeType =Db::connect('db_config1')->table('code_type')->where($map)->field('')->find();
$list = collection_data($list,$codeType);
$column = explode('|',$codeType['tab']);
unset($column[count($column)-1]);
$p = new \lib\Page($list,10);
//
//$p->data; //当前页数据
//$p->render; 分页html模板
//$p->page; //当前第几页
//$p->total; //总记录数
//$p->listRows; //每页显示记录数
//$p->pageNum; //总页数
//根据需要组装数据输出显示,例如:
$this->assign('list',$list);
$this->assign('column',$column);
$this->assign('codeType',$codeType);
$this->assign('page', $p->render);
$this->assign([
'p' => $p,
]);
return $this->fetch();
//页面代码
<table class="display table table-bordered table-striped" id="dynamic-table">
<thead>
<tr>
<th> <span class="line"></span>mn</th>
{volist name="column" id="vo" key="k" }
<th> <span class="line"></span>{$vo}</th>
{/volist}
<th> <span class="line"></span>时间</th>
</tr>
</thead>
<tbody>
{notempty name='$p->data'}
{volist name="$p->data" id="vo" key="k" }
<tr>
<!-- <td style="border-top:0px; border-bottom:1px #edf2f7 solid;color:#bf242a;">{$vo.id}</td> -->
<td>{$vo.mn}</td>
{volist name="vo['dataList']" id="v" }
<td>{$v.data} {$v.unit}</td>
{/volist}
<td>{$vo.create_time}</td>
</tr>
{/volist}
{/notempty}
</tfoot>
</table>
//注释 tp5.1加了去除预定义字符 htmlentities
<?php echo $p->render; ?>
4. htmlentities 为了避免出现XSS安全问题,默认的变量输出都会使用htmlentities方法进行转义输出。so 原本的$p->render就不生效了。很是苦恼。html是晚于php解析的,<?php echo $p->render; ?> 测了一下就好了。
Hello,{$name}!编译后 Hello,<?php echo htmlentities($name);?>!
5.没了,多读书多看报,少吃零食多睡觉。