Blogger Information
Blog 38
fans 1
comment 0
visits 26096
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
分页类查询与实战--2018年09月11日15时40分
一根火柴棒的博客
Original
501 people have browsed it

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

答:首先要计算出,有多少个符合查询条件的数据,其次在获取到每页要显示的数据数量,再计算出一共需要用多少页来进行显示,这样就能得到3个变量:总数据个数,每页显示个数,总页数,这样就在做分页查询的时候把这几个数据表现出来即可


2编程: 实现分页查询,要求有上一下,下一页,直接跳到首页和尾页,中间页的生成,以及快速页码跳转功能:

实例

<?php

$pdo = new PDO('mysql:host=127.0.0.1;dbname=user', 'root', 'root');

$page = isset($_GET['p']) ? $_GET['p'] : 1;
$offset = ($page - 1) * 2;
$sql = "SELECT * FROM name LIMIT {$offset},2";

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

//获取总记录数
$stmt = $pdo->prepare("SELECT COUNT(*) FROM name");
$stmt->execute();
$total = $stmt->fetchColumn(0);

//获取总页数
$totalPage = ceil($total / 2); //ceil 向上取整

?>


<!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, th, td {
            border: 1px solid black;
        }

        table th {
            background-color: lightseagreen;
        }

        table {
            border-collapse: collapse;
            width: 60%;
            margin: 30px auto;
            text-align: center;
        }

        table caption {
            font-size: 1.5rem;
            margin-bottom: 15px;
        }

        h3 {
            text-align: center;
        }

        h3 a {
            text-decoration-line: none;
            margin-left: 10px;
            border: 1px solid black;
            border-radius: 15px;

            display: inline-block;
            height: 30px;
            min-width: 20px;
            padding: 0 10px;
            background-color: green;
            color: black;
        }

        h3 a:hover {
            background-color: red;
            color: white;
        }

        form {
            display: inline;
        }
    </style>
</head>
<body>

<table>
    <caption>员工信息表</caption>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>年龄</th>
    </tr>
    <?php foreach ($rows as $row): ?>
        <tr>
            <td><?php echo $row['id'] ?></td>
            <td><?php echo $row['name'] ?></td>
            <td><?php echo $row['age'] ?></td>
        </tr>
    <?php endforeach; ?>

</table>

<h3>
    <a href="http://www.php.io/0910/demo1.php?p=1">首页</a>
    <a href="http://www.php.io/0910/demo1.php?p=<?php echo (($page - 1) == 0) ? 1 : ($page - 1); ?>">上一页</a>
    <?php for ($i = 1; $i <= $totalPage; $i++): ?>
        <a href="http://www.php.io/0910/demo1.php?p=<?php echo $i; ?>"
        <?php echo ($i == $page) ? 'style="background-color:yellow;color:red"': '';?>

        ><?php echo $i ?></a>
    <?php endfor; ?>


    <a href="http://www.php.io/0910/demo1.php?p=<?php echo (($page + 1) > $totalPage) ? $totalPage : $page + 1; ?>">下一页</a>
    <a href="http://www.php.io/0910/demo1.php?p=<?php echo $totalPage; ?>">尾页</a>


    <form action="" method="get">
        第
        <select name="p" id="">

            <?php for ($i=1;$i<=$totalPage;$i++):?>
                <option value=<?php echo $i?>><?php echo $i?></option>
            <?php endfor;?>
        </select>
        页
        <button>跳转</button>
    </form>



</h3>



</body>
</html>

运行实例 »

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


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