博客列表 >PHP分页查询

PHP分页查询

Yang_Sir
Yang_Sir原创
2020年05月15日 14:43:40953浏览
  • 分页查询的原理就是利用查询条件LIMIT进行返回数据的限定
  • 纯用PHP处理页面信息并不方便
  • PHP通过get或post获取的数字实际是字符串格式的,用于比较时要注意

1.分页查询类

  • 创建一个Paging分页查询类,该类基础Db数据库查询类
  • 当进行分页查询时,自动生成页码HTML代码
  1. <?php
  2. // session_start();
  3. include 'Db.php';
  4. //创建分页查询类,继承Db类
  5. class Paging extends Db
  6. {
  7. public static $sql='';
  8. public static $current_page = 1;//当前页
  9. public static $total = 0;//总数
  10. public static $pagecount = 0;//总页数
  11. public static $num = 10;//每页数量
  12. public static $displaypage = 5;//页码显示数量
  13. public static $pageHtml = '';//保存分页
  14. /**
  15. * 按页查询的时候调用
  16. */
  17. public function paginate($table,$where='',$current_page=1,$pagecount='',$field='*',$order=''){
  18. self::$current_page = $current_page;
  19. //当页码数为空时,认为没有初始化,需要查询总数
  20. if($pagecount===''){
  21. self::setPagecount($table,$where);
  22. }else{
  23. self::$pagecount = $pagecount;
  24. }
  25. self::createSqlStr($table,$field,$where,$order);
  26. self::createPage(self::$pagecount,$current_page);
  27. return parent::find(self::$sql);
  28. }
  29. /**
  30. * 生成分页HTML代码
  31. */
  32. public static function createPage($pagecount,$current_page=1,$custom_style=''){
  33. if($pagecount<1){
  34. return self::$pageHtml='<div class="pagecontainer"><small>没有数据</small></div>';
  35. }
  36. $style = "<style>
  37. .pagecontainer{
  38. width;80%;
  39. display: flex;
  40. flex-flow: row nowrap;
  41. justify-content: center;
  42. padding: 10px;
  43. }
  44. .pagecontainer button{
  45. background-color: white;
  46. border: 1px solid #4CAF50;
  47. margin:0 10px;
  48. padding:8px 15px;
  49. color:red;
  50. border-radius: 5px;
  51. font-size:14px;
  52. }
  53. .pagecontainer input{
  54. font-size:18px;
  55. width:50px;
  56. height:30px;
  57. }
  58. #act{
  59. border: 1px solid #8080ff;
  60. background-color: #80ffff;
  61. }
  62. .pagecontainer button:hover {background-color: #e7e7e7;}
  63. </style>
  64. /*直接跳转*/
  65. <script>
  66. function getpage(){
  67. var page = document.getElementById('jpage').value;
  68. var p = '?p='+page;
  69. window.location.href=p;
  70. }
  71. </script>
  72. ";
  73. //用户自定义样式
  74. if($custom_style!==''){
  75. $style= $custom_style;
  76. }
  77. //上下页码
  78. $pre = $current_page>1?$current_page-1:1;
  79. $next = $current_page<$pagecount?$current_page+1:$pagecount;
  80. $a=$style."<div class='pagecontainer'><a href='?p={$pre}'><button value='pgup'>上一页</button></a>";
  81. //判断总页数是否少于可展示页码数,如果是:输出全部页码
  82. if($pagecount<=self::$displaypage){
  83. for($i=1;$i<=$pagecount;$i++){
  84. if($i ==$current_page){
  85. $a.="<a href='?p=$i'><button id='act'>$i</button></a>";
  86. continue;
  87. }
  88. $a.="<a href='?p=$i'><button>$i</button></a>";
  89. }
  90. }else{//如果不是
  91. //第一页必输出,当前页单独设置样式
  92. if(1==$current_page){
  93. $a.="<a href='?p=1'><button id='act'>1</button></a>";
  94. }else{
  95. $a.="<a href='?p=1'><button>1</button></a>";
  96. }
  97. //左右偏移量
  98. $offset = intval(self::$displaypage/2);
  99. /**当前页码之前的页码输出 */
  100. //如果当前页大于偏移量+2(第一页和他自己),输出...、输出当前页之前的偏移量页码数
  101. if($current_page>$offset+2){
  102. $a.="<a><button>...</button></a>";
  103. $i = $offset;
  104. while($i){
  105. $page = $current_page-$i;
  106. $a.="<a href='?p={$page}'><button>$page</button></a>";
  107. $i--;
  108. }
  109. $a.="<a href='?p={$current_page}'><button id='act'>$current_page</button></a>";
  110. }else{
  111. //如当前页没有大于偏移量+2,输出第二页到当前页的页码
  112. $i = 2;
  113. while($i<=$current_page){
  114. if($i ==$current_page){
  115. $a.="<a href='?p={$i}'><button id='act'>$i</button></a>";
  116. $i++;
  117. continue;
  118. }
  119. $a.="<a href='?p={$i}'><button >$i</button></a>";
  120. $i++;
  121. }
  122. }
  123. /**当前页码之后的页码输出 */
  124. //如果当前页码加上偏移量+1(尾页总显示)小于总页数,//输出当前页后偏移量数目的页码,然后输出...和尾页
  125. if(($current_page+$offset+1)<$pagecount){
  126. $i = 1;
  127. while($i<=$offset){
  128. $page = $current_page+$i;
  129. $a.="<a href='?p={$page}'><button>$page</button></a>";
  130. $i++;
  131. }
  132. $a.="<a><button>...</button></a>";
  133. //输出尾页
  134. $a.="<a href='?p={$pagecount}'><button>$pagecount</button></a>";
  135. }else{
  136. //如果不是,则输出当前页至倒数第二页的页码
  137. $i = $pagecount-$current_page;
  138. //echo $pagecount-$current_page;
  139. // echo $i;//exit;
  140. while($i>0){
  141. $current_page++;
  142. $a.="<a href='?p={$current_page}'><button>$current_page</button></a>";
  143. $i--;
  144. }
  145. }
  146. }
  147. $a.="<a href='?p={$next}'><button>下一页</button></a>";
  148. $a.="<aa><input type='text' id='jpage''/></aa> <a><button onclick='getpage()'>跳转</button></a></div>";
  149. self::$pageHtml = $a;
  150. return $a;
  151. }
  152. /**
  153. * 设置每页数量
  154. */
  155. public static function setNum(int $num){
  156. self::$num = $num;
  157. }
  158. /**
  159. * 设置最多显示的页码
  160. */
  161. public static function setDisplaypage(int $num){
  162. self::$num = $num;
  163. }
  164. /**
  165. * 初始化页面,当前页为1的时候调用一次
  166. */
  167. public static function setPagecount($table,$where=''){
  168. $sql = "select count(*) as count from `{$table}` {$where}";
  169. $count = parent::find($sql);
  170. self::$total = $count[0]['count'];
  171. self::$pagecount = ceil(self::$total/self::$num);
  172. // return self::$pagecount;
  173. }
  174. /**
  175. * 拼接sql查询语句
  176. */
  177. public function createSqlStr($table,$field='*',$where='',$order=''){
  178. $limit = '';
  179. $offset = (self::$current_page-1)*self::$num;
  180. if(self::$pagecount!==1){
  181. $limit = " LIMIT ".self::$num." OFFSET ".$offset;
  182. }
  183. self::$sql = "select {$field} from `{$table}` {$where} {$limit} {$order}";
  184. return self::$sql;
  185. }
  186. }

