系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 网络编程 > PHP编程 > 详细页面

php封装的page分页类完整实例代码

时间:2020-02-03来源:系统城作者:电脑系统城

效果图

1.测试实例test.php


 
  1. <?php
  2. header("Content-Type: text/html; charset=utf-8");
  3. date_default_timezone_set("Asia/Shanghai"); //时区
  4.  
  5. require_once('page.class.php');
  6.  
  7. $showrow = 5;
  8. $curpage = empty($_GET['page']) ? 1 : $_GET['page'];
  9. $url = "?page={page}";
  10. $dsn = 'mysql:host=xxx.xxx.80.xxx;dbname=admin';
  11. $pdo = new PDO($dsn, 'root', 'root');
  12. $pdo->query('set names utf8');
  13. $sql = "SELECT * from operator_list where 1=1";
  14.  
  15. $res_gg = $pdo->query("SELECT count(*) as ctn from operator_list where 1=1;");
  16. $result = $res_gg->fetch();
  17.  
  18. $total = $result["ctn"];
  19.  
  20. if (!empty($_GET['page']) && $total != 0 && $curpage > ceil($total / $showrow)) {
  21. $curpage = ceil($total_rows / $showrow);
  22. }
  23. $sql .= " LIMIT " . ($curpage - 1) * $showrow . ",$showrow;";
  24.  
  25. $res_zz = $pdo->query($sql);
  26. $result = $res_zz->fetchAll();
  27.  
  28. //print_r(json_encode($result));die;
  29.  
  30. ?>
  31.  
  32. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  33. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  34. <html xmlns="http://www.w3.org/1999/xhtml">
  35. <head>
  36. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  37. <title></title>
  38. <meta name="keywords" content="入库"/>
  39. <meta name="description" content="入库"/>
  40. <script type="text/javascript" src="static/js/jquery-1.11.0.min.js?v=1"></script>
  41. <link rel="stylesheet" type="text/css" href="static/css/common.css" rel="external nofollow"/>
  42. </head>
  43. <body>
  44. <div class="head">
  45. <!-- <div class="head_inner clearfix">-->
  46. <!-- <ul id="nav">-->
  47. <!-- <li><a href="javascript;" rel="external nofollow" rel="external nofollow" >商品列表</a></li>-->
  48. <!-- <li><a href="javascript;" rel="external nofollow" rel="external nofollow" >详情列表</a></li>-->
  49. <!-- </ul>-->
  50. <!-- <a class="logo" href="javascript" rel="external nofollow" >
  51. <img src="javascript;" alt="公司logo" /></a> -->
  52. <!-- </div>-->
  53. </div>
  54. <div class="container">
  55. <div class="demo">
  56. <h2 class="title">报表</h2>
  57.  
  58. <div class="showData">
  59. <table width="100%" border="0" align="center"
  60. style="border:1px solid #ccc;" cellpadding="0" cellspacing="1">
  61. <tr align="center">
  62. <td>ID</td>
  63. <td>商品编号</td>
  64. <td>订阅状态</td>
  65. <td>商品状态</td>
  66. <td>修改时间</td>
  67. <td>创建时间</td>
  68. </tr>
  69. <?php
  70. if (!empty($result)) {
  71. foreach ($result as $k => $v) {
  72. ?>
  73. <tr align="center">
  74. <td><?php echo $v['id']; ?></td>
  75. <td><?php echo $v["customer_id"]; ?></td>
  76. <td><?php echo $v["name"]; ?></td>
  77. <td><?php echo $v["role_id"]; ?></td>
  78. <td><?php echo $v["status"]; ?></td>
  79. <td><?php echo $v["cdate"]; ?></td>
  80. </tr>
  81. <?php
  82. }
  83. }
  84. ?>
  85. </table>
  86. </div>
  87. <div class="showPage">
  88. <?php
  89. if ($total > $showrow) {//总记录数大于每页显示数,显示分页
  90. $page = new page($total, $showrow, $curpage, $url, 3);
  91. echo $page->myde_write();
  92. }
  93. ?>
  94. </div>
  95. </div>
  96. </div>
  97. <div class="foot">
  98. 阿里巴巴:<a href="#" rel="external nofollow" target="_blank">https://www.taobao.com</a>
  99. </div>
  100. </body>
  101. </html>

