PaginateHelper.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2022/11/3
  6. * Time: 16:49
  7. */
  8. namespace paginate;
  9. class PaginateHelper
  10. {
  11. private $total; //数据表中总记录数
  12. private $listRows; //每页显示行数
  13. private $page; //当前页码
  14. private $pageNum; //总页数
  15. private $config = array("prev" => "上一页", "next" => "下一页", "first" => "首 页", "last" => "尾 页");
  16. private $uri;
  17. private $listNum = 8;
  18. /**
  19. * PaginateHelper constructor.
  20. * @param $total int 总记录数
  21. * @param int $listRows 每页显示行数
  22. * @param null $currentPage 当前页码
  23. * @param string $query_string 追加get参数
  24. */
  25. public function __construct($total, $listRows = 10, $currentPage = null, $query_string = "")
  26. {
  27. $this->total = $total;//总记录数
  28. $this->listRows = $listRows;//每页记录数
  29. $this->pageNum = ceil($this->total / $this->listRows);//计算总页数
  30. $this->uri = $this->getUri($query_string);
  31. $this->setPage($currentPage);
  32. }
  33. /**
  34. * 获取sql的limit语句
  35. * @return string
  36. */
  37. public function getLimitStr()
  38. {
  39. return "Limit " . ($this->page - 1) * $this->listRows . ", {$this->listRows}";
  40. }
  41. private function setPage($currentPage = null)
  42. {
  43. $page = $currentPage ?: ($_GET["page"]??1);
  44. $page = intval($page);
  45. $this->page = $page > 0 ? ($page > $this->pageNum ? $this->pageNum : $page) : 1;//当前页码
  46. }
  47. /**
  48. * 获取当前uri
  49. * @param $query_string
  50. * @return string
  51. */
  52. public function getUri($query_string)
  53. {
  54. $url = $_SERVER["REQUEST_URI"] . (strpos($_SERVER["REQUEST_URI"], '?') ? '' : "?") . $query_string;
  55. $parse = parse_url($url);
  56. if (isset($parse["query"])) {
  57. parse_str($parse['query'], $params);
  58. unset($params["page"]);//去除page参数
  59. $url = $parse['path'] . '?' . http_build_query($params);
  60. }
  61. return $url;
  62. }
  63. /**
  64. * 当前页的起始位置
  65. * @return int
  66. */
  67. public function start()
  68. {
  69. if ($this->total == 0)
  70. return 0;
  71. else
  72. return ($this->page - 1) * $this->listRows + 1;//记录从1开始
  73. }
  74. /**
  75. * 当前页的结束位置
  76. * @return mixed
  77. */
  78. public function end()
  79. {
  80. return min($this->page * $this->listRows, $this->total);
  81. }
  82. //首页
  83. private function first()
  84. {
  85. $html = "";
  86. if ($this->page == 1)
  87. $html .= '';
  88. else
  89. $html .= "&nbsp;&nbsp;<a href='{$this->uri}&page=1'>{$this->config["first"]}</a>&nbsp;&nbsp;";
  90. return $html;
  91. }
  92. //上一页
  93. private function prev()
  94. {
  95. $html = "";
  96. if ($this->page == 1)
  97. $html .= '';
  98. else
  99. $html .= "&nbsp;&nbsp;<a href='{$this->uri}&page=" . ($this->page - 1) . "'>{$this->config["prev"]}</a>&nbsp;&nbsp;";
  100. return $html;
  101. }
  102. private function pageList()
  103. {
  104. $linkPage = "";
  105. $inum = floor($this->listNum / 2);
  106. for ($i = $inum; $i >= 1; $i--) {
  107. $page = $this->page - $i;
  108. if ($page < 1)
  109. continue;
  110. $linkPage .= "&nbsp;<a href='{$this->uri}&page={$page}'>{$page}</a>&nbsp;";
  111. }
  112. $linkPage .= "&nbsp;{$this->page}&nbsp;";
  113. for ($i = 1; $i <= $inum; $i++) {
  114. $page = $this->page + $i;
  115. if ($page <= $this->pageNum)
  116. $linkPage .= "&nbsp;<a href='{$this->uri}&page={$page}'>{$page}</a>&nbsp;";
  117. else
  118. break;
  119. }
  120. return $linkPage;
  121. }
  122. //下一页
  123. private function next()
  124. {
  125. $html = "";
  126. if ($this->page >= $this->pageNum)
  127. $html .= '';
  128. else
  129. $html .= "&nbsp;&nbsp;<a href='{$this->uri}&page=" . ($this->page + 1) . "'>{$this->config["next"]}</a>&nbsp;&nbsp;";
  130. return $html;
  131. }
  132. //尾页
  133. private function last()
  134. {
  135. $html = "";
  136. if ($this->page == $this->pageNum)
  137. $html .= '';
  138. else
  139. $html .= "&nbsp;&nbsp;<a href='{$this->uri}&page=" . ($this->pageNum) . "'>{$this->config["last"]}</a>&nbsp;&nbsp;";
  140. return $html;
  141. }
  142. //跳到第几页
  143. private function goPage()
  144. {
  145. return '&nbsp;&nbsp;<input type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>' . $this->pageNum . ')?' . $this->pageNum . ':this.value;location=\'' . $this->uri . '&page=\'+page+\'\'}" value="' . $this->page . '" style="width:25px"><input type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>' . $this->pageNum . ')?' . $this->pageNum . ':this.previousSibling.value;location=\'' . $this->uri . '&page=\'+page+\'\'">&nbsp;&nbsp;';
  146. }
  147. //显示分页
  148. public function fpage($display = array(0, 1, 2, 3, 4, 5, 6, 7, 8))
  149. {
  150. $html[0] = "&nbsp;&nbsp;共有<b>{$this->total}</b>个记录&nbsp;&nbsp;";
  151. $html[1] = "&nbsp;&nbsp;每页显示<b>{$this->listRows}</b>条,本页<b>{$this->start()}~{$this->end()}</b>条&nbsp;&nbsp;";
  152. $html[2] = "&nbsp;&nbsp;<b>{$this->page}/{$this->pageNum}</b>页&nbsp;&nbsp;";
  153. $html[3] = $this->first();
  154. $html[4] = $this->prev();
  155. $html[5] = $this->pageList();
  156. $html[6] = $this->next();
  157. $html[7] = $this->last();
  158. $html[8] = $this->goPage();
  159. $fpage = '';
  160. foreach ($display as $index) {
  161. $fpage .= $html[$index];
  162. }
  163. return $fpage;
  164. }
  165. }