Home >Backend Development >PHP Tutorial >ThinkPHP paging implementation, thinkphp paging implementation_PHP tutorial
TP3.2 framework manual, there is a data paging, but you have to write too much code every time, and there are some Chinese settings, etc. Sorry, as a program developer, I need to sort it out:
O. Let’s look at the renderings first
1. Paging method
<span>/*</span><span>* * TODO 基础分页的相同代码封装,使前台的代码更少 * @param $m 模型,引用传递 * @param $where 查询条件 * @param int $pagesize 每页查询条数 * @return \Think\Page </span><span>*/</span> <span>function</span> getpage(&<span>$m</span>,<span>$where</span>,<span>$pagesize</span>=10<span>){ </span><span>$m1</span>=<span>clone</span> <span>$m</span>;<span>//</span><span>浅复制一个模型</span> <span>$count</span> = <span>$m</span>->where(<span>$where</span>)-><span>count</span>();<span>//</span><span>连惯操作后会对join等操作进行重置</span> <span>$m</span>=<span>$m1</span>;<span>//</span><span>为保持在为定的连惯操作,浅复制一个模型</span> <span>$p</span>=<span>new</span> Think\Page(<span>$count</span>,<span>$pagesize</span><span>); </span><span>$p</span>->lastSuffix=<span>false</span><span>; </span><span>$p</span>->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录 每页<b>%LIST_ROW%</b>条 第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>'<span>); </span><span>$p</span>->setConfig('prev','上一页'<span>); </span><span>$p</span>->setConfig('next','下一页'<span>); </span><span>$p</span>->setConfig('last','末页'<span>); </span><span>$p</span>->setConfig('first','首页'<span>); </span><span>$p</span>->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%'<span>); </span><span>$p</span>->parameter=I('get.'<span>); </span><span>$m</span>->limit(<span>$p</span>->firstRow,<span>$p</span>-><span>listRows); </span><span>return</span> <span>$p</span><span>; }</span>
The getpage method can be placed in Application/Common/Common/function.php of the TP framework. This document can specifically place some common methods, which can be called anywhere (such as: Controller files, View files, etc.).
2. Call the paging method
<span>$m</span>=M('products'<span>); </span><span>$p</span>=getpage(<span>$m</span>,<span>$where</span>,10<span>); </span><span>$list</span>=<span>$m</span>->field(<span>true</span>)->where(<span>$where</span>)->order('id desc')-><span>select(); </span><span>$this</span>-><span>list</span>=<span>$list</span><span>; </span><span>$this</span>->page=<span>$p</span>->show();
View code again
<div class="pagination"><span> {$page} </div></span>
Third, the last step is the paging style. This is a bit messy. Because the background framework is downloaded from the Internet, the style has not yet been sorted out. This style can also be implemented by yourself, it is simple.
<span>.pagination ul </span>{<span> display</span>:<span> inline-block</span>;<span> margin-bottom</span>:<span> 0</span>;<span> margin-left</span>:<span> 0</span>;<span> -webkit-border-radius</span>:<span> 3px</span>;<span> -moz-border-radius</span>:<span> 3px</span>;<span> border-radius</span>:<span> 3px</span>;<span> -webkit-box-shadow</span>:<span> 0 1px 2px rgba(0,0,0,0.05)</span>;<span> -moz-box-shadow</span>:<span> 0 1px 2px rgba(0,0,0,0.05)</span>;<span> box-shadow</span>:<span> 0 1px 2px rgba(0,0,0,0.05)</span>; }<span> .pagination ul li </span>{<span> display</span>:<span> inline</span>; }<span> .pagination ul li.rows </span>{<span> line-height</span>:<span> 30px</span>;<span> padding-left</span>:<span> 5px</span>; }<span> .pagination ul li.rows b</span>{<span>color</span>:<span> #f00</span>}<span> .pagination ul li a, .pagination ul li span </span>{<span> float</span>:<span> left</span>;<span> padding</span>:<span> 4px 12px</span>;<span> line-height</span>:<span> 20px</span>;<span> text-decoration</span>:<span> none</span>;<span> background-color</span>:<span> #fff</span>;<span> background</span>:<span> url('../images/bottom_bg.png') 0px 0px</span>;<span> border</span>:<span> 1px solid #d3dbde</span>; <span>/*</span><span>border-left-width: 0;</span><span>*/</span><span> margin-left</span>:<span> 2px</span>;<span> color</span>:<span> #08c</span>; }<span> .pagination ul li a:hover</span>{<span> color</span>:<span> red</span>;<span> background</span>:<span> #0088cc</span>; }<span> .pagination ul li.first-child a, .pagination ul li.first-child span </span>{<span> border-left-width</span>:<span> 1px</span>;<span> -webkit-border-bottom-left-radius</span>:<span> 3px</span>;<span> border-bottom-left-radius</span>:<span> 3px</span>;<span> -webkit-border-top-left-radius</span>:<span> 3px</span>;<span> border-top-left-radius</span>:<span> 3px</span>;<span> -moz-border-radius-bottomleft</span>:<span> 3px</span>;<span> -moz-border-radius-topleft</span>:<span> 3px</span>; }<span> .pagination ul .disabled span, .pagination ul .disabled a, .pagination ul .disabled a:hover </span>{<span> color</span>:<span> #999</span>;<span> cursor</span>:<span> default</span>;<span> background-color</span>:<span> transparent</span>; }<span> .pagination ul .active a, .pagination ul .active span </span>{<span> color</span>:<span> #999</span>;<span> cursor</span>:<span> default</span>; }<span> .pagination ul li a:hover, .pagination ul .active a, .pagination ul .active span </span>{<span> background-color</span>:<span> #f0c040</span>; }<span> .pagination ul li.last-child a, .pagination ul li.last-child span </span>{<span> -webkit-border-top-right-radius</span>:<span> 3px</span>;<span> border-top-right-radius</span>:<span> 3px</span>;<span> -webkit-border-bottom-right-radius</span>:<span> 3px</span>;<span> border-bottom-right-radius</span>:<span> 3px</span>;<span> -moz-border-radius-topright</span>:<span> 3px</span>;<span> -moz-border-radius-bottomright</span>:<span> 3px</span>; }<span> .pagination ul li.current a</span>{<span>color</span>:<span> #f00 </span>;<span>font-weight</span>:<span> bold</span>;<span> background</span>:<span> #ddd</span>}
TP’s paging is actually very simple:
First you need to import the paging class.
The code is as follows:
$User = M('User'); // Instantiate the User object
import('ORG.Util.Page');//Import the paging class
$count = $User->where('status=1')->count();// Query the total number of records that meet the requirements
$Page = new Page($count,25);// Example Pass in the total number of records and the number of records displayed on each page to the paging class
$show = $Page->show(); // Paging display output
// When performing paging data query, pay attention to the parameters of the limit method. Use the attributes of the Page class
$list = $User->where('status=1')->order('create_time')->limit($Page->firstRow.','.$ Page->listRows)->select();
$this->assign('list',$list);//Assign data set
$this->assign('page', $show);// Assignment paging output
$this->display(); // Output template
Article pagination, I remember there should be thinkphpcms. Find that one, install it and see how it is written. . You can do it - -