2.封装的page分页类page.class.php


 
  1. <?php
  2.  
  3. /* * *********************************************
  4. * @类名: page
  5. * @参数: $myde_total - 总记录数
  6. * $myde_size - 一页显示的记录数
  7. * $myde_page - 当前页
  8. * $myde_url - 获取当前的url
  9. * @功能: 分页实现
  10. */
  11.  
  12. class page {
  13.  
  14. private $myde_total; //总记录数
  15. private $myde_size; //一页显示的记录数
  16. private $myde_page; //当前页
  17. private $myde_page_count; //总页数
  18. private $myde_i; //起头页数
  19. private $myde_en; //结尾页数
  20. private $myde_url; //获取当前的url
  21. /*
  22. * $show_pages
  23. * 页面显示的格式,显示链接的页数为2*$show_pages+1。
  24. * 如$show_pages=2那么页面上显示就是[首页] [上页] 1 2 3 4 5 [下页] [尾页]
  25. */
  26. private $show_pages;
  27.  
  28. public function __construct($myde_total = 1, $myde_size = 1, $myde_page = 1, $myde_url,$show_pages = 2) {
  29. $this->myde_total = $this->numeric($myde_total);
  30. $this->myde_size = $this->numeric($myde_size);
  31. $this->myde_page = $this->numeric($myde_page);
  32. $this->myde_page_count = ceil($this->myde_total / $this->myde_size);
  33. $this->myde_url = $myde_url;
  34. if ($this->myde_total < 0)
  35. $this->myde_total = 0;
  36. if ($this->myde_page < 1)
  37. $this->myde_page = 1;
  38. if ($this->myde_page_count < 1)
  39. $this->myde_page_count = 1;
  40. if ($this->myde_page > $this->myde_page_count)
  41. $this->myde_page = $this->myde_page_count;
  42. $this->limit = ($this->myde_page - 1) * $this->myde_size;
  43. $this->myde_i = $this->myde_page - $show_pages;
  44. $this->myde_en = $this->myde_page + $show_pages;
  45. if ($this->myde_i < 1) {
  46. $this->myde_en = $this->myde_en + (1 - $this->myde_i);
  47. $this->myde_i = 1;
  48. }
  49. if ($this->myde_en > $this->myde_page_count) {
  50. $this->myde_i = $this->myde_i - ($this->myde_en - $this->myde_page_count);
  51. $this->myde_en = $this->myde_page_count;
  52. }
  53. if ($this->myde_i < 1)
  54. $this->myde_i = 1;
  55. }
  56.  
  57. //检测是否为数字
  58. private function numeric($num) {
  59. if (strlen($num)) {
  60. if (!preg_match("/^[0-9]+$/", $num)) {
  61. $num = 1;
  62. } else {
  63. $num = substr($num, 0, 11);
  64. }
  65. } else {
  66. $num = 1;
  67. }
  68. return $num;
  69. }
  70.  
  71. //地址替换
  72. private function page_replace($page) {
  73. return str_replace("{page}", $page, $this->myde_url);
  74. }
  75.  
  76. //首页
  77. private function myde_home() {
  78. if ($this->myde_page != 1) {
  79. return "<a href='" . $this->page_replace(1) . "' title='首页'>首页</a>";
  80. } else {
  81. return "<p>首页</p>";
  82. }
  83. }
  84.  
  85. //上一页
  86. private function myde_prev() {
  87. if ($this->myde_page != 1) {
  88. return "<a href='" . $this->page_replace($this->myde_page - 1) . "' title='上一页'>上一页</a>";
  89. } else {
  90. return "<p>上一页</p>";
  91. }
  92. }
  93.  
  94. //下一页
  95. private function myde_next() {
  96. if ($this->myde_page != $this->myde_page_count) {
  97. return "<a href='" . $this->page_replace($this->myde_page + 1) . "' title='下一页'>下一页</a>";
  98. } else {
  99. return"<p>下一页</p>";
  100. }
  101. }
  102.  
  103. //尾页
  104. private function myde_last() {
  105. if ($this->myde_page != $this->myde_page_count) {
  106. return "<a href='" . $this->page_replace($this->myde_page_count) . "' title='尾页'>尾页</a>";
  107. } else {
  108. return "<p>尾页</p>";
  109. }
  110. }
  111.  
  112. //输出
  113. public function myde_write($id = 'page') {
  114. $str = "<div id=" . $id . ">";
  115. $str.=$this->myde_home();
  116. $str.=$this->myde_prev();
  117. if ($this->myde_i > 1) {
  118. $str.="<p class='pageEllipsis'>...</p>";
  119. }
  120. for ($i = $this->myde_i; $i <= $this->myde_en; $i++) {
  121. if ($i == $this->myde_page) {
  122. $str.="<a href='" . $this->page_replace($i) . "' title='第" . $i . "页' class='cur'>$i</a>";
  123. } else {
  124. $str.="<a href='" . $this->page_replace($i) . "' title='第" . $i . "页'>$i</a>";
  125. }
  126. }
  127. if ($this->myde_en < $this->myde_page_count) {
  128. $str.="<p class='pageEllipsis'>...</p>";
  129. }
  130. $str.=$this->myde_next();
  131. $str.=$this->myde_last();
  132. $str.="<p class='pageRemark'>共" . $this->myde_page_count .
  133. "页" . $this->myde_total . "条数据</p>";
  134. $str.="</div>";
  135. return $str;
  136. }
  137.  
  138. }
  139.  
  140. ?>

