ホームページ  >  記事  >  バックエンド開発  >  Smarty での PHP ページング コードの使用例

Smarty での PHP ページング コードの使用例

WBOY
WBOYオリジナル
2016-07-25 08:52:381886ブラウズ
  1. /**
  2. * ページネーションクラス
  3. */
  4. class Pager {
  5. var $total; // レコードの総数
  6. var $pageSize // 各ページに表示されるレコードの数
  7. var $; currentPage; // 現在のページ番号
  8. var $offset; // レコードのオフセット
  9. var $numberOffset = 5; // ページ番号のオフセット
  10. ;リクエストパラメータ
  11. //=================
  12. //Fn: Pager
  13. //Function: Constructor
  14. //============= = ====
  15. public function __construct ($total, $pageSize, $currentPage, $request = "") {
  16. $this->total = $total;
  17. $this->pageSize = $pageSize;
  18. $ this ->pageOffset();
  19. $this->pageTotal();
  20. $this->currentPage($currentPage);
  21. $this->request = $request;
  22. }
  23. //==== = ============
  24. //Fn: pageOffset
  25. //関数: データベース レコード オフセット
  26. //=================
  27. パブリック関数pageOffset() {
  28. return $this->offset = $this->pageSize * ($this->currentPage - 1);
  29. }
  30. //============ = ====
  31. //Fn: pageTotal
  32. //関数: 総ページ数を計算
  33. //==================
  34. public function pageTotal() {
  35. return $this->pageTotal = ceil($this->total / $this->pageSize);
  36. }
  37. //=================
  38. / /Fn : currentPage
  39. //関数: ページ数を設定します
  40. //==================
  41. public function currentPage($currentPage) {
  42. if (isset($ currentPage)) {
  43. $this->currentPage = intval($currentPage);
  44. } else {
  45. $this->currentPage = 1;
  46. }
  47. return $this->currentPage;
  48. }
  49. //== ===== ==========
  50. //Fn: nextPage
  51. //機能: 次のページにジャンプします
  52. //================ ==
  53. public function nextPage() {
  54. // レコード数を表示
  55. $link = "合計 {$this->total} レコード ";
  56. // ページ ステップ サイズ
  57. $stepPage = $this-> ;currentPage ?ceil($this->currentPage / $this->numberOffset) : 1;
  58. // ページ番号設定
  59. $numberPage = ($this->pageTotal > $this->numberOffset) ? $this->numberOffset : $this->pageTotal;
  60. // 1 ページのみ
  61. if ($this->total pageSize) {
  62. $link .= "[ホーム] ]|[最後のページ] ";
  63. } else {
  64. // 総ページ数と現在のページを設定します
  65. $link .= "ページ {$this->currentPage}/{$this->pageTotal}  ;";
  66. // ホームページ
  67. $link .= "request}>[ホーム] " ;
  68. // 次の列
  69. if ($stepPage > 1) {
  70. $lastIndex = ($stepPage - 1) * $this->numberOffset;
  71. $link .= "request}>[";
  72. }
  73. // 前のページ
  74. if ($this->currentPage > ; 1) {
  75. $prePage = $this->currentPage - 1;
  76. $link .="request}> ;[";
  77. }
  78. // 数値のページ番号
  79. $i = ($stepPage - 1) * $this->numberOffset;
  80. for ($j = $i; $ j pageTotal; $j++) {
  81. $newPage = $j + 1;
  82. if ($this->currentPage == $j + 1 ) {
  83. $link .= " [" . ($j + 1) "]";
  84. } else {
  85. $link .= "[" . ($j+1) . "]";
  86. }
  87. }
  88. //次のページ
  89. if ($this->currentPage pageTotal){
  90. $nextPage = $this->currentPage + 1;
  91. $link .= "[ >]";
  92. }
  93. // 次の列
  94. if ($stepPage total) {
  95. $nextPage = $stepPage * ($this- >numberOffset + 1) ;
  96. if ($nextPage pageTotal) {
  97. $link .= " request}>[> ;>]";
  98. }
  99. }
  100. // 最後のページ
  101. if ($this->currentPage pageTotal) {
  102. $link .= "..pageTotal}{$this->request}>[最後のページ]";
  103. }
  104. }
  105. return $link;
  106. }
  107. }
  108. ?>
コードをコピー

2 番目、PHP ページング クラスの呼び出し例:

1. URL によって返されるページ数を取得します。

  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、スマートな割り当て:

  1. $show = 表示するデータを取得します
  2. $this->gt;tpl->assign('numlink', $pager->nextPage()) // ページングリストを取得します
  3. $this -> ;tpl->assign('data',$show);
コードをコピー

ページネーション効果: Smarty での PHP ページング コードの使用例

上記のページング コードは、アドレス バーに渡されるすべての情報を公開する URL リダイレクトを実装していません。これを改善することができます。



声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。