Blogger Information
Blog 7
fans 0
comment 1
visits 11582
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
如何用php实现分页效果
Original
858 people have browsed it

在一个网站中,分页是很常见的,可是怎样才能实现分页呢,今天为大家实现一下分页的效果
首先我们写分页前要知道两个很重要的地方,一个是数据库查询方式,一个是分页计算公式
数据库查询方式 select from tableName limit num1,num2; 这是一个限定查询的语句,后面跟两个参数,num1为从第几个开始查找,num2为查找的信息的个数
从查询方式中 num1这个值怎么来呢, 这就用到分页的计算公式了, num1 = (当前页 - 1)
每页显示的条数

实现分页前我们先在数据库里面准备一个表,并且插入数据,这些都是必需的前提工作了 (直接拷贝到数据库运行就行)

  1. CREATE TABLE `test` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
  4. `content` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
  5. PRIMARY KEY (`id`) USING BTREE
  6. ) ENGINE = MyISAM AUTO_INCREMENT = 19 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;
  7. INSERT INTO `test` VALUES (1, '测试1', '睡大觉');
  8. INSERT INTO `test` VALUES (2, '测试1', '睡大觉');
  9. INSERT INTO `test` VALUES (3, '测试1', '睡大觉');
  10. INSERT INTO `test` VALUES (4, '测试1', '睡大觉');
  11. INSERT INTO `test` VALUES (5, '测试1', '睡大觉');
  12. INSERT INTO `test` VALUES (6, '测试1', '睡大觉');
  13. INSERT INTO `test` VALUES (7, 'qwea', '睡大觉');
  14. INSERT INTO `test` VALUES (8, 'zxcz', '睡大觉');
  15. INSERT INTO `test` VALUES (9, 'qwe', '睡大觉');
  16. INSERT INTO `test` VALUES (10, 'bcv', '睡大觉');
  17. INSERT INTO `test` VALUES (11, 'rtyu', '睡大觉');
  18. INSERT INTO `test` VALUES (12, 'xcvx', '睡大觉');
  19. INSERT INTO `test` VALUES (13, '456', '睡大觉');
  20. INSERT INTO `test` VALUES (14, 'eqwe456', '睡大觉');
  21. INSERT INTO `test` VALUES (15, 'asd2', '睡大觉');
  22. INSERT INTO `test` VALUES (16, '123', '睡大觉');
  23. INSERT INTO `test` VALUES (17, 'rcvxtyu', '睡大觉');
  24. INSERT INTO `test` VALUES (18, '2312asda', '睡大觉');
  1. <?php
  2. //创建一个分页数据查询的方法
  3. function getPageList($page,$pageSize){
  4. // 创建连接
  5. $conn = mysqli_connect('127.0.0.1', 'root', 'root', 'page');
  6. $s = ($page - 1) * $pageSize;
  7. $sql = "SELECT * FROM test LIMIT {$s},{$pageSize}";
  8. $sql1 = "SELECT count(*) as count FROM test";
  9. $result = mysqli_query($conn, $sql);
  10. $result1 = mysqli_query($conn, $sql1);
  11. $data['table_count'] = $result1->fetch_assoc()['count'];
  12. $data['pageSize'] = $pageSize;
  13. $data['data'] = [];
  14. if ($result->num_rows > 0) {
  15. while($row = $result->fetch_assoc()) {
  16. $data['data'][] = $row;
  17. }
  18. }
  19. mysqli_close($conn);
  20. return $data;
  21. }
  22. //创建一个处理分页的方法(这里我只实现 首页,上一页,下一页,尾页)
  23. function getPage($data){
  24. //首先我们要先处理参数
  25. $get = $_GET;
  26. $page = isset($get['page'])?$get['page']:1;
  27. //将其他的参数拼接起来,不需要分页页数
  28. unset($get['page']);
  29. $url = http_build_query($get);
  30. $url = $url?$url.'&':'?';
  31. $end_page = ceil($data['table_count'] / $data['pageSize']);
  32. $src = '';
  33. //如果当前页数为1 那第一个跟首页不能点击
  34. if($page == 1){
  35. $src = "<a><span>首页</span></a> ";
  36. $src .= "<a><span>上一个</span></a> ";
  37. }else{
  38. $src .= "<a href='?{$url}page=1'>首页</a> ";
  39. $pages = $page - 1;
  40. $src .= "<a href='?{$url}page={$pages}'>上一个</a> ";
  41. }
  42. //如果当前页是最后一页 那下一个跟尾页不能点击
  43. if($page == $end_page){
  44. $src .= "<a ><span>下一个</span></a> ";
  45. $src .= "<a><span>尾页</span></a> ";
  46. }else{
  47. $pages = $page + 1;
  48. $src .= "<a href='?{$url}page={$pages}'>下一个</a> ";
  49. $src .= "<a href='?{$url}page={$end_page}'>尾页</a> ";
  50. }
  51. return $src;
  52. }
  53. $page = isset($_GET['page'])?$_GET['page']:1;
  54. $list = getPageList($page,5);
  55. $page = getPage($list);
  56. ?>
  57. <!doctype html>
  58. <html lang="en">
  59. <head>
  60. <meta charset="UTF-8">
  61. <meta name="viewport"
  62. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  63. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  64. <title>Document</title>
  65. </head>
  66. <body>
  67. <table>
  68. <tr>
  69. <th>id</th>
  70. <th>name</th>
  71. </tr>
  72. <?php foreach($list['data'] as $v){ ?>
  73. <tr>
  74. <td><?php echo $v['id']; ?></td>
  75. <td><?php echo $v['name']; ?></td>
  76. </tr>
  77. <?php } ?>
  78. </table>
  79. <?php echo $page; ?>
  80. </body>
  81. </html>
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
1 comments
2021-09-23 15:52:10
太感谢博主
1 floor
Author's latest blog post