Blogger Information
Blog 46
fans 2
comment 0
visits 19466
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1. 实现上一页,下一页功能,注意越界
P粉314265155
Original
391 people have browsed it

将数据填充到前端的代码表格中

  1. <?php
  2. namespace _0819;
  3. // 将数据填充到前端的代码表格中
  4. // fetch + while
  5. // fetchALL+ foreach
  6. // bindValue 值绑定到变量上
  7. // bindParam 引用绑定到变量上
  8. // bindColumn 将字段绑定到变量上
  9. // 'SELECT `id`,`name`,`email` FROM `staff`';
  10. use PDO;
  11. $db = new PDO('mysql:dbname=bittel','root','root');
  12. // herdoc
  13. $sql = <<< SQL
  14. SELECT `id`,`name`,`sex`,`email`
  15. FROM `staff`;
  16. SQL;
  17. // sql语句对象预处理
  18. $stmt = $db->prepare($sql);
  19. // 执行sql
  20. $stmt->execute();
  21. // while,foreach, list()进行结果数组的解构到变量中
  22. // 0 1 2 3 替换 bindColumn里面第一个数的值
  23. $stmt->bindColumn('id', $id);
  24. $stmt->bindColumn('name', $name);
  25. $stmt->bindColumn('sex', $sex);
  26. $stmt->bindColumn('email', $email);
  27. // 此种展示太简单 不会用的
  28. // while ($stmt->fetch() ){
  29. // printf('%d:%s,%d,(%s)<br>',$id,$name,$sex,$email);
  30. // }
  31. // 此次以表格方式展示
  32. $table =<<<TABLE
  33. <style>
  34. table {
  35. border-collapse: collapse;
  36. width: 90%;
  37. min-width: 360px;
  38. margin: 30px auto;
  39. text-align:center;
  40. }
  41. table,th,td {
  42. border: 1px solid #000;
  43. padding: 5px;
  44. }
  45. table caption {
  46. font-size: 18px;
  47. margin-bottom: 10px;
  48. }
  49. table thead {
  50. background: lightcyan;
  51. }
  52. table tbody tr:hover {
  53. cursor: pointer;
  54. background: #efefef;
  55. }
  56. </style>
  57. <table>
  58. <caption>员工信息表</caption>
  59. <thead>
  60. <tr>
  61. <th>ID</th>
  62. <th>姓名</th>
  63. <th>性别</th>
  64. <th>邮箱</th>
  65. </tr>
  66. </thead>
  67. <tbody>
  68. TABLE;
  69. // while ($stmt->fetch() ){
  70. // printf('%d:%s,%d,(%s)<br>',$id,$name,$sex,$email);
  71. // }
  72. while ($stmt->fetch(PDO::FETCH_BOUND) ){
  73. // 对性别进行处理 0 、1
  74. $tsex= $sex?'男':'女';
  75. // 拼装 html 代码
  76. $table .= <<<TR
  77. <tr>
  78. <td>$id</td>
  79. <td>$name</td>
  80. <td>$tsex</td>
  81. <td>$email</td>
  82. </tr>
  83. TR;
  84. }
  85. $table .='</tbody></table>';
  86. echo $table;

获取分页数据

  1. <?php
  2. // 获取分页数据
  3. namespace _0819;
  4. use PDO;
  5. // 连接
  6. $db = new PDO('mysql:dbname=bittel','root','root');
  7. // 1、页数
  8. $page = $_GET['p'];
  9. echo '当前页数,p:'.$page.'<br>';
  10. // 2、每页展示的数据 num =5
  11. $num =5 ;
  12. echo '当前数量:num'.$num.'<hr>';
  13. // 3、偏移量 offset = (页数 -1)*数量
  14. $offset =($page - 1) * $num;
  15. // 打印当前偏移量
  16. echo '当前偏移量 offse:'.$offset.'<hr>';
  17. // $sql = <<< SQL
  18. $sql = <<< SQL
  19. SELECT *
  20. FROM `staff`
  21. LIMIT $offset, $num;
  22. SQL;
  23. $stmt = $db ->prepare($sql);
  24. $stmt->execute();
  25. $staffs = $stmt ->fetchAll(PDO::FETCH_ASSOC);
  26. // fetchAll 是二维数组
  27. foreach($staffs as $staff){
  28. // list($id,$name,....) 简化extract() 解构后得到一系类变量
  29. extract($staff) ;
  30. printf('%d-%s-%s-%s<br>', $id, $name, $sex, $email);
  31. }

