Home >php教程 >PHP开发 >thinkphp paging implementation effect

thinkphp paging implementation effect

高洛峰
高洛峰Original
2016-12-21 16:39:341327browse

Regarding the implementation effect of thinkphp paging, there are two types. One is to call the function method in the public function, and the other is to write the paging method in the model. Let’s sort it out for those who need it.

1. Paging method

/**
 * TODO 基础分页的相同代码封装,使前台的代码更少
 * @param $m 模型,引用传递
 * @param $where 查询条件
 * @param int $pagesize 每页查询条数
 * @return \Think\Page
 */
function getpage(&$m,$where,$pagesize=10){
  $m1=clone $m;//浅复制一个模型
  $count = $m->where($where)->count();//连惯操作后会对join等操作进行重置
  $m=$m1;//为保持在为定的连惯操作,浅复制一个模型
  $p=new Think\Page($count,$pagesize);
  $p->lastSuffix=false;
  $p->setConfig(&#39;header&#39;,&#39;<li class="rows">共<b>%TOTAL_ROW%</b>条记录  每页<b>%LIST_ROW%</b>条  第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>&#39;);
  $p->setConfig(&#39;prev&#39;,&#39;上一页&#39;);
  $p->setConfig(&#39;next&#39;,&#39;下一页&#39;);
  $p->setConfig(&#39;last&#39;,&#39;末页&#39;);
  $p->setConfig(&#39;first&#39;,&#39;首页&#39;);
  $p->setConfig(&#39;theme&#39;,&#39;%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%&#39;);
 
  $p->parameter=I(&#39;get.&#39;);
 
  $m->limit($p->firstRow,$p->listRows);
 
  return $p;
}

The getpage method can be placed in Application/Common/Common/function.php of the TP framework. This document can specifically place some common methods and can be called anywhere (such as: Controller file, View file wait).

Second, call the paging method

$m=M(&#39;products&#39;);
$p=getpage($m,$where,10);
$list=$m->field(true)->where($where)->order(&#39;id desc&#39;)->select();
$this->list=$list;
$this->page=$p->show();

Then the View code

<div class="pagination">
  {$page}
</div>

Three, the last is the paging style, this is a bit messy, because the background framework is downloaded from the Internet, the style has not been sorted out yet, this You can also implement the style yourself, it’s simple.

.pagination ul {
  display: inline-block;
  margin-bottom: 0;
  margin-left: 0;
  -webkit-border-radius: 3px;
  -moz-border-radius: 3px;
  border-radius: 3px;
  -webkit-box-shadow: 0 1px 2px rgba(0,0,0,0.05);
  -moz-box-shadow: 0 1px 2px rgba(0,0,0,0.05);
  box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}
.pagination ul li {
 display: inline;
}
 
.pagination ul li.rows {
  line-height: 30px;
  padding-left: 5px;
}
.pagination ul li.rows b{color: #f00}
 
.pagination ul li a, .pagination ul li span {
  float: left;
  padding: 4px 12px;
  line-height: 20px;
  text-decoration: none;
  background-color: #fff;
  background: url(&#39;../images/bottom_bg.png&#39;) 0px 0px;
  border: 1px solid #d3dbde;
  /*border-left-width: 0;*/
  margin-left: 2px;
  color: #08c;
}
.pagination ul li a:hover{
  color: red;
  background: #0088cc;
}
.pagination ul li.first-child a, .pagination ul li.first-child span {
  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;
}
.pagination ul .disabled span, .pagination ul .disabled a, .pagination ul .disabled a:hover {
color: #999;
cursor: default;
background-color: transparent;
}
.pagination ul .active a, .pagination ul .active span {
color: #999;
cursor: default;
}
.pagination ul li a:hover, .pagination ul .active a, .pagination ul .active span {
background-color: #f0c040;
}
.pagination ul li.last-child a, .pagination ul li.last-child span {
  -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;
}
 
.pagination ul li.current a{color: #f00 ;font-weight: bold; background: #ddd}

For more articles related to thinkphp paging implementation effects, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn