博客列表 >MYSQL+PHP+H5前端,实战分页原理,WEB开发术中的‘葵花宝典’...

MYSQL+PHP+H5前端,实战分页原理,WEB开发术中的‘葵花宝典’...

张福根一修品牌运营
张福根一修品牌运营原创
2020年12月11日 17:51:26878浏览

分页原理与实现:

连接数据库:

连接数据库

定义数据库databases.php:

  1. <?php
  2. namespace pdo_edu;
  3. return [
  4. 'type' => $type ?? 'mysql',
  5. 'host' => $host ?? 'localhost',
  6. 'dbname' => $dbname ?? 'apple',
  7. 'port' => $port ?? '3306',
  8. 'charset' => $charset ?? 'utf8',
  9. 'username' => $username ?? 'root',
  10. 'password' => $password ?? 'root'
  11. ];

连接数据库connect.php:

  1. <?php
  2. namespace pdo_edu2;
  3. $config = require __DIR__ . '/database.php';
  4. use PDO;
  5. //PDO数据连接~三要素 DSN数据源 username password,
  6. extract($config);
  7. $dsn = sprintf('%s:host=%s;dbname=%s',$type,$host,$dbname);
  8. try {
  9. $pdo = new PDO($dsn,$username ,$password,[PDO::ATTR_ERRMODE=> PDO::ERRMODE_WARNING]);
  10. // var_dump($pdo);
  11. //设置结果集的默认获取方式,只获取关联数组部分
  12. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
  13. } catch(\Exception $e) {
  14. die('Connection error: '. $e->getMessage());
  15. }

拿到数据shuju.php:

拿到数据

后端处理处理:

  1. <?php
  2. require 'connect.php';
  3. //每页显示的数量
  4. $num = 8;
  5. //当前页码,默认为1
  6. $page = $_GET['p'] ?? 1;
  7. // 计算偏移量
  8. $offset = ($page - 1) * $num ;
  9. //获取分页数据
  10. $sql = " SELECT `id`,`name`,`pro`,`price` FROM `order` LIMIT {$num} OFFSET {$offset}";
  11. $orders = $pdo->query($sql)->fetchAll();
  12. // print_r($users);
  13. //获取总页数,count() 函数返回数组中元素的数目,ceil() 函数向上舍入为最接近的整数。
  14. $sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `order`";
  15. $pages = $pdo->query($sql)->fetch()['total'];
  16. // print_r($pages);

前端数据分页显示fenye.php:

数据分页显示

  1. <?php require 'shuju.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. <link rel="stylesheet" href="style.css">
  9. </head>
  10. <body>
  11. <table>
  12. <caption>服务需求信息表</caption>
  13. <thead>
  14. <tr>
  15. <td>编号</td>
  16. <td>姓名</td>
  17. <td>项目</td>
  18. <td>价格</td>
  19. <td>操作</td>
  20. </tr>
  21. </thead>
  22. <tbody>
  23. <?php foreach($orders as $order):?>
  24. <tr>
  25. <td><?= $order['id']?></td>
  26. <td><?= $order['name']?></td>
  27. <td><?= $order['pro']?></td>
  28. <td><?= $order['price']?></td>
  29. <td><button>删除</button><button>编辑</button></td>
  30. </tr>
  31. <?php endforeach;?>
  32. </tbody>
  33. </table>
  34. <!-- 动态生成分页条 -->
  35. <p>
  36. <!-- 1. 分页条的动态生成,2. 跳转地址的动态生成,3. 当前页码的高亮显示 -->
  37. <!-- 分页条中的页码应该动态生成 -->
  38. <?php for ($i = 1; $i <= $pages; $i++): ?>
  39. <?php
  40. $jump = sprintf('%s?p=%s',$_SERVER['PHP_SELF'], $i);
  41. // $i: 分页中的页码
  42. // $page: 在URL中通过GET获取的页码?p=X
  43. $active = ($i == $page) ? 'active' : null;
  44. ?>
  45. <a href="<?=$jump?>" class="<?=$active?>"><?=$i?></a>
  46. <?php endfor ?>
  47. </p>
  48. </body>
  49. </html>

style.css:

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. color: #555;
  6. }
  7. body {
  8. display: flex;
  9. flex-direction: column;
  10. align-items: center;
  11. }
  12. /*表格样式*/
  13. table {
  14. width: 90%;
  15. border: 1px solid;
  16. border-collapse: collapse;
  17. text-align: center;
  18. }
  19. table caption {
  20. font-size: 1.2rem;
  21. margin: 15px;
  22. }
  23. table td,
  24. table th {
  25. border: 1px solid;
  26. padding: 10px;
  27. }
  28. table tr:hover {
  29. background-color: #eee;
  30. }
  31. table thead tr:only-of-type {
  32. background-color: lightseagreen;
  33. }
  34. table button {
  35. width: 56px;
  36. height: 26px;
  37. }
  38. table button:last-of-type {
  39. color: seagreen;
  40. }
  41. table button {
  42. cursor: pointer;
  43. margin: 0 3px;
  44. }
  45. /*分页条样式*/
  46. body > p {
  47. display: flex;
  48. }
  49. p > a {
  50. text-decoration: none;
  51. color: #555;
  52. border: 1px solid;
  53. padding: 5px 10px;
  54. margin: 10px 2px;
  55. }
  56. .active {
  57. background-color: lightseagreen;
  58. color: white;
  59. border: 1px solid lightseagreen;
  60. }
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议
酒淋后2020-12-11 18:10:071楼
优秀太优秀了,带带我