获取总页数

  1. <?php
  2. // 获取总页数
  3. namespace _0819;
  4. use PDO;
  5. // 连接
  6. $db = new PDO('mysql:dbname=bittel','root','root');
  7. // 1、页数 必须要给一个默认页数值,。不然报错 用两个??赋值
  8. $page = $_GET['p']??1;
  9. echo '当前页数,p:'.$page.'<br>';
  10. // 2、每页展示的数据 num =5
  11. $num =5 ;
  12. echo '当前数量:num'.$num.'<hr>';
  13. // 3、偏移量 offset = (页数 -1)*数量
  14. $offset =($page - 1) * $num;
  15. // 打印当前偏移量
  16. echo '当前偏移量 offset:'.$offset.'<hr>';
  17. // 4、计算总记录数
  18. //方式一: SELECT COUNT('id') FROM `staff`;
  19. // 方式二:SELECT COUNT(*) AS `total` FROM `staff`; 起别名
  20. // 计算页数 一步到位:SELECT CEIL( COUNT(*)/5) AS `total` FROM `staff`;
  21. // 推荐
  22. $sql =' SELECT COUNT(*) AS `total` FROM `staff`;' ;
  23. $stmt = $db ->prepare($sql);
  24. $stmt->execute();
  25. // 将总数量绑定到一个变量上
  26. $stmt ->bindColumn('total',$total);
  27. $stmt->fetch();
  28. echo'当前的总记录数:$total:'.$total.'<hr>';
  29. // 5. 计算总页数
  30. // 向上取整
  31. $pages = ceil($total / $num);
  32. echo '当前总页数: pages = ' . $pages . '<hr>';
  33. $sql = <<< SQL
  34. SELECT *
  35. FROM `staff`
  36. LIMIT $offset, $num;
  37. SQL;
  38. $stmt = $db->prepare($sql);
  39. $stmt->execute();
  40. $staffs = $stmt->fetchAll(PDO::FETCH_ASSOC);
  41. // 在foreach遍历前增加判断是否存在数据
  42. if(count($staffs)===0){
  43. echo '查询结果为空';
  44. }else{
  45. foreach($staffs as $staff){
  46. extract($staff);
  47. printf('%d--%s---%s----%s<br>',$id,$name,$sex,$email);
  48. }
  49. }
  50. function createPages(int $page, int $pages): array
  51. {
  52. // 当前是第8页, 共计20页
  53. // [1, ... 6, 7, 8, 9, 10, .... 20]
  54. // 当前是第10页, 共计20页
  55. // [1, ... 8, 9, 10, 11, 12, .... 20]
  56. // 1. 生成与总页数长度相同的递增的整数数组
  57. $pageArr = range(1, $pages);
  58. // 2. 只需要当前和前后二页, 其它页码用 false/null 来标记
  59. $paginate = array_map(function ($p) use ($page, $pages) {
  60. return ($p == 1 || $p == $pages || abs($page-$p) <=2) ? $p : '...';
  61. }, $pageArr);
  62. // dump($paginate);
  63. // 去重, 替换
  64. $before = array_unique(array_slice($paginate, 0, $page));
  65. $after = array_unique(array_slice($paginate, $page));
  66. // 用解构进行合并
  67. return [...$before, ...$after];
  68. }
  69. echo '<hr>';
  70. $pgnull = createPages($page, $pages);
  71. print_r($pgnull);
  72. // array_map ($pgnull as $pg ){
  73. // if ( $pg===null){
  74. // return '...';
  75. // }else {
  76. // return $pg;
  77. // }
  78. // }

