Heim  >  Artikel  >  Backend-Entwicklung  >  php分页类代码与smarty结合使用的例子

php分页类代码与smarty结合使用的例子

WBOY
WBOYOriginal
2016-07-25 08:52:381929Durchsuche
  1. /**
  2. * 分页类
  3. */
  4. class Pager {
  5. var $total; // 记录总数
  6. var $pageSize; // 每一页显示的记录数
  7. var $currentPage; // 当前页码
  8. var $offset; // 记录偏移量
  9. var $pageTotal; // 总页数
  10. var $numberOffset = 5; // 页码偏移量
  11. var $request = ""; // 页面请求参数
  12. //=================
  13. //Fn: Pager
  14. //功能:构造函数
  15. //=================
  16. public function __construct ($total, $pageSize, $currentPage, $request = "") {
  17. $this->total = $total;
  18. $this->pageSize = $pageSize;
  19. $this->pageOffset();
  20. $this->pageTotal();
  21. $this->currentPage($currentPage);
  22. $this->request = $request;
  23. }
  24. //=================
  25. //Fn: pageOffset
  26. //功能:数据库记录偏移量
  27. //=================
  28. public function pageOffset() {
  29. return $this->offset = $this->pageSize * ($this->currentPage - 1);
  30. }
  31. //=================
  32. //Fn: pageTotal
  33. //功能:计算总页数
  34. //=================
  35. public function pageTotal() {
  36. return $this->pageTotal = ceil($this->total / $this->pageSize);
  37. }
  38. //=================
  39. //Fn: currentPage
  40. //功能:设置页数
  41. //=================
  42. public function currentPage($currentPage) {
  43. if (isset($currentPage)) {
  44. $this->currentPage = intval($currentPage);
  45. } else {
  46. $this->currentPage = 1;
  47. }
  48. return $this->currentPage;
  49. }
  50. //=================
  51. //Fn: nextPage
  52. //功能:跳转到下一页
  53. //=================
  54. public function nextPage() {
  55. // 显示记录数
  56. $link = "共{$this->total}条 ";
  57. // 页码步长
  58. $stepPage = $this->currentPage ? ceil($this->currentPage / $this->numberOffset) : 1;
  59. // 数字页码设定
  60. $numberPage = ($this->pageTotal > $this->numberOffset) ? $this->numberOffset : $this->pageTotal;
  61. // 只有一页
  62. if ($this->total pageSize) {
  63. $link .= "[首页]|[末页]";
  64. } else {
  65. // 设置总页数和当前页
  66. $link .= "第{$this->currentPage}/{$this->pageTotal}页 ";
  67. // 首页
  68. $link .= "request}>[首页] ";
  69. // 下一列
  70. if ($stepPage > 1) {
  71. $lastIndex = ($stepPage - 1) * $this->numberOffset;
  72. $link .= "request}>[";
  73. }
  74. // 上一页
  75. if ($this->currentPage > 1) {
  76. $prePage = $this->currentPage - 1;
  77. $link .="request}>[";
  78. }
  79. // 数字页码
  80. $i = ($stepPage - 1) * $this->numberOffset;
  81. for ($j = $i; $j pageTotal; $j++) {
  82. $newPage = $j + 1;
  83. if ($this->currentPage == $j + 1) {
  84. $link .= "[" . ($j + 1) . "]";
  85. } else {
  86. $link .= "request}>[" . ($j+1) . "]";
  87. }
  88. }
  89. //下一页
  90. if ($this->currentPage pageTotal){
  91. $nextPage = $this->currentPage + 1;
  92. $link .= "request}>[>]";
  93. }
  94. // 下一列
  95. if ($stepPage total) {
  96. $nextPage = $stepPage * ($this->numberOffset + 1);
  97. if ($nextPage pageTotal) {
  98. $link .= "request}>[>>]";
  99. }
  100. }
  101. // 末页
  102. if ($this->currentPage pageTotal) {
  103. $link .= "..pageTotal}{$this->request}>[末页]";
  104. }
  105. }
  106. return $link;
  107. }
  108. }
  109. ?>
复制代码

二,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分页类代码

以上分页代码没有实现url重定向,使得在地址栏中所有传递的信息都暴露出来了,大家可以进行完善下。



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn