Blogger Information
Blog 36
fans 0
comment 0
visits 28496
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
分页查询的原理及其应用——2018年9月10日
Jackson
Original
646 people have browsed it

1.分页查询的原理

查询语句中 LIMIT 0,5; LIMIT的第一个参数是偏移量,第二个参数是每个页面显示的数据量。而偏移量是有一个计算公式的,如下:

            偏移量 = (当前页码-1)*每个页面显示的数量

当前页码则是等于地址栏中的提交变量p;通过提交不同的页码,来获取不同的查询数据,然后循环输出在表格里。

2.分页查询的实现

实例

<!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{
            text-align: center;
            width: 40%;
            border-collapse: collapse;
            margin: 20px auto;
        }
        table,td,th,tr{
            border: 1px solid deepskyblue;
        }
        table tr:first-child{
            background: skyblue;
        }
        table caption{
            font-size: 1.5em;
            margin-bottom: 15px;
        }
        td,th{padding:5px 10px;}
        h3{text-align: center;}
        a{
            text-decoration-line: none;
            display: inline-block;
            border: 1px solid gray;
            color: deepskyblue;
            border-radius: 5px;
            box-shadow: 1.5px 1.5px 1.5px lightskyblue;
            margin-left: 5px;
            padding:0 5px;
            font-weight: normal;
        }
        a:hover, button:hover{
            background: skyblue;
            color: white;
        }
        form{display: inline-block;}
        form select,button{
            color: deepskyblue;
            box-shadow: 1.5px 1.5px 1.5px lightskyblue;
            margin-left: 5px;
            padding:2px 5px;

        }
    </style>
<body>
<?php
/**
 * 分页查询原理
 * 地址栏 $p
 * 偏移量 显示数量  $num  limit0,5   $_GET[$p]
 */
$pdo = new PDO('mysql:host=localhost;dbname=php','root','root');
$pdo->query('set names utf8');

$page = isset($_GET['p']) ? $_GET['p'] : 1;//从地址栏获取当前页码
$num = 5;//每页显示数量
$offset = ($page-1)*$num; //偏移量
$sql = "SELECT * FROM `staff` LIMIT {$offset}, {$num};";

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

$stmt = $pdo->prepare("SELECT * FROM `staff`; ");
$stmt->execute();
$pages = ceil(count($stmt->fetchAll(PDO::FETCH_ASSOC))/$num);
?>
    <table>
        <caption>员工信息表</caption>
        <tr>
            <th>ID</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>工资</th>
        </tr>
        <?php foreach($result as $row):?>
        <tr>
            <td><?php echo $row['staff_id'];?></td>
            <td><?php echo $row['name'];?></td>
            <td><?php echo $row['age'];?></td>
            <td><?php echo $row['salary'];?></td>
        </tr>
        <?php endforeach;?>
    </table>

    <h3>
        <a href="demo.php?p=<?php echo $page>1 ? $page-1 : 1;?>">上一页</a>
        <a href="demo.php?p=<?php echo $page=1?>">首页</a>

        <a href="demo.php?p=<?php echo $page=1?>">1</a>
        <a href="demo.php?p=<?php echo $page=2?>">2</a>
        <a href="demo.php?p=<?php echo $page=3?>">3</a>
        <a href="demo.php?p=<?php echo $page=4?>">4</a>

        <a href="demo.php?p=<?php echo $page=$pages ?>">尾页</a>
        <a href="demo.php?p=<?php echo $page<$pages ? $page+1 : $pages;?>">下一页</a>

        <form action="demo.php" method="get">
            <select name="p" id="page">
                <?php for($i=1; $i<=$pages; $i++):?>
                <option value="<?php echo $i;?>" <?php echo $_GET['p'] == $i? 'selected': '';?> >第<?php echo $i;?>页</option>
                <?php endfor;?>
            </select>
            <button>跳转</button>
        </form>
    </h3>
</body>
</html>
运行实例 »

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

总结: 要理解清楚查询语句limit的两个参数,同时还要知道偏移的计算方法。


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