当前位置: 移动技术网 > IT编程>开发语言>PHP > thinkphp3.2.3 分页代码分享

thinkphp3.2.3 分页代码分享

2017年12月12日  | 移动技术网IT编程  | 我要评论

对于thinkphp分页的实现效果,两种调用方法,一种调用公共函数中的函数方法(参考),一种是在模型中书写分页的方法

 

1、在公共函数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;
}

在控制器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();
 }
}

在视图index/中显示 

<!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>

样式的书写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、在模板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);
 }
}

控制器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();
 }
}

视图显示index/ 

<!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>

分页的样式与第1中公共方法中page.css中样式相同 
以上<link href="__css__/page.css" rel="stylesheet" />引用文件常量"__css__"在公共配置文件中配置:

 return array(   'tmpl_parse_string'=>array(
  '__css__'=>__root__.'/public/css',
 ))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网