生成的页码样式:

2.分页查询示例

  • 现有一张商品信息表,80余条数据
  • 有按条件查询功能

2.1获取数据

  • 根据当前url查询字符串中的p属性,获取当前页码,默认为1
  • 启用session保存查询条件,使分页查询时查询条件不丢失,以及保存页码数可以不用每次都查询总数
  1. <?php
  2. require('Paging.php');
  3. include 'config.php';
  4. session_start();
  5. $paging = new Paging($dsn,$username,$password);
  6. $where = '';
  7. //判断是否提交了条件查询,删除原查询的信息
  8. if(isset($_POST['goodsname'])){
  9. unset($_SESSION['pagecount']);
  10. unset( $_SESSION['goodsname']);
  11. unset( $_SESSION['goodsmodel']);
  12. }
  13. //如果有提交条件查询,保存查询信息,用于分页查询时的where条件
  14. if(isset($_POST['goodsname'])&&$_POST['goodsname']!='') {
  15. $_SESSION['goodsname']=$_POST['goodsname'];
  16. }
  17. if(isset($_POST['goodsmodel'])&&$_POST['goodsmodel']!=''){
  18. $_SESSION['goodsmodel']=$_POST['goodsmodel'];
  19. }
  20. //根据查询条件设置where语句
  21. if(isset($_SESSION['goodsname'])&&isset($_SESSION['goodsmodel'])){
  22. $where = " where `name`='{$_SESSION['goodsname']}' and `model`='{$_SESSION['goodsmodel']}'";
  23. }elseif(isset($_SESSION['goodsname'])){
  24. $where = " where `name`='{$_SESSION['goodsname']}'";
  25. }elseif(isset($_SESSION['goodsmodel'])){
  26. $where = " where `model`='{$_SESSION['goodsmodel']}'";
  27. }
  28. //保存总页数,可以不用每次都去统计总数
  29. if(!isset($_SESSION['pagecount'])){
  30. $data = $paging->paginate($table,$where);
  31. $page = $paging::$pageHtml;//调用页码
  32. $_SESSION['pagecount'] = $paging::$pagecount;
  33. }else{
  34. $p = 1;//当前页
  35. if(isset($_GET['p'])){
  36. $p = $_GET['p'];
  37. }
  38. $pagecount = $_SESSION['pagecount'];//获取总页数
  39. $data = $paging->paginate($table,$where,$p,$pagecount);
  40. $_SESSION['pagecount'] = $paging::$pagecount;//更新session
  41. $page = $paging::$pageHtml;//调用页码
  42. }

