php分頁類別程式碼與smarty結合使用的例子

WBOY
發布: 2016-07-25 08:52:38
原創
1976 人瀏覽過
  1. /**
  2. * 分頁類
  3. */
  4. class Pager {
  5. var $total; // 記錄總數
  6. var Pager {
  7. var $total; // 記錄總數
  8. varvar $pageSize; // 每一頁顯示的記錄數
  9. var $currentPage; // 目前頁碼
  10. var $offset; // 記錄偏移量
  11. var $pageTotal; // 總頁數
  12. var $numberOffset = 5; // 頁碼偏移量
  13. var $request = ""; // 頁面請求參數
  14. //=================
  15. //Fn: Pager
  16. //功能:建構子
  17. //=================
  18. public function __construct ($total, $ pageSize, $currentPage, $request = "") {
  19. $this->total = $total;
  20. $this->pageSize = $pageSize;
  21. $this->pageOffset();
  22. $this->pageTotal();
  23. $this->currentPage($currentPage);
  24. $this->request = $request;
  25. }
  26. //======== =========
  27. //Fn: pageOffset
  28. //功能:資料庫記錄偏移
  29. //================ =
  30. public function pageOffset() {
  31. return $this->offset = $this->pageSize * ($this->currentPage - 1);
  32. }
  33. //== ===============
  34. //Fn: pageTotal
  35. //功能:計算總頁數
  36. //=========== ======
  37. public function pageTotal() {
  38. return $this->pageTotal = ceil($this->total / $this->pageSize);
  39. }
  40. //=================
  41. //Fn: currentPage
  42. //功能:設定頁數
  43. //======== =========
  44. public function currentPage($currentPage) {
  45. if (isset($currentPage)) {
  46. $this->currentPage = intval($currentPage);
  47. $this->currentPage = intval($currentPage);
  48. $this->currentPage = intval($currentPage);
  49. } else {
  50. $this->currentPage = 1;
  51. }
  52. return $this->currentPage;
  53. }
  54. //============= ====
  55. //Fn: nextPage
  56. //功能:跳到下一頁
  57. //=================
  58. public function nextPage() {
  59. // 顯示記錄數
  60. $link = "共{$this->total}條 ";
  61. // 頁碼步長
  62. $stepPage = $stepPage = $stepPage = $ this->currentPage ? ceil($this->currentPage / $this->numberOffset) : 1;
  63. // 數字頁碼設定
  64. $numberPage = ($this->pageTotal > $this- >numberOffset) ? $this->numberOffset : $this->pageTotal;
  65. // 只有一頁
  66. if ($this->total pageSize) {
  67. $ link .= "[首頁]|[末頁]";
  68. } else {
  69. // 設定總頁數與目前頁
  70. $link .= "第{$this->currentPage}/{ $this->pageTotal}頁 ";
  71. // 首頁
  72. $link .= "request}>[首頁]  ";
  73. // 下一列
  74. if ($stepPage > 1) {
  75. $lastIndex = ($stepPage - 1) * $this->numberOffset;
  76. $stepPage - 1) * $this->numberOffset;
  77. $ link .= "request}>[";
  78. }
  79. / / 上一頁
  80. if ($this->currentPage > 1) {
  81. $prePage = $this->currentPage - 1;
  82. $link .="request}>[";
  83. }
  84. // 數字頁碼
  85. $i = ($stepPage - 1 ) * $this->numberOffset;
  86. for ($j = $i; $j pageTotal; $j ) {
  87. $newPage = $j 1;
  88. if ($this->currentPage == $j 1) {
  89. $link .= "[" . ($j 1) . "]";
  90. } else {
  91. $link .= "request}>[" . ($j 1) . "]";
  92. }
  93. }
  94. //下一頁
  95. if ($this->currentPage pageTotal){
  96. $nextPage = $this->currentPage 1;
  97. $link .= "request}>[>]";
  98. }
  99. //下一列
  100. if ($stepPage total) {
  101. $nextPage = $stepPage * ($this->numberOffset 1);
  102. if ($nextPage pageTotal) {
  103. $link .= "request}>[>>]";
  104. }
  105. }
  106. // 末頁
  107. if ($this->currentPage pageTotal) {
  108. $link .= ".. pageTotal}{$this->request}>[末頁]";
  109. }
}
return $link; }}
? >複製程式碼

二,php分頁類別呼叫範例:

1,取得URL傳回的page頁數:

  1. $cur_page = 1;
  2. if (isset($_GET["pageNo"])) {
  3. $cur_page = $_GET[" pageNo"];
  4. }
複製程式碼

2,建立分頁物件:

  1. $nums:某資料的總數
  2. $page_size:每頁顯示數字
  3. $cur_page:目前頁數
  4. $request:其他Url請求可選參數
  5. $pager = new Pager($nums, $page_size, $cur_page, $request);
複製程式碼

3, smarty賦值:

  1. $show = 得到要顯示的資料
  2. $this->tpl->assign('numlink', $pager->nextPage()) ; // 得到分頁清單
  3. $this->tpl->assign('data',$show);
複製程式碼

分頁效果: php分頁類別程式碼與smarty結合使用的例子

以上分頁代碼沒有實現url重定向,使得在網址列中所有傳遞的資訊都暴露出來了,大家可以進行完善下。



來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!