当前位置:Gxlcms > PHP教程 > 一款万能的php分页类实例代码

一款万能的php分页类实例代码

时间:2021-07-01 10:21:17 帮助过:6人阅读

分页 (pagination),即将一个页面分成两个或两个以上的页面。

有一种自动分页机制,可以将移动 Web窗体中的内

容分割成一组组较小的页进行呈现,以适合于特定的设备。该机制还呈现可用于浏览到其他页的用户界面元素。

这篇文章主要介绍了一款万能的php分页类,特别好用,需要使用php分页类的朋友不要错过。代码如下

  1. <?php
  2. /*
  3. * To change this template, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. /**
  7. * 分页类
  8. * 使用方式:
  9. * $page = new Page();
  10. * $page->init(1000, 20);
  11. * $page->setNotActiveTemplate('<span> {a} </span>');
  12. * $page->setActiveTemplate('{a}');
  13. * echo $page->show();
  14. *
  15. *
  16. * @author 风居住的地方
  17. */
  18. class Page {
  19. /**
  20. * 总条数
  21. */
  22. private $total;
  23. /**
  24. * 每页大小
  25. */
  26. private $pageSize;
  27. /**
  28. * 总页数
  29. */
  30. private $pageNum;
  31. /**
  32. * 当前页
  33. */
  34. private $page;
  35. /**
  36. * 地址
  37. */
  38. private $uri;
  39. /**
  40. * 分页变量
  41. */
  42. private $pageParam;
  43. /**
  44. * LIMIT XX,XX
  45. */
  46. private $limit;
  47. /**
  48. * 数字分页显示
  49. */
  50. private $listnum = 8;
  51. /**
  52. * 分页显示模板
  53. * 可用变量参数
  54. * {total} 总数据条数
  55. * {pagesize} 每页显示条数
  56. * {start} 本页开始条数
  57. * {end} 本页结束条数
  58. * {pagenum} 共有多少页
  59. * {frist} 首页
  60. * {pre} 上一页
  61. * {next} 下一页
  62. * {last} 尾页
  63. * {list} 数字分页
  64. * {goto} 跳转按钮
  65. */
  66. private $template = '<p><span>共有{total}条数据</span><span>每页显示{pagesize}条数据</span>,<span>本页{start}-{end}条数据</span><span>共有{pagenum}页</span><ul>{frist}{pre}{list}{next}{last}{goto}</ul></p>';
  67. /**
  68. * 当前选中的分页链接模板
  69. */
  70. private $activeTemplate = '<li class="active"><a href="javascript:;">{text}</a></li>';
  71. /**
  72. * 未选中的分页链接模板
  73. */
  74. private $notActiveTemplate = '<li><a href="{url}">{text}</a></li>';
  75. /**
  76. * 显示文本设置
  77. */
  78. private $config = array('frist' => '首页', 'pre' => '上一页', 'next' => '下一页', 'last' => '尾页');
  79. /**
  80. * 初始化
  81. * @param type $total 总条数
  82. * @param type $pageSize 每页大小
  83. * @param type $param url附加参数
  84. * @param type $pageParam 分页变量
  85. */
  86. public function init($total, $pageSize, $param = '', $pageParam = 'page') {
  87. $this->total = intval($total);
  88. $this->pageSize = intval($pageSize);
  89. $this->pageParam = $pageParam;
  90. $this->uri = $this->geturi($param);
  91. $this->pageNum = ceil($this->total / $this->pageSize);
  92. $this->page = $this->setPage();
  93. $this->limit = $this->setlimit();
  94. }
  95. /**
  96. * 设置分页模板
  97. * @param type $template 模板配置
  98. */
  99. public function setTemplate($template) {
  100. $this->template = $template;
  101. }
  102. /**
  103. * 设置选中分页模板
  104. * @param type $activeTemplate 模板配置
  105. */
  106. public function setActiveTemplate($activeTemplate) {
  107. $this->activeTemplate = $activeTemplate;
  108. }
  109. /**
  110. * 设置未选中分页模板
  111. * @param type $notActiveTemplate 模板配置
  112. */
  113. public function setNotActiveTemplate($notActiveTemplate) {
  114. $this->notActiveTemplate = $notActiveTemplate;
  115. }
  116. /**
  117. * 返回分页
  118. * @return type
  119. */
  120. public function show() {
  121. return str_ireplace(array(
  122. '{total}',
  123. '{pagesize}',
  124. '{start}',
  125. '{end}',
  126. '{pagenum}',
  127. '{frist}',
  128. '{pre}',
  129. '{next}',
  130. '{last}',
  131. '{list}',
  132. '{goto}',
  133. ), array(
  134. $this->total,
  135. $this->setPageSize(),
  136. $this->star(),
  137. $this->end(),
  138. $this->pageNum,
  139. $this->frist(),
  140. $this->prev(),
  141. $this->next(),
  142. $this->last(),
  143. $this->pagelist(),
  144. $this->gopage(),
  145. ), $this->template);
  146. }
  147. /**
  148. * 获取limit起始数
  149. * @return type
  150. */
  151. public function getOffset() {
  152. return ($this->page - 1) * $this->pageSize;
  153. }
  154. /**
  155. * 设置LIMIT
  156. * @return type
  157. */
  158. private function setlimit() {
  159. return "limit " . ($this->page - 1) * $this->pageSize . ",{$this->pageSize}";
  160. }
  161. /**
  162. * 获取limit
  163. * @param type $args
  164. * @return type
  165. */
  166. public function __get($args) {
  167. if ($args == "limit") {
  168. return $this->limit;
  169. } else {
  170. return null;
  171. }
  172. }
  173. /**
  174. * 初始化当前页
  175. * @return int
  176. */
  177. private function setPage() {
  178. if (!empty($_GET[$this->pageParam])) {
  179. if ($_GET[$this->pageParam] > 0) {
  180. if ($_GET[$this->pageParam] > $this->pageNum)
  181. return $this->pageNum;
  182. else
  183. return $_GET[$this->pageParam];
  184. }
  185. }
  186. return 1;
  187. }
  188. /**
  189. * 初始化url
  190. * @param type $param
  191. * @return string
  192. */
  193. private function geturi($param) {
  194. $url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], "?") ? "" : "?") . $param;
  195. $parse = parse_url($url);
  196. if (isset($parse["query"])) {
  197. parse_str($parse["query"], $params);
  198. unset($params["page"]);
  199. $url = $parse["path"] . "?" . http_build_query($params);
  200. return $url;
  201. } else {
  202. return $url;
  203. }
  204. }
  205. /**
  206. * 本页开始条数
  207. * @return int
  208. */
  209. private function star() {
  210. if ($this->total == 0) {
  211. return 0;
  212. } else {
  213. return ($this->page - 1) * $this->pageSize + 1;
  214. }
  215. }
  216. /**
  217. * 本页结束条数
  218. * @return type
  219. */
  220. private function end() {
  221. return min($this->page * $this->pageSize, $this->total);
  222. }
  223. /**
  224. * 设置当前页大小
  225. * @return type
  226. */
  227. private function setPageSize() {
  228. return $this->end() - $this->star() + 1;
  229. }
  230. /**
  231. * 首页
  232. * @return type
  233. */
  234. private function frist() {
  235. $html = '';
  236. if ($this->page == 1) {
  237. $html .= $this->replace("{$this->uri}&page=1", $this->config['frist'], true);
  238. } else {
  239. $html .= $this->replace("{$this->uri}&page=1", $this->config['frist'], false);
  240. }
  241. return $html;
  242. }
  243. /**
  244. * 上一页
  245. * @return type
  246. */
  247. private function prev() {
  248. $html = '';
  249. if ($this->page > 1) {
  250. $html .= $this->replace($this->uri.'&page='.($this->page - 1), $this->config['pre'], false);
  251. } else {
  252. $html .= $this->replace($this->uri.'&page='.($this->page - 1), $this->config['pre'], true);
  253. }
  254. return $html;
  255. }
  256. /**
  257. * 分页数字列表
  258. * @return type
  259. */
  260. private function pagelist() {
  261. $linkpage = "";
  262. $lastlist = floor($this->listnum / 2);
  263. for ($i = $lastlist; $i >= 1; $i--) {
  264. $page = $this->page - $i;
  265. if ($page >= 1) {
  266. $linkpage .= $this->replace("{$this->uri}&page={$page}", $page, false);
  267. } else {
  268. continue;
  269. }
  270. }
  271. $linkpage .= $this->replace("{$this->uri}&page={$this->page}", $this->page, true);
  272. for ($i = 1; $i <= $lastlist; $i++) {
  273. $page = $this->page + $i;
  274. if ($page <= $this->pageNum) {
  275. $linkpage .= $this->replace("{$this->uri}&page={$page}", $page, false);
  276. } else {
  277. break;
  278. }
  279. }
  280. return $linkpage;
  281. }
  282. /**
  283. * 下一页
  284. * @return type
  285. */
  286. private function next() {
  287. $html = '';
  288. if ($this->page < $this->pageNum) {
  289. $html .= $this->replace($this->uri.'&page='.($this->page + 1), $this->config['next'], false);
  290. } else {
  291. $html .= $this->replace($this->uri.'&page='.($this->page + 1), $this->config['next'], true);
  292. }
  293. return $html;
  294. }
  295. /**
  296. * 最后一页
  297. * @return type
  298. */
  299. private function last() {
  300. $html = '';
  301. if ($this->page == $this->pageNum) {
  302. $html .= $this->replace($this->uri.'&page='.($this->pageNum), $this->config['last'], true);
  303. } else {
  304. $html .= $this->replace($this->uri.'&page='.($this->pageNum), $this->config['last'], false);
  305. }
  306. return $html;
  307. }
  308. /**
  309. * 跳转按钮
  310. * @return string
  311. */
  312. private function gopage() {
  313. $html = '';
  314. $html.=' <input type="text" value="' . $this->page . '" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>' . $this->pageNum . ')?' . $this->pageNum . ':this.value;location=\'' . $this->uri . '&page=\'+page+\'\'}" style="width:25px;"/><input type="button" onclick="javascript:var page=(this.previousSibling.value>' . $this->pageNum . ')?' . $this->pageNum . ':this.previousSibling.value;location=\'' . $this->uri . '&page=\'+page+\'\'" value="GO"/>';
  315. return $html;
  316. }
  317. /**
  318. * 模板替换
  319. * @param type $replace 替换内容
  320. * @param type $result 条件
  321. * @return type
  322. */
  323. private function replace($url, $text, $result = true) {
  324. $template = ($result ? $this->activeTemplate : $this->notActiveTemplate);
  325. $html = str_replace('{url}', $url, $template);
  326. $html = str_replace('{text}', $text, $html);
  327. return $html;
  328. }
  329. }