2.2 展示数据

  • 循环输出查询结果中的数据到表格中
  • 输出生成的页码
  1. <?php require('data.php');?>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>商品信息维护</title>
  8. <style>
  9. body {
  10. display: flex;
  11. flex-flow: column nowrap;
  12. align-items: center;
  13. }
  14. form {
  15. display: flex;
  16. flex-flow: row wrap;
  17. }
  18. form>section {
  19. margin: 10px;
  20. display: flex;
  21. flex-flow: row nowrap;
  22. }
  23. table {
  24. margin-top: 30px;
  25. width: 1000px;
  26. font-family: verdana, arial, sans-serif;
  27. font-size: 11px;
  28. color: #333333;
  29. border-width: 1px;
  30. border-color: #666666;
  31. border-collapse: collapse;
  32. }
  33. table>thead {
  34. background-color: #80ff80;
  35. }
  36. table th {
  37. border-width: 1px;
  38. padding: 8px;
  39. border-style: solid;
  40. border-color: #666666;
  41. }
  42. table td {
  43. border-width: 1px;
  44. padding: 8px;
  45. border-style: solid;
  46. border-color: #666666;
  47. background-color: #ffffff;
  48. text-align: center;
  49. }
  50. tfoot>tr,
  51. tfoot>tr>td {
  52. width: initial;
  53. }
  54. </style>
  55. </head>
  56. <body>
  57. <!-- 进行条件查询时要清除页码信息 -->
  58. <form action="<?php echo $_SERVER['PHP_SELF'] ?>" class="queryterms" method="POST">
  59. <section>
  60. <label for="goodsname">商品名称:</label>
  61. <input type="text" name="goodsname" id="goodsname" value="<?php if(isset($_SESSION['goodsname'])) echo $_SESSION['goodsname']?>">
  62. </section>
  63. <section>
  64. <label for="goodsmodel">商品型号:</label>
  65. <input type="text" name="goodsmodel" id="goodsmodel" value="<?php if(isset($_SESSION['goodsmodel'])) echo $_SESSION['goodsmodel']?>">
  66. </section>
  67. <section>
  68. <button>查询</button>
  69. </section>
  70. </form>
  71. <div>
  72. <table>
  73. <thead>
  74. <tr>
  75. <th>ID</th>
  76. <th>名称</th>
  77. <th>型号</th>
  78. <th>价格</th>
  79. <th>数量</th>
  80. <th>状态</th>
  81. <th>操作</th>
  82. </tr>
  83. </thead>
  84. <tbody>
  85. <?php foreach($data as $val):?>
  86. <tr>
  87. <td><?php echo $val['id'] ?></td>
  88. <td><?php echo $val['name']?></td>
  89. <td><?php echo $val['model']?></td>
  90. <td><?php echo $val['price']?></td>
  91. <td><?php echo $val['number']?></td>
  92. <td><?php echo $val['status']?></td>
  93. <td><a href="handle.php?act=edit&id=<?php echo $val['id'] ?>">编辑</a>
  94. <a href="handle.php?act=delete&id=<?php echo $val['id'] ?>">删除</a></td>
  95. </tr>
  96. <?php endforeach; ?>
  97. </tbody>
  98. <tfoot>
  99. <tr>
  100. <td colspan="7"><?php echo $page; ?></td>
  101. </tr>
  102. </tfoot>
  103. </table>
  104. </div>
  105. </body>
  106. </html>