分页展示数据

  1. <?php
  2. // 加载zuoye3.php
  3. require 'zuoye3.php';
  4. echo'<hr>';
  5. ?>
  6. <!DOCTYPE html>
  7. <html lang="zh-CN">
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  12. <title>分页展示数据</title>
  13. <style>
  14. table {
  15. width: 400px;
  16. border-collapse: collapse;
  17. text-align: center;
  18. }
  19. table th,
  20. table td {
  21. border: 1px solid;
  22. padding: 5px;
  23. }
  24. table thead {
  25. background-color: lightcyan;
  26. }
  27. table caption {
  28. font-size: larger;
  29. margin-bottom: 8px;
  30. }
  31. body>p {
  32. display: flex;
  33. }
  34. p>a {
  35. text-decoration: none;
  36. color: #555;
  37. border: 1px solid;
  38. padding: 5px 10px;
  39. margin: 10px 2px;
  40. }
  41. /* 高亮 */
  42. .active {
  43. background-color: seagreen;
  44. color: white;
  45. border: 1px solid seagreen;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <table>
  51. <!-- 标题 -->
  52. <caption>员工信息表</caption>
  53. <!-- 表头 -->
  54. <thead>
  55. <tr>
  56. <td>ID</td>
  57. <td>姓名</td>
  58. <td>性别</td>
  59. <td>邮箱</td>
  60. </tr>
  61. </thead>
  62. <tbody>
  63. <?php
  64. foreach ($staffs as $staff):
  65. extract($staff);
  66. ?>
  67. <tr>
  68. <td><?=$id?></td>
  69. <td><?=$name?></td>
  70. <td><?=$sex?></td>
  71. <td><?=$email?></td>
  72. </tr>
  73. <?php endforeach ?>
  74. </tbody>
  75. </table>
  76. <!-- 分页条
  77. < ?php for(): ?>
  78. < < ?php endfor ? >
  79. -->
  80. <p>
  81. <!-- 页面地址 -->
  82. <!-- <?php echo $_SERVER['PHP_SELF'] . '?p=' . 3; ?> -->
  83. <?php for($i=1;$i<= $pages;$i++): ?>
  84. <!-- / / 页面跳转的 url -->
  85. <?php $url = $_SERVER['PHP_SELF'] . '?p=' . $i;
  86. // 高亮设置 实现页面高亮 注意类型转换 将$_GET['p'] 字符串型改为整数型
  87. // $active = ($i===Intval($_GET['p'])) ? 'active' : null;
  88. // 注意要个 第一页给个默认值 ,值为空的时候给默认值,有值不用
  89. // 目前还未做页码检查 ???? 自行考虑
  90. $page = $_GET['p']??1;
  91. $active = ($i===Intval($page)) ? 'active' : null;
  92. ?>
  93. <!-- 把地址塞到分页条 生成分页条样式 生成分页数 -->
  94. <a href="<?=$url?>" class="<?=$active?>"><?=$i?></a>
  95. <?php endfor ?>
  96. </p>
  97. </body>
  98. </html>

分页展示数据—-上一页、下一页

  1. <?php
  2. // 加载zuoye3.php
  3. require 'zuoye3.php';
  4. echo'<hr>';
  5. ?>
  6. <!DOCTYPE html>
  7. <html lang="zh-CN">
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  12. <title>分页展示数据</title>
  13. <style>
  14. table {
  15. width: 400px;
  16. border-collapse: collapse;
  17. text-align: center;
  18. }
  19. table th,
  20. table td {
  21. border: 1px solid;
  22. padding: 5px;
  23. }
  24. table thead {
  25. background-color: lightcyan;
  26. }
  27. table caption {
  28. font-size: larger;
  29. margin-bottom: 8px;
  30. }
  31. body>p {
  32. display: flex;
  33. }
  34. p>a {
  35. text-decoration: none;
  36. color: #555;
  37. border: 1px solid;
  38. padding: 5px 10px;
  39. margin: 10px 2px;
  40. }
  41. /* 高亮 */
  42. .active {
  43. background-color: seagreen;
  44. color: white;
  45. border: 1px solid seagreen;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <table>
  51. <!-- 标题 -->
  52. <caption>员工信息表</caption>
  53. <!-- 表头 -->
  54. <thead>
  55. <tr>
  56. <td>ID</td>
  57. <td>姓名</td>
  58. <td>性别</td>
  59. <td>邮箱</td>
  60. </tr>
  61. </thead>
  62. <tbody>
  63. <?php
  64. foreach ($staffs as $staff):
  65. extract($staff);
  66. ?>
  67. <tr>
  68. <td><?=$id?></td>
  69. <td><?=$name?></td>
  70. <td><?=$sex?></td>
  71. <td><?=$email?></td>
  72. </tr>
  73. <?php endforeach ?>
  74. </tbody>
  75. </table>
  76. <!-- 分页条
  77. < ?php for(): ?>
  78. < < ?php endfor ? >
  79. -->
  80. <p>
  81. <?php
  82. $uppage = $page-1;
  83. $upurl= $_SERVER['PHP_SELF'] . '?p=' . $uppage;
  84. ?>
  85. <?php if($uppage>0):?>
  86. <a href=<?=$upurl?> >上一页</a>}
  87. <?php endif ?>
  88. <?php foreach($pgnull as $pg): ?>
  89. <?php
  90. if($pg==='...'){
  91. $url='#';
  92. }else {
  93. $url= $_SERVER['PHP_SELF'] . '?p=' . $pg;
  94. }
  95. $page = $_GET['p']??1;
  96. $active = ($pg===Intval($page)) ? 'active' : null;
  97. ?>
  98. <a href="<?=$url?>" class="<?=$active?>"><?=$pg?></a>
  99. <?php endforeach ?>
  100. <?php
  101. $nextpage = $page + 1;
  102. $nexturl= $_SERVER['PHP_SELF'] . '?p=' . $nextpage;
  103. if($nextpage >$pages){
  104. // echo '没有了';
  105. exit;
  106. }
  107. ?>
  108. <a href=<?=$nexturl?>>下一页</a>
  109. </p>
  110. </body>
  111. </html>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
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
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!