php分頁類程式碼範例,可在php架構中使用的分頁類

來源:互聯網
上載者:User
  1. //php分頁類代碼
  2. class page{
  3. public $page; //當前頁
  4. public $pagenum; // 頁數
  5. public $pagesize; // 每頁顯示條數
  6. public function __construct($count, $pagesize){
  7. $this->pagenum = ceil($count/$pagesize);
  8. $this->pagesize = $pagesize;
  9. $this->page =(isset($_GET['p'])&&$_GET['p']>0) ? intval($_GET['p']) : 1;
  10. }
  11. /**
  12. * 獲得 url 後面get傳遞的參數
  13. */
  14. public function getUrl(){
  15. $url = 'index.php?'.http_build_query($_GET);
  16. $url = preg_replace('/[?,&]p=(\w)+/','',$url);
  17. $url .= (strpos($url,"?") === false) ? '?' : '&';
  18. return $url;
  19. }
  20. /**
  21. * 獲得分頁html
  22. */
  23. public function getPage(){
  24. $url = $this->getUrl();
  25. $start = $this->page-5;
  26. $start=$start>0 ? $start : 1;
  27. $end = $start+9;
  28. $end = $end<$this->pagenum ? $end : $this->pagenum;
  29. $pagestr = '';
  30. if($this->page>5){
  31. $pagestr = "首頁 ";
  32. }
  33. if($this->page!=1){
  34. $pagestr.= "page-1).">上一頁";
  35. }
  36. for($i=$start;$i<=$end;$i++){
  37. $pagestr.= "".$i." ";
  38. }
  39. if($this->page!=$this->pagenum){
  40. $pagestr.="page+1).">下一頁";
  41. }
  42. if($this->page+5<$this->pagenum){
  43. $pagestr.="pagenum.">尾頁 ";
  44. }
  45. return $pagestr;
  46. } // edit: bbs.it-home.org
  47. }
  48. // 分頁代碼測試
  49. $page = new page(100,10);
  50. $str=$page->getPage();
  51. echo $str;
  52. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.