效果图,初始页面:

效果图,分页查询:

效果图,有条件查询:

效果图,跳转查询:

3.扩展,页面操作更新和删除数据

1 . 数据处理页,handle.php

  • 根据url中的查询字符串判断是什么类型的操作,分发处理

    1. <?php
    2. include 'config.php';//数据库参数
    3. include 'Db.php';
    4. $db = new Db($dsn,$username,$password);
    5. $act = $_GET['act'];
    6. $where =" where `id`= '{$_GET['id']}' ";
    7. switch($act)
    8. {
    9. case 'delete':
    10. $row = $db::delete($table,$where);
    11. echo $db::$sql;var_dump($row);
    12. if($row==1) echo '<script>alert("删除成功");location.href="goods.php";</script>';
    13. break;
    14. case 'update':
    15. if(isset($_POST['id'])){
    16. $data = $_POST;
    17. $where =" where `id`= '{$_POST['id']}' ";
    18. $row = $db::update($table,$where,$data);
    19. }
    20. if($row==1){
    21. echo '<script>alert("更新成功");location.href="goods.php";</script>';
    22. }else{
    23. echo '<script>alert("更新失败");location.href="goods.php";</script>';
    24. }
    25. break;
    26. case 'edit':
    27. $sql = "select * from `{$table}` {$where}";
    28. $edit_data = $db::find($sql)[0];
    29. if(!empty($edit_data)){include'edit.php';unset($_SESSION['pagecount']);}else{
    30. echo '<script>alert("获取数据失败");location.href="goods.php";</script>';
    31. }
    32. break;
    33. case 'add':
    34. break;
    35. }

    2 . 编辑信息页,edit.php

  • 根据隐藏的input中id的值获取数据,显示到页面中。
  • 提交修改后的信息进行更新
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=xiug, initial-scale=1.0">
  6. <title>修改商品信息</title>
  7. <style>
  8. body {
  9. margin: 0;
  10. text-align: center;
  11. display: flex;
  12. flex-flow: column nowrap;
  13. align-items: center;
  14. }
  15. form {
  16. background-color: #80ffff;
  17. border: 1px solid #c0c0c0;
  18. width: 600px;
  19. display: flex;
  20. flex-flow: column nowrap;
  21. align-items: center;
  22. padding: 30px;
  23. }
  24. form>section {
  25. width: 80%;
  26. margin: 30px 0;
  27. display: grid;
  28. grid-template-columns: 200px 300px;
  29. font-size: 1.2em;
  30. }
  31. form>section input {
  32. font-size: 1.2em;
  33. }
  34. button {
  35. background-color: #0080c0;
  36. width: 200px;
  37. margin-top: 30px;
  38. padding: 10px;
  39. border-radius: 5px;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <?php
  45. // print_r($edit_data);
  46. ?>
  47. <h1>修改商品信息</h1>
  48. <form action="handle.php?act=update" method="POST">
  49. <input type="hidden" name="id" value="<?php echo $edit_data['id'] ?>">
  50. <section>
  51. <label for="name">商品名称:</label>
  52. <input type="text" name="name" id="name" value="<?php echo $edit_data['name']?>">
  53. </section>
  54. <section>
  55. <label for="model">商品型号:</label>
  56. <input type="text" name="model" id="model" value="<?php echo $edit_data['model'] ?>">
  57. </section>
  58. <section>
  59. <label for="price">价格:</label>
  60. <input type="text" name="price" id="price" value="<?php echo $edit_data['price'] ?>">
  61. </section>
  62. <section>
  63. <label for="number">数量:</label>
  64. <input type="text" name="number" id="number" value="<?php echo $edit_data['number'] ?>">
  65. </section>
  66. <section>
  67. <label for="status">状态:</label>
  68. <input type="text" name="status" id="status" value="<?php echo $edit_data['status'] ?>">
  69. </section>
  70. <div class='button'>
  71. <button type="submit">提交</button>
  72. </div>
  73. </form>
  74. </body>
  75. </html>

效果图,编辑页面:

效果图,删除:

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议