在一个网站中,分页是很常见的,可是怎样才能实现分页呢,今天为大家实现一下分页的效果
首先我们写分页前要知道两个很重要的地方,一个是数据库查询方式,一个是分页计算公式
数据库查询方式  select * from tableName limit  num1,num2; 这是一个限定查询的语句,后面跟两个参数,num1为从第几个开始查找,num2为查找的信息的个数
从查询方式中 num1这个值怎么来呢,  这就用到分页的计算公式了, num1 = (当前页 - 1) * 每页显示的条数


实现分页前我们先在数据库里面准备一个表,并且插入数据,这些都是必需的前提工作了 (直接拷贝到数据库运行就行)
CREATE TABLE `test`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
  `content` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 19 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;


INSERT INTO `test` VALUES (1, '测试1', '睡大觉');
INSERT INTO `test` VALUES (2, '测试1', '睡大觉');
INSERT INTO `test` VALUES (3, '测试1', '睡大觉');
INSERT INTO `test` VALUES (4, '测试1', '睡大觉');
INSERT INTO `test` VALUES (5, '测试1', '睡大觉');
INSERT INTO `test` VALUES (6, '测试1', '睡大觉');
INSERT INTO `test` VALUES (7, 'qwea', '睡大觉');
INSERT INTO `test` VALUES (8, 'zxcz', '睡大觉');
INSERT INTO `test` VALUES (9, 'qwe', '睡大觉');
INSERT INTO `test` VALUES (10, 'bcv', '睡大觉');
INSERT INTO `test` VALUES (11, 'rtyu', '睡大觉');
INSERT INTO `test` VALUES (12, 'xcvx', '睡大觉');
INSERT INTO `test` VALUES (13, '456', '睡大觉');
INSERT INTO `test` VALUES (14, 'eqwe456', '睡大觉');
INSERT INTO `test` VALUES (15, 'asd2', '睡大觉');
INSERT INTO `test` VALUES (16, '123', '睡大觉');
INSERT INTO `test` VALUES (17, 'rcvxtyu', '睡大觉');
INSERT INTO `test` VALUES (18, '2312asda', '睡大觉');

 

<?php

//创建一个分页数据查询的方法
function getPageList($page,$pageSize){
    // 创建连接
    $conn = mysqli_connect('127.0.0.1', 'root', 'root', 'page');
    $s = ($page - 1) * $pageSize;
    $sql = "SELECT * FROM test LIMIT {$s},{$pageSize}";
    $sql1 = "SELECT count(*) as count FROM test";
    $result = mysqli_query($conn, $sql);
    $result1 = mysqli_query($conn, $sql1);
    $data['table_count'] = $result1->fetch_assoc()['count'];
    $data['pageSize'] = $pageSize;
    $data['data'] = [];
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $data['data'][] = $row;
        }
    }
    mysqli_close($conn);
    return $data;
}

//创建一个处理分页的方法(这里我只实现 首页,上一页,下一页,尾页)
function getPage($data){
    //首先我们要先处理参数
    $get = $_GET;
    $page = isset($get['page'])?$get['page']:1;

    //将其他的参数拼接起来,不需要分页页数
    unset($get['page']);
    $url = http_build_query($get);
    $url = $url?$url.'&':'?';

    $end_page = ceil($data['table_count'] / $data['pageSize']);
    $src = '';
    //如果当前页数为1 那第一个跟首页不能点击
    if($page == 1){
        $src = "<a><span>首页</span></a>&emsp;";
        $src .= "<a><span>上一个</span></a>&emsp;";
    }else{
        $src .= "<a href='?{$url}page=1'>首页</a>&emsp;";
        $pages = $page - 1;
        $src .= "<a href='?{$url}page={$pages}'>上一个</a>&emsp;";
    }

    //如果当前页是最后一页 那下一个跟尾页不能点击
    if($page == $end_page){
        $src .= "<a ><span>下一个</span></a>&emsp;";
        $src .= "<a><span>尾页</span></a>&emsp;";
    }else{
        $pages = $page + 1;
        $src .= "<a href='?{$url}page={$pages}'>下一个</a>&emsp;";
        $src .= "<a href='?{$url}page={$end_page}'>尾页</a>&emsp;";
    }
    return $src;
}


$page = isset($_GET['page'])?$_GET['page']:1;
$list = getPageList($page,5);
$page = getPage($list);

?>

<!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>Document</title>
</head>
<body>
    <table>
        <tr>
            <th>id</th>
            <th>name</th>
        </tr>
        <?php foreach($list['data']  as $v){  ?>
            <tr>
                <td><?php echo $v['id']; ?></td>
                <td><?php echo $v['name']; ?></td>
            </tr>
        <?php } ?>

    </table>
    <?php echo $page; ?>
</body>
</html>