博主信息
博文 38
粉丝 0
评论 0
访问量 30588
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
第十五课—分页查询 2018年9月10日
空白
原创
767人浏览过

1.分页查询的原理:根据sql查询语句LIMIT的偏移量与显示数量

2.偏移量的计算方法:当前偏移量的计算公式 = (页数-1) * 每页显示的数量  offset = (page-1) * num


3.分页查询的实现:

实例

<?php
    require './query.php';
    use model\Query;

    $page=new Query();
//    连接数据库
    $page->connect('mysql','127.0.0.1','test','root','');

//    获取当前分页
    $currentPage = $page->getPage();

//    获取总页数
    $totalPages = $page->getPages('user');

//    获取分页数据
    $data = $page->getData('user');
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>分页查询</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <table class="table table-bordered">
                    <tbody>
                    <caption class="h3 text-center">员工信息表</caption>
                    <tr>
                        <th>id</th>
                        <th>name</th>
                        <th>sex</th>
                        <th>salary</th>
                    </tr>
                    <?php foreach ($data as $row):?>
                    <tr>
                        <td><?php echo $row['id'];?></td>
                        <td><?php echo $row['name'];?></td>
                        <td><?php echo $row['sex']?'男':'女';?></td>
                        <td><?php echo $row['salary'];?></td>
                    </tr>
                    <?php endforeach;?>
                    </tbody>
                </table>

                <nav aria-label="Page navigation">
                    <ul class="pagination">
<!--                        首页-->
                        <li>
                            <a href="http://127.0.0.1/php/15/1.php?p=1">首页</a>
                        </li>

<!--                        上一页-->
                        <li>
                            <a href="http://127.0.0.1/php/15/1.php?p=<?php echo $currentPage-1;?>" aria-label="Previous">
                                <span aria-hidden="true">«</span>
                            </a>
                        </li>

                        <?php for ($i=1; $i<=$totalPages; $i++):?>
                        <li>
                            <a href="http://127.0.0.1/php/15/1.php?p=<?php echo $i;?>"><?php echo $i;?></a>
                        </li>
                        <?php endfor;?>

<!--                        下一页-->
                        <li>
                            <a href="http://127.0.0.1/php/15/1.php?p=<?php echo $currentPage+1;?>" aria-label="Next">
                                <span aria-hidden="true">»</span>
                            </a>
                        </li>

<!--                        尾页-->
                        <li>
                            <a href="http://127.0.0.1/php/15/1.php?p=<?php echo $totalPages;?>">尾页</a>
                        </li>
                    </ul>
                </nav>

                <form action="" method="get">
                    <select name="p" class="form-control" style="width: 200px;">
                        <?php for ($i=1; $i<=$totalPages; $i++):?>
                            <option value="<?php echo $i;?>" <?php if($currentPage==$i){echo 'selected';}?>><?php echo $i;?></option>
                        <?php endfor; ?>
                    </select>
                    <button class="btn btn-default" type="submit">跳转</button>
                </form>

            </div>
        </div>
    </div>

<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

运行实例 »

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

实例

<?php
/**
 * 分页查询类
 */

namespace model;

class Query
{
    //起始偏移量
    private $offset;

    //每页记录数
    private $num;

    //数据库连接对象
    private $pdo;

//    每页查询数
    public function __construct($num=5)
    {
        $this->num=$num;
        $this->offset = ($this->getPage()-1)*$this->num;
    }

//    连接数据库
    public function connect($type,$host,$dbname,$user,$pass)
    {
        try {
            $this->pdo = new \PDO("{$type}:host={$host};dbname={$dbname}",$user,$pass);
        } catch (\PDOException $e) {
            die($e->getMessage());
        }
    }

//    获取当前页码
    public function getPage()
    {
        //如果url中存在页码变量p则取之,否则默认为1,即第一页
        return isset($_GET['p']) ? $_GET['p'] : 1;
    }

//    获取总页数
    public function getPages($table)
    {
        $stmt=$this->pdo->prepare("SELECT COUNT(*) FROM $table");
        $stmt->execute();

//        获取记录总数
        $total=$stmt->fetchColumn();

        // ceil()是向上取整函数
        return ceil($total/$this->num);
    }

//    获取分页数据
    public function getData($table)
    {
//        从指定数据表中的获取当前页需要显示的记录
        $sql="SELECT *FROM {$table} LIMIT {$this->offset},{$this->num}";
        $stmt=$this->pdo->prepare($sql);
        $stmt->execute();

//        获取分页数据并返回关联部分
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }
}

运行实例 »

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

1.png


总结:偏移量 = (当前页码-1)*每页显示的条数数量

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学