php分頁類執行個體 php與mysql簡單分頁類代碼

來源:互聯網
上載者:User
  1. class PageModel {
  2. /**
  3. * 擷取分頁數組
  4. * @param unknown $page 當前頁面數
  5. * @param unknown $goodsCount 商品總數
  6. * @param unknown $pageLength 每個頁面展示頁面數
  7. */
  8. public static function getPageArr($page, $goodsCount, $pageCountLength, $pageLength) {
  9. //頁面總數
  10. $allPageCount = ceil($goodsCount / $pageLength);
  11. //如果頁面總是比長度短,設定頁面長度為頁面總數
  12. if ($allPageCount <= $pageCountLength) {
  13. $allPageCount = ceil($goodsCount / $pageLength);
  14. }
  15. //總頁面數一頁展示完
  16. if ($allPageCount <= $pageCountLength) {
  17. for ($i = 0; $i < $allPageCount; $i ++) {
  18. $arr[] = array('page' => $i + 1);
  19. }
  20. return $arr;
  21. }
  22. //前後的長度
  23. $halfLength = floor($pageCountLength / 2);
  24. //因為太小,所以放原來位置,左邊
  25. if ($page <= $halfLength) {
  26. $arr = array();
  27. for ($i = 0; $i < $pageCountLength; $i ++) {
  28. $arr[] = array('page' => $i + 1);
  29. }
  30. return $arr;
  31. }
  32. //太大,只取到邊緣,超出也只取到邊緣
  33. if ($page > $allPageCount - floor($pageCountLength / 2)) {
  34. for ($i = -$pageCountLength; $i < 0; $i ++) {
  35. $arr[] = array('page' => $allPageCount + $i + 1);
  36. }
  37. return $arr;
  38. }
  39. //中間的數,把中間的取出來
  40. for ($i = -$halfLength; $i < $pageCountLength - $halfLength; $i ++) {
  41. $arr[] = array('page' => $page + $i);
  42. }
  43. return $arr;
  44. }
  45. }
複製代碼

二、php分頁類代碼

代碼:

  1. class Helper_Page{

  2. /** 總資訊數 */

  3. var $infoCount;
  4. /** 總頁數 */
  5. var $pageCount;
  6. /** 每頁顯示條數 */
  7. var $items;
  8. /** 當前頁碼 */
  9. var $pageNo;
  10. /** 查詢的起始位置 */
  11. var $startPos;
  12. /** 下一頁 */
  13. var $nextPageNo;
  14. /** 上一頁 */
  15. var $prevPageNo;

  16. function Helper_Page($infoCount, $items, $pageNo)

  17. {
  18. $this->infoCount = $infoCount;
  19. $this->items = $items;
  20. $this->pageNo = $pageNo;
  21. $this->pageCount = $this->GetPageCount();
  22. $this->AdjustPageNo();
  23. $this->startPos = $this->GetStartPos();
  24. }
  25. function AdjustPageNo()
  26. {
  27. if($this->pageNo == '' || $this->pageNo < 1)
  28. $this->pageNo = 1;
  29. if ($this->pageNo > $this->pageCount)
  30. $this->pageNo = $this->pageCount;
  31. }
  32. /**
  33. * 下一頁
  34. */
  35. function GoToNextPage()
  36. {
  37. $nextPageNo = $this->pageNo + 1;
  38. if ($nextPageNo > $this->pageCount)
  39. {
  40. $this->nextPageNo = $this->pageCount;
  41. return false;
  42. }
  43. $this->nextPageNo = $nextPageNo;
  44. return true;
  45. }
  46. /**
  47. * 上一頁
  48. */
  49. function GotoPrevPage()
  50. {
  51. $prevPageNo = $this->pageNo - 1;
  52. if ($prevPageNo < 1)
  53. {
  54. $this->prevPageNo = 1;
  55. return false;
  56. }
  57. $this->prevPageNo = $prevPageNo;
  58. return true;
  59. }
  60. function GetPageCount()
  61. {
  62. return ceil($this->infoCount / $this->items);
  63. }
  64. function GetStartPos()
  65. {
  66. return ($this->pageNo - 1) * $this->items;
  67. }
  68. }

複製代碼
  • 聯繫我們

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