Home  >  Article  >  Backend Development  >  thinkphp3.2.3 paging code sharing_php example

thinkphp3.2.3 paging code sharing_php example

WBOY
WBOYOriginal
2016-08-17 13:02:391002browse

For the implementation effect of thinkphp paging, there are two calling methods, one is to call the function method in the public function (refer to http://www.cnblogs.com/tianguook/p/4326613.html), and the other is to write in the model How to paginate


1. Write in the public function Application/Common/Common/function.php:

function getpage($count,$pagesize=10) {
 $page=new Think\Page($count,$pagesize);
 $page->setConfig('header', '<li>共<b>%TOTAL_ROW%</b>条记录 <b>%NOW_PAGE%</b>/<b>%TOTAL_PAGE%</b>页</li>');
 $page->setConfig('prev', '上一页');
 $page->setConfig('next', '下一页');
 $page->setConfig('last', '末页');
 $page->setConfig('first', '首页');
 $page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
 $page->lastSuffix=false;//最后一页不显示总页数
 return $page;
}

Call in controller PageController.class.php

namespace Home\Controller;
use Think\Controller;
class PageController extends Controller {
 public function index() {
  $m=M('user');
  $count=$m->count();
  $page= getpage($count,8);//Common/function.php中分页
  $list=$m->limit($page->firstRow,$page->listRows)->select();
  $this->assign('list',$list);//赋值数据集
  $this->assign('page',$page->show());//赋值分页输出
  $this->display();
 }
}

Displayed in view index/index.html

<!DOCTYPE html>
<html>
 <head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   <link href="__CSS__/page.css" rel="stylesheet" />
 </head>
 <body>
  <div>
   <volist name="list" id="vo">
    <notemply name="$vo['name']">
     用户名:<p>{$vo['name']}</p>
    </notemply>
   </volist>
   <div>
    <table>
     <tr>
      <td colspan="3" bgcolor="#FFFFFF">
       <div class="pages">{$page} </div>
      </td>
     </tr>
    </table>
   </div>
  </div>
 </body>
</html>

Style writing page.css

.pages a,.pages span {
 display:inline-block;
 padding:2px 5px;
 margin:0 1px;
 border:1px solid #f0f0f0;
 -webkit-border-radius:3px;
 -moz-border-radius:3px;
 border-radius:3px;
}
.pages a,.pages li {
 display:inline-block;
 list-style: none;
 text-decoration:none; color:#58A0D3;
}
.pages a.first,.pages a.prev,.pages a.next,.pages a.end{
 margin:0;
}
.pages a:hover{
 border-color:#50A8E6;
}
.pages span.current{
 background:#50A8E6;
 color:#FFF;
 font-weight:700;
 border-color:#50A8E6;
}

2. Write the paging function in the template UserModel.class.php

namespace Home\Model;
use Think\Model;
class UserModel extends Model {
 public function getPage() {
   $page=I('p',0,'int');
   $limit=8;
   $data=$this->page($page,$limit)->select();
   $count= $this->count();
   $Page=new \Think\Page($count, $limit);
   
   $Page->lastSuffix=false;//是否显示总页数
   $Page->setConfig('header','<li>共<b>%TOTAL_ROW%</b>幅图片  每页<b>'.$limit.'</b>幅  <b>%NOW_PAGE%</b>/<b>%TOTAL_PAGE%</b>页</li>');
   $Page->setConfig('prev','上一页');
   $Page->setConfig('next','下一页');
   $Page->setConfig('last','末页');
   $Page->setConfig('first','首页');
   $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
   $show=$Page->show();
   return array('list'=>$data,'page'=>$show);
 }
}

Call in controller PageController.class.php

namespace Home\Controller;
use Think\Controller;
class PageController extends Controller {
 public function index() {
  $m=D('Upload_img');
  $list=$m->getPage();//model中分页
  $this->assign('list',$list);//赋值数据集
  $this->display();
 }
}

View shows index/index.html

<!DOCTYPE html>
<html>
 <head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   <link href="__CSS__/page.css" rel="stylesheet" />
 </head>
 <body>
  <div>
   <volist name="list.list" id="vo">
    <notemply name="$vo['name']">
     用户名:<p>{$vo['name']}
    </notemply>
   </volist>
   <div>
    <table>
     <tr>
      <td colspan="3" bgcolor="#FFFFFF">
       <div class="pages">{$list.page} </div>
      </td>
     </tr>
    </table>
   </div>
  </div>
 </body>
</html>

The style of paging is the same as the style in page.css in the public method in the first
The above 8d3bdc0d50d8a618325d7e8ff6ac5b03refers to the file constant "__CSS__"configured in the public configuration file:

 return array(   'TMPL_PARSE_STRING'=>array(
  '__CSS__'=>__ROOT__.'/Public/Css',
 ))

The above is the entire content of this article. I hope it will be helpful to everyone’s study. I also hope that everyone will support Script Home.

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