Home  >  Article  >  Backend Development  >  Transformation of ThinkPHP paging function_PHP tutorial

Transformation of ThinkPHP paging function_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:44:47812browse

Renovation of ThinkPHP paging function

First, after creating the ThinkPHP project, open ThinkPHP/Common/functions.php in the root directory (the public functions of tp are stored here)
Add the following code
 1 function mypage($tot,$length){
 2     $page=$_GET['p']?$_GET['p']:1;
 3     $offset=($page-1)*$length;
 4     $prevpage=$page-1;
 5 
 6     $pages=ceil($tot/$length);
 7 
 8     if($page>=$pages){
 9         $nextpage=$pages;
10     }else{
11         $nextpage=$page+1;
12     }
13 
14     $limit="{$offset},{$length}";
15 
16     $show="
17     <h4>
18         <a href=&#39;__SELF__/p/1&#39;class=&#39;btn btn-warning btn-sm&#39;>首页</a>
19         <a href=&#39;__SELF__/p/{$prevpage}&#39; class=&#39;btn btn-warning btn-sm&#39;>上一页</a>  
20         <span>{$page}/{$pages}</span>
21         <a href=&#39;__SELF__/p/{$nextpage}&#39; class=&#39;btn btn-warning btn-sm&#39;>下一页</a>
22         <a href=&#39;__SELF__/p/{$pages}&#39; class=&#39;btn btn-warning btn-sm&#39;>末页</a>
23     </h4>";
24     C(&#39;limit&#39;,$limit);
25     C(&#39;show&#39;,$show);
26 }

This defines the paging function. The class of the a connection can be defined by yourself, or not defined, and then the paging html definition style is selected from the parent element through css on the page.
Then refer to the paging function in Action: (The red part is the key code)
1         $goods=M(&#39;Goods&#39;);
2         $count=$goods->where(&#39;is_pass=1 and is_self=1&#39;)->count();
3         mypage($count,5);
4         $this->rows=$goods->where(&#39;is_pass=1 and is_self=1&#39;)->limit(C(&#39;limit&#39;))->order(&#39;trade_num desc,price asc&#39;)->select();
5         $this->assign(&#39;show&#39;,C(&#39;show&#39;));
6         $this->display();

Referenced in tpl template:
1                                                                      
2                                                                                              
3                     16b28748ea4df4d9c2150843fecfba68
Because I am using bootstrap, the effect is as follows. The style can be defined by yourself through css

http://www.bkjia.com/PHPjc/1047179.html

truehttp: //www.bkjia.com/PHPjc/1047179.htmlTechArticleTransformation of ThinkPHP paging functions. First, after creating the ThinkPHP project, open ThinkPHP/Common/functions in the root directory. php (the public functions of TP are stored here) add the following code 1...
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