Blogger Information
Blog 28
fans 0
comment 0
visits 26821
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
分页查询作业
YHF的博客
Original
1025 people have browsed it

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

分页查询原理:

1) 创建数据库连接

2)获取数据库数据

3)设计每页显示的纪录数

计算方法:

怎么分段,当前在第几段 (每页有几条,当前再第几页)

前10条记录:select * from table limit 0,10
第11至20条记录:select * from table limit 10,10
第21至30条记录:select * from table limit 20,10

 

分页公式:

(当前页数 - 1 )X 每页条数 , 每页条数
Select * from table limit ($Page- 1) * $PageSize, $PageSize

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

index.php

实例

<?php
//导入分页类
require './inc/Page.php';
use model\Page;
//实例化分页类
$page=new Page();
//连接数据库
$page->connect('mysql','127.0.0.1','test','root','');
//获取当前页
$nowPage=$page->getPage();
//获取总页数
$totalPage=$page->getPages('user');
//获取分页数据
$data=$page->getData('user');
?>
<!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;
            text-align: center;
            border-collapse:collapse;
        }
        table,th,td{
            border:1px solid #000000;
        }
        table tr:first-child{
            background-color:#22c9f1;
        }
        table tr th{
            padding: 5px;
        }
        table tr td{
            padding:8px;
        }
        table caption{
            font-size:1.5rem;
            margin: 15px;
        }
        h3{
            text-align: center;
        }
        h3 a{
            text-decoration:none;
            margin-left: 10px;
            border:1px solid black;
            display:inline-block;
            height: 30px;
            min-height: 30px;
            padding:0 10px;
            background-color:#d0e9c6;
        }
        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>
        <th>邮箱</th>
    </tr>
    <?php foreach ($data as $row): ?>
        <tr>
            <td><?php echo $row['id']; ?></td>
            <td><?php echo $row['name']; ?></td>
            <td><?php echo $row['salary']; ?></td>
            <td><?php echo $row['email']; ?></td>
        </tr>
    <?php endforeach;?>
</table>
<h3>
    <!--    当前是第一页时,上一页和首页不显示-->
    <?php if($nowPage !=1):?>
        <a href="http://127.0.0.1/phpstorm/peixun/php/15day/work/demo4.php?p=1">首页</a>
        <a href="http://127.0.0.1/phpstorm/peixun/php/15day/work/demo4.php?p=<?php
        echo (($nowPage-1)==0)? 1 : ($nowPage-1); ?>">上一页</a>
    <?php endif;?>

    <!--    中间部分-->
    <?php for($i=1;$i<=$totalPage;$i++):?>
        <a href="http://127.0.0.1/phpstorm/peixun/php/15day/work/demo4.php?p=<?php echo $i?>"
            <?php echo ($i==$nowPage)? 'style="background-color:red;"':''?>
        >
            <?php echo $i ?>
        </a>
    <?php  endfor;?>


    <!--    下一页与尾页-->
    <?php if($nowPage != $totalPage): ?>
        <a href="http://127.0.0.1/phpstorm/peixun/php/15day/work/demo4.php?p=<?php
        echo (($nowPage+1)>$totalPage)?$totalPage:($nowPage+1) ?>">下一页</a>
        <a href="http://127.0.0.1/phpstorm/peixun/php/15day/work/demo4.php?p=<?php
        echo $totalPage; ?>">尾页</a>
    <?php endif;?>
    <!--实现页面快速跳转-->
    <form action="" method="get">
        <select name="p" id="">
            <?php for($i=1;$i<=$totalPage;$i++): ?>
            <!--            循环输出全部页码,并锁定当前页-->
            <option value="<?php echo $i; ?>"
                <?php if($nowPage==$i){
                    echo 'selected';
                }
                ?>>
                <?php echo $i;?>
                <?php endfor;?>
            </option>
        </select>页
        <button>跳转</button>
    </form>
</body>
</html>

运行实例 »

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

page.php

实例

<?php
/**
 * Created by PhpStorm.
 * User: 714326675
 * Date: 2018/9/13
 * Time: 10:25
 */

namespace model;


class Page
{
    //查询起始偏移量
    private $offset;
    //每页记录数量
    private $num;
    //数据库连接对象
    private $pdo;
    //构造方法
    public function __construct($num=5){
        //初始化每页记录数量
        $this->num=$num;
        //查询起始偏移量:(页码-1)*数量
        $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则取p ,否则默认为1,即第一页
        return isset($_GET['p'])?$_GET['p']:1;
    }
    //获取总页数
    public function getPages(){
        //因为是读操作,所以必须使用count()获取,不允许通过rowCount()进行判断
        $stmt=$this->pdo->prepare("SELECT COUNT(*) FROM `user`;");
        $stmt->execute();
        //获取结果集中的一行一列,即记录总数
        $total=$stmt->fetchColumn(0);
        //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.jpg


总结:

偏移量计算:

(当前页数 - 1 )X 每页条数 , 每页条数
Select * from table limit ($Page- 1) * $PageSize, $PageSize

Correction status:Uncorrected

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