3.css样式


 
  1. html, body, div, span, h1, h2, h3, h4, h5, h6, p, pre,
  2. a, code, em, img, small, strong, sub, sup, u, i, center,
  3. dl, dt, dd, ol, ul, li, fieldset, form, label {
  4. margin: 0;
  5. padding: 0;
  6. border: 0;
  7. outline: 0;
  8. font-size: 100%;
  9. vertical-align: baseline;
  10. background: transparent
  11. }
  12.  
  13. a {
  14. color: #007bc4;
  15. text-decoration: none;
  16. cursor: pointer;
  17. }
  18.  
  19. .table_parameters a:hover {
  20. text-decoration: none
  21. }
  22.  
  23. a:hover {
  24. text-decoration: underline
  25. }
  26.  
  27. ol, ul {
  28. list-style: none
  29. }
  30.  
  31. table {
  32. border-collapse: collapse;
  33. border-spacing: 0
  34. }
  35.  
  36. /*html {*/
  37. /*background: url(../images/demo_bg.png)*/
  38. /*}*/
  39.  
  40. body {
  41. height: 100%;
  42. font: 12px/18px "Microsoft Yahei", Tahoma, Helvetica,
  43. Arial, Verdana, "\5b8b\4f53", sans-serif;
  44. color: #51555c
  45. }
  46.  
  47. img {
  48. border: 0;
  49. cursor: pointer;
  50. }
  51.  
  52. .clearfix:after {
  53. visibility: hidden;
  54. display: block;
  55. font-size: 0;
  56. content: " ";
  57. clear: both;
  58. height: 0
  59. }
  60.  
  61. .head {
  62. /*border-bottom: 1px solid #dadada;*/
  63. padding: 0 0 5px
  64. }
  65.  
  66. .head_inner {
  67. margin: 0 auto;
  68. width: 980px
  69. }
  70.  
  71. .container {
  72. width: 80%;
  73. /*min-height: 600px;*/
  74. margin: 30px auto 0 auto;
  75. border: 1px solid #d3d3d3;
  76. background: #fff;
  77. -moz-border-radius: 5px;
  78. -khtml-border-radius: 5px;
  79. -webkit-border-radius: 5px;
  80. border-radius: 5px
  81. }
  82.  
  83. .demo > h2.title {
  84. margin: 4px 0 30px;
  85. padding: 15px 0 10px 20px;
  86. border-bottom: 1px solid #d3d3d3;
  87. font-size: 18px;
  88. color: #a84c10;
  89. background: url(../images/arrow.jpg) no-repeat 2px 14px
  90. }
  91.  
  92. .foot {
  93. height: 60px;
  94. padding: 10px 2px;
  95. line-height: 24px;
  96. text-align: center
  97. }
  98.  
  99. .foot a:hover {
  100. color: #51555c
  101. }
  102.  
  103. .btn {
  104. -webkit-border-radius: 3px;
  105. -moz-border-radius: 3px;
  106. -ms-border-radius: 3px;
  107. -o-border-radius: 3px;
  108. border-radius: 3px;
  109. background-color: #ff8400;
  110. color: #fff;
  111. display: inline-block;
  112. height: 28px;
  113. line-height: 28px;
  114. text-align: center;
  115. padding: 0 12px;
  116. transition: background-color .2s linear 0s;
  117. border: 0;
  118. cursor: pointer
  119. }
  120.  
  121. .btn:hover {
  122. background-color: #e95a00;
  123. text-decoration: none
  124. }
  125.  
  126. .demo {
  127. width: 700px;
  128. margin: 0 auto
  129. }
  130.  
  131. ul.ul_demo li {
  132. background: url("../images/demo_icon.gif") no-repeat scroll 0 6px;
  133. line-height: 28px;
  134. padding-left: 20px
  135. }
  136.  
  137. .input, .table input[type='text'] {
  138. border: 1px solid #ccc;
  139. padding: 0 5px;
  140. width: 220px;
  141. height: 26px;
  142. line-height: 26px
  143. }
  144.  
  145. #nav {
  146. float: right;
  147. margin: 30px 0 0
  148. }
  149.  
  150. #nav li {
  151. float: left;
  152. font-size: 16px;
  153. margin-right: 20px
  154. }
  155.  
  156. .btn.loading {
  157. opacity: .5
  158. }
  159.  
  160. h3 a.cur {
  161. color: #f30;
  162. }
  163.  
  164. .demo h3 a {
  165. font-size: 14px;
  166. margin: 0 10px 5px 0;
  167. display: inline-block
  168. }
  169.  
  170. .red {
  171. color: red
  172. }
  173.  
  174. .notice {
  175. font-size: 14px;
  176. margin-bottom: 10px;
  177. }
  178.  
  179. .table_parameters {
  180. border-left: 1px solid #d3d3d3;
  181. border-top: 1px solid #d3d3d3;
  182. margin: 6px auto;
  183. font-size: 14px
  184. }
  185.  
  186. .table_parameters tr.tr_head {
  187. background: none repeat scroll 0 0 #f7f7f7;
  188. font-weight: bold;
  189. padding: 6px;
  190. text-align: center
  191. }
  192.  
  193. .table_parameters td, .table_parameters th {
  194. border-bottom: 1px solid #d3d3d3;
  195. border-right: 1px solid #d3d3d3;
  196. line-height: 26px;
  197. padding: 2px
  198. }
  199.  
  200. h1 {
  201. font: 32px "Microsoft Yahei";
  202. margin: 40px auto;
  203. text-align: center;
  204. }
  205.  
  206. h2 {
  207. font-size: 16px;
  208. margin: 10px 0;
  209. }
  210.  
  211. .menu {
  212. height: 30px;
  213. margin-bottom: 30px;
  214. padding: 10px;
  215. background-color: #f0f0f0;
  216. text-align: center;
  217. }
  218.  
  219. .menu a {
  220. display: inline-block;
  221. height: 30px;
  222. padding: 0 20px;
  223. line-height: 30px;
  224. font-size: 14px;
  225. color: #333;
  226. text-decoration: none;
  227. }
  228.  
  229. .menu a:hover {
  230. color: #000;
  231. background-color: #e9e9e9;
  232. }
  233.  
  234. .menu .cur {
  235. background-color: #ddd !important;
  236. color: #000;
  237. }
  238.  
  239. .vad a {
  240. display: inline-block;
  241. height: 36px;
  242. line-height: 36px;
  243. margin: 0 5px;
  244. padding: 0 50px;
  245. font-size: 14px;
  246. text-align: center;
  247. color: #eee;
  248. text-decoration: none;
  249. background-color: #222;
  250. }
  251.  
  252. .vad a:hover {
  253. color: #fff;
  254. background-color: #000;
  255. }
  256.  
  257. .thead {
  258. width: 728px;
  259. height: 90px;
  260. margin: 0 auto;
  261. }
  262.  
  263. textarea {
  264. border: 1px solid #ccc;
  265. font-size: 12px;
  266. height: 100px;
  267. line-height: 18px;
  268. padding: 5px;
  269. width: 300px;
  270. }
  271.  
  272. .table td {
  273. padding: 10px 0
  274. }
  275.  
  276. .disabled {
  277. opacity: .6;
  278. filter: alpha(opacity=60)
  279. }
  280. .demo > p {
  281. line-height: 30px;
  282. font-size: 14px
  283. }
  284.  
  285. .demo > p a {
  286. font-size: 14px
  287. }
  288.  
  289. .demo h3 {
  290. font-size: 16px;
  291. margin: 20px 0
  292. }
  293.  
  294. select {
  295. background-color: #fff;
  296. background-position: right center;
  297. background-repeat: no-repeat;
  298. border: 1px solid #888;
  299. border-radius: 3px;
  300. box-sizing: border-box;
  301. font: 12px/1.5 Tahoma, Arial, sans-serif;
  302. height: 30px;
  303. padding: 0 4px;
  304. }
  305.  
  306. fieldset {
  307. border: 1px solid #ccc;
  308. border-radius: 5px;
  309. margin: 1em 0;
  310. padding: 10px 20px;
  311. }
  312.  
  313. dl.row dt {
  314. width: 2em;
  315. }
  316.  
  317. dl.row dt {
  318. clear: left;
  319. float: left;
  320. line-height: 30px;
  321. padding: 5px;
  322. text-align: right;
  323. width: 6em;
  324. }
  325.  
  326. dl.row dd {
  327. float: left;
  328. padding: 5px;
  329. }
  330.  
  331. .pager {
  332. text-align: right;
  333. }
  334.  
  335. .pager a {
  336. padding: 3px 8px;
  337. margin-left: 3px;
  338. line-height: 20px;
  339. background: #f9f9f9;
  340. border: 1px solid #DBDBDB;
  341. text-decoration: none
  342. }
  343.  
  344. .pager a:hover,
  345. .pager a.current {
  346. background-color: #7CD5B1;
  347. color: #fff;
  348. border: 1px solid #7CD5B1;
  349. cursor: pointer;
  350. }
  351.  
  352. .page {
  353. text-align: center;
  354. margin: 50px 0
  355. }
  356.  
  357. .page a, .page span.prev_disabled {
  358. border: 1px solid #ededed;
  359. color: #3d3d3d;
  360. font-weight: 700;
  361. height: 35px;
  362. line-height: 35px;
  363. margin-left: 5px;
  364. min-width: 9px;
  365. padding: 0 13px;
  366. text-align: center;
  367. text-decoration: none;
  368. vertical-align: top;
  369. font-family: "simsun";
  370. display: inline-block
  371. }
  372.  
  373. .page span.prev_disabled {
  374. cursor: default;
  375. color: #ccc;
  376. margin: 0 10px 0 0
  377. }
  378.  
  379. .page a.current {
  380. background-color: #f40;
  381. border-color: #f40;
  382. color: #fff;
  383. font-weight: 700;
  384. position: relative;
  385. z-index: 1;
  386. }
  387.  
  388. .page .extra {
  389. display: inline-block;
  390. margin-left: 10px;
  391. height: 35px;
  392. line-height: 35px;
  393. color: #656565;
  394. }
  395.  
  396. .page .page-num {
  397. border: 1px solid #ededed;
  398. height: 21px;
  399. text-align: center;
  400. width: 35px;
  401. display: inline-block
  402. }
  403.  
  404. .page .page-submit {
  405. background-clip: padding-box;
  406. border: 1px solid #ededed;
  407. border-radius: 2px;
  408. cursor: pointer;
  409. height: 21px;
  410. line-height: 21px;
  411. text-align: center;
  412. width: 43px;
  413. display: inline-block
  414. }
  415.  
  416. .page .page-submit:hover {
  417. border-color: #f40;
  418. color: #f40;
  419. }
  420.  
  421. .page a:focus, .page a:hover {
  422. border-color: #f40;
  423. z-index: 1;
  424. }
  425.  
  426. .loading {
  427. margin: 30px 0;
  428. text-align: center
  429. }
  430.  
  431. p {
  432. margin: 0
  433. }
  434.  
  435. #page {
  436. height: 40px;
  437. padding: 20px 0px;
  438. }
  439.  
  440. #page a {
  441. display: block;
  442. float: left;
  443. margin-right: 10px;
  444. padding: 2px 12px;
  445. height: 24px;
  446. border: 1px #cccccc solid;
  447. background: #fff;
  448. text-decoration: none;
  449. color: #808080;
  450. font-size: 12px;
  451. line-height: 24px;
  452. }
  453.  
  454. #page a:hover {
  455. color: #077ee3;
  456. border: 1px #077ee3 solid;
  457. }
  458.  
  459. #page a.cur {
  460. border: none;
  461. background: #077ee3;
  462. color: #fff;
  463. }
  464.  
  465. #page p {
  466. float: left;
  467. padding: 2px 12px;
  468. font-size: 12px;
  469. height: 24px;
  470. line-height: 24px;
  471. color: #bbb;
  472. border: 1px #ccc solid;
  473. background: #fcfcfc;
  474. margin-right: 8px;
  475.  
  476. }
  477.  
  478. #page p.pageRemark {
  479. border-style: none;
  480. background: none;
  481. margin-right: 0px;
  482. padding: 4px 0px;
  483. color: #666;
  484. }
  485.  
  486. #page p.pageRemark b {
  487. color: red;
  488. }
  489.  
  490. #page p.pageEllipsis {
  491. border-style: none;
  492. background: none;
  493. padding: 4px 0px;
  494. color: #808080;
  495. }
  496.  
  497. .dates li {
  498. font-size: 14px;
  499. margin: 20px 0
  500. }
  501.  
  502. .dates li span {
  503. text-align: center
  504. }
  505.  
  506. td {
  507. font-size: 15px;
  508. margin: 20px 0
  509. }

具体的大家可以多学习别人的php分页类然后多练就可以了。

分享到:

相关信息

  • Laravel框架自定义分页样式操作示例

    这篇文章主要介绍了Laravel框架自定义分页样式操作,结合实例形式详细分析了laravel框架自定义分页样式的具体操作步骤、实现方法及相关注意事项,需要的朋友可以参考下...

    2020-02-03

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载