thinkphp分頁方法與實現代碼

來源:互聯網
上載者:User
  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}
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.