Heim  >  Artikel  >  Backend-Entwicklung  >  thinkphp分页方法与实现代码

thinkphp分页方法与实现代码

WBOY
WBOYOriginal
2016-07-25 08:52:24925Durchsuche
  1. /**
  2. * TODO 基础分页的相同代码封装,使前台的代码更少
  3. * @param $m 模型,引用传递
  4. * @param $where 查询条件
  5. * @param int $pagesize 每页查询条数
  6. * @return \Think\Page
  7. */
  8. function getpage(&$m,$where,$pagesize=10){
  9. $m1=clone $m;//浅复制一个模型
  10. $count = $m->where($where)->count();//连惯操作后会对join等操作进行重置
  11. $m=$m1;//为保持在为定的连惯操作,浅复制一个模型
  12. $p=new Think\Page($count,$pagesize);
  13. $p->lastSuffix=false;
  14. $p->setConfig('header','
  15. %TOTAL_ROW%条记录 每页%LIST_ROW%条 第%NOW_PAGE%页/共%TOTAL_PAGE%
  16. ');
  17. $p->setConfig('prev','上一页');
  18. $p->setConfig('next','下一页');
  19. $p->setConfig('last','末页');
  20. $p->setConfig('first','首页');
  21. $p->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
  22. $p->parameter=I('get.');
  23. $m->limit($p->firstRow,$p->listRows);
  24. return $p;
  25. }
复制代码

getpage方法可以放在TP框架的 Application/Common/Common/function.php,这个文档可以专门放置一些通用的方法,在哪里都可以调用(如:Controller文件,View文件等)。

二、调用分页方法

  1. $m=M('products');
  2. $p=getpage($m,$where,10);
  3. $list=$m->field(true)->where($where)->order('id desc')->select();
  4. $this->list=$list;
  5. $this->page=$p->show();
  6. 这是View代码
  7.   {$page}
  • 复制代码

    三、分页样式

    1. .pagination ul {
    2. display: inline-block;
    3. margin-bottom: 0;
    4. margin-left: 0;
    5. -webkit-border-radius: 3px;
    6. -moz-border-radius: 3px;
    7. border-radius: 3px;
    8. -webkit-box-shadow: 0 1px 2px rgba(0,0,0,0.05);
    9. -moz-box-shadow: 0 1px 2px rgba(0,0,0,0.05);
    10. box-shadow: 0 1px 2px rgba(0,0,0,0.05);
    11. }
    12. .pagination ul li {
    13. display: inline;
    14. }
    15. .pagination ul li.rows {
    16. line-height: 30px;
    17. padding-left: 5px;
    18. }
    19. .pagination ul li.rows b{color: #f00}
    20. .pagination ul li a, .pagination ul li span {
    21. float: left;
    22. padding: 4px 12px;
    23. line-height: 20px;
    24. text-decoration: none;
    25. background-color: #fff;
    26. background: url('../images/bottom_bg.png') 0px 0px;
    27. border: 1px solid #d3dbde;
    28. /*border-left-width: 0;*/
    29. margin-left: 2px;
    30. color: #08c;
    31. }
    32. .pagination ul li a:hover{
    33. color: red;
    34. background: #0088cc;
    35. }
    36. .pagination ul li.first-child a, .pagination ul li.first-child span {
    37. border-left-width: 1px;
    38. -webkit-border-bottom-left-radius: 3px;
    39. border-bottom-left-radius: 3px;
    40. -webkit-border-top-left-radius: 3px;
    41. border-top-left-radius: 3px;
    42. -moz-border-radius-bottomleft: 3px;
    43. -moz-border-radius-topleft: 3px;
    44. }
    45. .pagination ul .disabled span, .pagination ul .disabled a, .pagination ul .disabled a:hover {
    46. color: #999;
    47. cursor: default;
    48. background-color: transparent;
    49. }
    50. .pagination ul .active a, .pagination ul .active span {
    51. color: #999;
    52. cursor: default;
    53. }
    54. .pagination ul li a:hover, .pagination ul .active a, .pagination ul .active span {
    55. background-color: #f0c040;
    56. }
    57. .pagination ul li.last-child a, .pagination ul li.last-child span {
    58. -webkit-border-top-right-radius: 3px;
    59. border-top-right-radius: 3px;
    60. -webkit-border-bottom-right-radius: 3px;
    61. border-bottom-right-radius: 3px;
    62. -moz-border-radius-topright: 3px;
    63. -moz-border-radius-bottomright: 3px;
    64. }
    65. .pagination ul li.current a{color: #f00 ;font-weight: bold; background: #ddd}
    复制代码


    Stellungnahme:
    Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn