Home > php教程 > php手册 > ThinkPHP 分页函数的改造

ThinkPHP 分页函数的改造

WBOY
Release: 2016-06-13 08:56:06
Original
1028 people have browsed it

ThinkPHP 分页函数的改造

首先在创建好ThinkPHP项目以后,打开根目录下的ThinkPHP/Common/functions.php(这里存放的是tp的公共函数)

 

添加如下代码

 

 

 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 }
Copy after login

这样就定义好了分页函数。其中a连接的class可以自己定义,也可以不定义,之后在页面中通过css从父元素选中分页的html定义样式。

之后就是在Action中引用分页函数:(红色部分是关键代码)

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();
Copy after login

 

在tpl模板中引用:

 

1                        

2                            

3                        

因为我用的是bootstrap所以效果如下,样式可以自己通过css定义

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template