第二款php分页类

  1. <?php
  2. /*
  3. *本程序文件对分页程序进行了封装
  4. *
  5. */
  6. class Page_Link
  7. {
  8. var $page_max = 10; //一组页码的最大数
  9. var $page_num = 10; //总页数
  10. var $length = 20; //一页的数据条数
  11. var $isNextPage = true;
  12. var $isFirstPage = false;
  13. function Calculation_Page_Num( $total )
  14. {
  15. $this->page_num = ceil( $total / $this->length );
  16. return $this->page_num;
  17. }
  18. function Calculation_Min_Max( $act_page = 1 )
  19. {
  20. // 定义左右偏移量
  21. $py_left = 0;
  22. $py_right = 0;
  23. // 定义左右边界
  24. $bj_left = 0;
  25. $bj_right = 0;
  26. // 定义滚动区间边界
  27. $gd_left = 0;
  28. $gd_right = 0;
  29. // 判断是否需要分组
  30. if ( ( $this->page_num - $this->page_max ) <= 0 )
  31. {
  32. // 不需要分组
  33. $bj_left = 1;
  34. $bj_right = $this->page_num;
  35. }
  36. else
  37. {
  38. // 要进行分组
  39. // 判断容量的奇偶
  40. $tmp = $this->page_max % 2;
  41. if ( $tmp === 1 )
  42. {
  43. // 奇数
  44. $py_left = $py_right = ( $this->page_max - 1 ) / 2;
  45. }
  46. else
  47. {
  48. // 偶数
  49. $py_left = $this->page_max / 2 - 1;
  50. $py_right = $this->page_max / 2;
  51. }
  52. // 计算滚动区间
  53. $gd_left = 1 + $py_left;
  54. $gd_right = $this->page_num - $py_right;
  55. // 判断当前页是否落入了滚动区间
  56. if ( $act_page >= $gd_left && $act_page <= $gd_right )
  57. {
  58. // 区间内
  59. $bj_left = $act_page - $py_left;
  60. $bj_right = $act_page + $py_right;
  61. }
  62. else
  63. {
  64. // 区间外
  65. if ( ( $act_page - $py_left ) <= 1 )
  66. {
  67. // 左侧固定区间
  68. $bj_left = 1;
  69. $bj_right = $this->page_max;
  70. }
  71. else
  72. {
  73. $bj_left = $this->page_num - $this->page_max + 1;
  74. $bj_right = $this->page_num;
  75. }
  76. }
  77. }
  78. $res = array();
  79. $res['min'] = $bj_left;
  80. $res['max'] = $bj_right;
  81. return $res;
  82. }
  83. // 主方法
  84. function make_page( $total, $act_page, $url, $param )
  85. {
  86. $page_num = $this->Calculation_Page_Num( $total );
  87. $arr_min_max = $this->Calculation_Min_Max( $act_page );
  88. if (!eregi("([?|&]$param=)", $url)) {
  89. $url = strpos($url,"?")===false?$url."?":$url."&";
  90. $url = $url."$param=0";
  91. }
  92. if ( $act_page > $page_num )
  93. {
  94. $act_page = $page_num;
  95. }
  96. // 用正则把url改成正规的
  97. $url = eregi_replace( $param . '=[0-9]+', $param . '=0', $url );
  98. $res = array();
  99. $d = 0;
  100. for( $i = $arr_min_max['min'];$i <= $arr_min_max['max'];$i++ )
  101. {
  102. if ( $i == $act_page )
  103. {
  104. $res[$d]['url'] = '';
  105. $res[$d]['name'] = $i;
  106. $res[$d]['no'] = $i;
  107. }
  108. else
  109. {
  110. $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url );
  111. $res[$d]['name'] = $i;
  112. $res[$d]['no'] = $i;
  113. }
  114. $d++;
  115. }
  116. if ( $this->isNextPage )
  117. {
  118. $res = $this->make_before_next_link( $res, $act_page, $url, $param );
  119. }
  120. if ( $this->isFirstPage )
  121. {
  122. $res = $this->make_first_end_link( $res, $act_page, $url, $param );
  123. }
  124. return $res;
  125. }
  126. //// 带总页数
  127. function make_page_with_total( $total, $act_page, $url, $param )
  128. {
  129. $page_num = $this->Calculation_Page_Num( $total );
  130. $arr_min_max = $this->Calculation_Min_Max( $act_page );
  131. if (!eregi("([?|&]$param=)", $url)) {
  132. $url = strpos($url,"?")===false?$url."?":$url."&";
  133. $url = $url."$param=0";
  134. }
  135. if ( $act_page > $page_num )
  136. {
  137. $act_page = $page_num;
  138. }
  139. // 用正则把url改成正规的
  140. $url = eregi_replace( $param . '=[0-9]+', $param . '=0', $url );
  141. $res = array();
  142. $d = 0;
  143. for( $i = $arr_min_max['min'];$i <= $arr_min_max['max'];$i++ )
  144. {
  145. if ( $i == $act_page )
  146. {
  147. $res[$d]['url'] = '';
  148. $res[$d]['name'] = $i;
  149. $res[$d]['no'] = $i;
  150. }
  151. else
  152. {
  153. $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url );
  154. $res[$d]['name'] = $i;
  155. $res[$d]['no'] = $i;
  156. }
  157. $d++;
  158. }
  159. if ( $this->isNextPage )
  160. {
  161. $res = $this->make_before_next_link( $res, $act_page, $url, $param );
  162. }
  163. if ( $this->isFirstPage )
  164. {
  165. $res = $this->make_first_end_link( $res, $act_page, $url, $param );
  166. }
  167. $total_num= ceil($total/$this->length);
  168. $result['total']=$total_num;
  169. $result['DATA']=$res;
  170. return $result;
  171. }
  172. // 附加上一页和下一页
  173. function make_before_next_link( $arr, $act, $url, $param )
  174. {
  175. $tmp = array();
  176. $before = $act - 1;
  177. $next = $act + 1;
  178. if ( $before < 1 )
  179. {
  180. $before = 1;
  181. $tmp[0]['url'] = '';
  182. $tmp[0]['name'] = "上一页";
  183. $tmp[0]['no'] = $before;
  184. }
  185. else
  186. {
  187. $tmp[0]['url'] = str_replace( $param . '=0', $param . '=' . $before, $url );
  188. $tmp[0]['name'] = "上一页";
  189. $tmp[0]['no'] = $before;
  190. }
  191. $counts = sizeof( $arr );
  192. $tmp_count = sizeof( $tmp );
  193. for( $i = 0;$i < $counts;$i++ )
  194. {
  195. $tmp[$tmp_count]['url'] = $arr[$i]['url'];
  196. $tmp[$tmp_count]['name'] = $arr[$i]['name'];
  197. $tmp[$tmp_count]['no'] = $arr[$i]['no'];
  198. $tmp_count++;
  199. }
  200. if ( $next > $this->page_num )
  201. {
  202. $next = $this->page_num;
  203. $tmp[$tmp_count]['url'] = '';
  204. $tmp[$tmp_count]['name'] = "下一页";
  205. $tmp[$tmp_count]['no'] = $next;
  206. }
  207. else
  208. {
  209. $tmp[$tmp_count]['url'] = str_replace( $param . '=0', $param . '=' . $next, $url );
  210. $tmp[$tmp_count]['name'] = "下一页";
  211. $tmp[$tmp_count]['no'] = $next;
  212. }
  213. return $tmp;
  214. }
  215. // 附加首页和尾页
  216. function make_first_end_link( $arr, $act, $url, $param )
  217. {
  218. $tmp = array();
  219. $before = 1;
  220. $next = $this->page_num;
  221. if ( $act == 1 )
  222. {
  223. $before = 1;
  224. $tmp[0]['url'] = '';
  225. $tmp[0]['name'] = "首页";
  226. $tmp[0]['no'] = $before;
  227. }
  228. else
  229. {
  230. $tmp[0]['url'] = str_replace( $param . '=0', $param . '=' . $before, $url );
  231. $tmp[0]['name'] = "首页";
  232. $tmp[0]['no'] = $before;
  233. }
  234. $counts = sizeof( $arr );
  235. $tmp_count = sizeof( $tmp );
  236. for( $i = 0;$i < $counts;$i++ )
  237. {
  238. $tmp[$tmp_count]['url'] = $arr[$i]['url'];
  239. $tmp[$tmp_count]['name'] = $arr[$i]['name'];
  240. $tmp[$tmp_count]['no'] = $arr[$i]['no'];
  241. $tmp_count++;
  242. }
  243. if ( $act == $this->page_num )
  244. {
  245. $tmp[$tmp_count]['url'] = '';
  246. $tmp[$tmp_count]['name'] = "尾页";
  247. $tmp[$tmp_count]['no'] = $next;
  248. }
  249. else
  250. {
  251. $tmp[$tmp_count]['url'] = str_replace( $param . '=0', $param . '=' . $next, $url );
  252. $tmp[$tmp_count]['name'] = "尾页";
  253. $tmp[$tmp_count]['no'] = $next;
  254. }
  255. return $tmp;
  256. }
  257. ?>

以上就是一款万能的php分页类实例代码的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行