Blogger Information
Blog 38
fans 0
comment 0
visits 25281
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
第十五课—分页查询 2018年9月10日
空白
Original
625 people have browsed it

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)*每页显示的条数数量

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
Author's latest blog post