Blogger Information
Blog 21
fans 0
comment 0
visits 18604
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
分面查询原理与类封装—2018年9月10日23时50分
耗子的博客
Original
732 people have browsed it

一问答:分页查询的原理与偏移量的计算方法

分页功能的开发流程:

1-连接数据库

2-读取数据库数据

3-生成LIMIT语句获取分页数据

4-使用分页SQL语句获取数据

5-显示获取的数据

6-获取总记录数用于页数及算偏移量

7-对翻页以及页码进行处理

偏移量的计算方法= (当前页码 -1) * 每页显示的数量


实例

<?php
//连接数据库(添加判断是否连接成功)
try {
    $pdo=new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
}catch (PDOException $ex)
{
    die($ex->getMessage());
}

//取得每页数量
$num=8;


//获取总记录数
$sql="SELECT COUNT(*) FROM sys_user;";
$stmt=$pdo->prepare($sql);
$stmt->execute();
$totalRecord=$stmt->fetchColumn(PDO::ATTR_AUTOCOMMIT);


//当前页数(使用三元运算符判断当前页URL是否存在p=?,如果存在则获取,如果不存在设置1)
$currentPage=isset($_GET['p'])?$_GET['p']:1;


//设置偏移值:(当前页数-1)*每页数量$num
$offset=($currentPage-1)*$num;


//取得总页数(使用ceil进一取整)
$totalPages=ceil($totalRecord/$num);



//获取数据
$sql="SELECT * FROM sys_user LIMIT {$offset},{$num};";

$stmt=$pdo->prepare($sql);
$stmt->execute();
$data=$stmt->fetchAll(PDO::FETCH_ASSOC);


?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>分页实现</title>
    <style>
        table{
           width: 70%;
            margin: 30px auto;        /*外边框上下30,左右自动*/
            text-align: center;     /*文本居中*/
            border-collapse: collapse;
        }
        table caption{
            font-family: 微软雅黑;
            font-size:1.5rem;
            font-weight: bold;
            margin-bottom: 20px;
        }
        table tr td,table tr th,span,option,select,button{
            font-family: 微软雅黑;
            font-size: 1rem;

        }
        a{
            text-decoration-line: none;
        }
        table th{
            background-color: #4cae4c;
            color: #fff;
        }
        table,tr,td{
            border: 1px solid #4cae4c;
            height: 40px;
        }
        table tr th{
            border: 1px solid #75ae87;
            height: 40px;
        }
        .page{
            text-align: center;
        }
        .page-class{
            display: inline-block;

        }
        div a{
            /*margin-left: 0px;*/
            /*background-color: #5cb85c;*/
            color: #5f677e;
            display: inline-block;
            border: 1px solid #5f677e;

            min-width: 23px;
        }
        div a:hover{
            background-color: #317ef3;
            color: #ffffff;
            border: 1px solid #317ef3;
        }
        button:hover{
            background-color: #317ef3;
            color: #ffffff;
            /*border: 1px solid #317ef3;*/
        }
        .active{
            background-color:  #317ef3;
            color: #fff;
        }

    </style>


</head>
<body>
<table>
    <caption>用户信息表</caption>
    <tr>
        <th>Id</th>
        <th>用户</th>
        <th>名称</th>
        <th>班次</th>
        <th>性别</th>
        <th>生日</th>
        <th>年龄</th>
        <th>照片</th>
        <th>电话</th>
        <th>地址</th>
        <th>卡号</th>
        <th>邮箱</th>
        <th>QQ</th>
        <th>部门</th>
        <th>区域</th>
        <th>微信</th>
    </tr>


    <?php foreach ($data as $row): ?>
    <tr>
        <td><?php echo $row['Id'];?></td>
        <td><?php echo $row['User']; ?></td>
        <td><?php echo $row['Name']; ?></td>
        <td><?php echo $row['TypeClass'];?></td>
        <td><?php echo $row['Gender'];?></td>
        <td><?php echo $row['Birthday'];?></td>
        <td><?php echo $row['Age'];?></td>
        <td><?php echo $row['Image'];?></td>
        <td><?php echo $row['Phone'];?></td>
        <td><?php echo $row['Address'];?></td>
        <td><?php echo $row['CardNo'];?></td>
        <td><?php echo $row['Email'];?></td>
        <td><?php echo $row['Qq'];?></td>
        <td><?php echo $row['Depart'];?></td>
        <td><?php echo $row['Area'];?></td>
        <td><?php echo $row['WorkWx'];?></td>
    </tr>
    <?php endforeach;?>
</table>
<div class="page">
<p class="page-class">当前是第:<?php echo $currentPage ?>页,共:<?php echo $totalPages?>页  </p>
<a href="zy.php?p=1">首页</a>

<a  href="zy.php?p=<?php echo ($currentPage-1)<=1?1:($currentPage-1) ?>">上一页</a>
<?php for ($i=1;$i<=$totalPages;$i++): ?>
    <a class="<?php if ($currentPage==$i) {echo 'active';} ?>" href="zy.php?p=<?php echo $i ?>"><?php echo $i ?></a>

<?php endfor; ?>
<a href="zy.php?p=<?php echo ($currentPage+1)>=$totalPages?$totalPages:($currentPage+1) ?>">下一页</a>
<a href="zy.php?p=<?php echo $totalPages?>">尾页 </a>
    <span>  跳转至:</span>
<form action="" method="get" style="display: inline-block">
    <select name="p" id="">
        <?php for ($i=1;$i<=$totalPages;$i++):?>
    <option value="<?php echo $i;?>">

        <?php echo $i; ?>
    </option>

     <?php endfor; ?>
    </select>
    <button>确定</button>
</form>
</div>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

效果.png

总结:

本章节老师讲解了分面查询原理与类封装,

分页功能的开发流程:

1-连接数据库

2-读取数据库数据

3-生成LIMIT语句获取分页数据

4-使用分页SQL语句获取数据

5-显示获取的数据

6-获取总记录数用于页数及算偏移量

7-对翻页以及页码进行处理

偏移量的计算方法= (当前页码 -1) * 每页显示的数量


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