TP3.2框架分页相关实现方法分析

本文实例讲述了TP3.2框架分页相关实现方法,分享给大家供大家参考,具体如下:

需要用到分页的继承这个控制器即可

  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class BaseController extends Controller {
  5. /**
  6. * 基础分页的相同代码封装,使前台的代码更少
  7. * @param $count 要分页的总记录数
  8. * @param int $pagesize 每页查询条数
  9. * @return \Think\Page
  10. */
  11. function getpage($count, $pagesize = 20,$waps) {
  12. $Page = new \Think\Page($count,$pagesize,$waps);
  13. $Page->rollPage = 5;
  14. $Page->setConfig('header', '<li class="rows">第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
  15. $Page->setConfig('prev', '上一页');
  16. $Page->setConfig('next', '下一页');
  17. $Page->setConfig('last', '末页');
  18. $Page->setConfig('first', '首页');
  19. $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
  20. $Page->lastSuffix = false;//最后一页不显示为总页数
  21. return $Page;
  22. }
  23. /**
  24. * @param $model 模型
  25. * @param $map where条件
  26. * @param $find 0 select 1=find 2=关联表
  27. * @return $list 数组
  28. */
  29. function _select($model,$map,$order='',$field="",$find="0",$limit=20,$join,$param){
  30. $waps = $param;
  31. if(emptyempty($model) && emptyempty($map)){
  32. $this->list = array();
  33. }else{
  34. if($find == 0){
  35. $count = $model->where($map)->count();
  36. $Page = $this->getpage($count,$limit,$waps);
  37. if($Page->firstRow < 0){
  38. $Page->firstRow = 0;
  39. }
  40. if($order != ""){
  41. $this->list = $model->field($field)->where($map)->order($order)->limit($Page->firstRow. ',' . $Page->listRows)->select();
  42. }else{
  43. $this->list = $model->field($field)->where($map)->limit($Page->firstRow. ',' . $Page->listRows)->select();
  44. }
  45. $this->page = $Page->show();
  46. }else if($find == 1){
  47. $this->list = $model->field($field)->where($map)->find();
  48. }else if($find == 3){
  49. $count = $model->alias("t1")->join($join)->where($map)->count();
  50. $Page = $this->getpage($count,$limit,$waps);
  51. if($Page->firstRow < 0){
  52. $Page->firstRow = 0;
  53. }
  54. if($order != ""){
  55. $this->list = $model->alias("t1")->join($join)->field($field)->where($map)->order($order)->limit($Page->firstRow. ',' . $Page->listRows)->select();
  56. }else{
  57. $this->list = $model->alias("t1")->join($join)->field($field)->where($map)->limit($Page->firstRow. ',' . $Page->listRows)->select();
  58. }
  59. $this->page = $Page->show();
  60. }else if($find == 4){
  61. if($order != ""){
  62. $this->list = $model->alias("t1")->join($join)->field($field)->where($map)->order($order)->limit($Page->firstRow. ',' . $Page->listRows)->find();
  63. }else{
  64. $this->list = $model->alias("t1")->join($join)->field($field)->where($map)->limit($Page->firstRow. ',' . $Page->listRows)->find();
  65. }
  66. }
  67. $this->limit = $Page->firstRow;
  68. $this->assign("listinfo",$this->list);
  69. $this->assign("page",$this->page);
  70. return($this->list);
  71. }
  72. }
  73. }

控制器中先继承后使用。

  1. if($this->txt_keyword){
  2. $where['m_title'] = array('like','%'.$this->txt_keyword.'%');
  3. }
  4. $where['status'] = $param['status'] = 0;
  5. $this->_select($this->information_mod,$where,'','*',0,10,'',$param);
  6. $this->display();

模板中

  1. <div class="hg_page" >
  2. {$page}
  3. </div>

分页样式

  1. <style>
  2. .hg_page .first, .hg_page .current, .hg_page .num, .hg_page .next, .hg_page .end, .hg_page .rows, .hg_page .prev{
  3. float: left;
  4. height: 37px;
  5. padding: 0 15px;
  6. border: 1px solid #e5e5e5;
  7. line-height: 37px;
  8. margin: 0 3px;
  9. }
  10. .hg_page{
  11. text-align: center;
  12. height: 37px;
  13. margin:0 auto;
  14. margin-top: 30px;
  15. margin-bottom: 20px;
  16. width: 800px;
  17. }
  18. </style>