博客列表 >实现页面上一页,下一页功能并完成仿PHP中文网分页条

实现页面上一页,下一页功能并完成仿PHP中文网分页条

叫我孙大树
叫我孙大树原创
2022年08月22日 02:40:52331浏览
<?php
namespace _0819;
//引入朱老师的分页条函数
require __DIR__ . '/page_refer.php';
//加载模拟的数据库数据
$pure_userInfo = file_get_contents(__DIR__ . '/userinfo.json');
$pure_userInfo = json_decode($pure_userInfo, true);
//虚拟数据库
function VitrualSQLreturn($arr, $offset, $limit)
{
    return array_filter($arr, function ($e) use ($offset, $limit) {

        return $e['id'] > $offset && $e['id'] <= $offset + $limit;
    });
}

//实现GET传参翻页
$page = $_GET['page'] ?? (int)1;
$pageLimit = (int)20;
$userInfo = VitrualSQLreturn($pure_userInfo, ($page - 1) * $pageLimit, $pageLimit);
//实现分页器必要参数
$allDataCount = count($pure_userInfo);
$pageChange = createPages((int)$page, ceil((int)$allDataCount / (int)$pageLimit));
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>选手信息表</title>
    <style>
        table {
            /*width: 400px;*/
            border-collapse: collapse;
            text-align: center;
        }

        table th,
        table td {
            border: 1px solid;
            padding: 5px;
        }

        table thead {
            background-color: lightcyan;
        }

        table caption {
            font-size: larger;
            /*margin-bottom: 8px;*/
            font-weight: bold;
            border: 1px solid;
            padding: 5px 0px;
        }

        body > p {
            display: flex;
        }

        p > a {
            text-decoration: none;
            color: #555;
            border: 1px solid;
            padding: 5px 10px;
            margin: 10px 2px;
        }

        .active {
            background-color: seagreen;
            color: white;
            border: 1px solid seagreen;
        }

        .close{
            color: #bbb;
        }

    </style>
</head>
<body>
<table>
    <caption>选手信息表</caption>
    <thead>
    <tr>
        <th>id</th>
        <th>姓名</th>
        <th>身份证</th>
        <th>出生日期</th>
        <th>性别</th>
        <th>电话号</th>
        <th>住址</th>
    </tr>
    </thead>

    <tbody>
    <?php foreach ($userInfo as $v) :extract($v) ?>
        <tr>
            <td><?= $id ?></td>
            <td><?= $name ?></td>
            <td><?= $identity_num ?></td>
            <td><?= $date_of_birth ?></td>
            <td><?php echo $sex ? '男' : '女' ?></td>
            <td><?= $phone_num ?></td>
            <td><?= $address ?></td>
        </tr>
    <?php endforeach ?>
    </tbody>
</table>
<p>
    <?php if ($page > 1) : ?>
    <a href="<?php echo $_SERVER['PHP_SELF'] . '?page=' . $page-1 ?>" >上一页</a>
    <?php else: ?>
    <a class="close">上一页</a>
    <?php endif ?>
    <?php foreach ($pageChange as $v) : ?>
        <?php if (isset($v)): ?>
            <a href="<?php echo $_SERVER['PHP_SELF'] . '?page=' . $v ?>" <?php echo $page == $v ? 'class="active"' : null ?>><?= $v ?></a>
        <?php else: ?>
            <a>......</a>
        <?php endif ?>
    <?php endforeach ?>
    <?php if ($page < ceil((int)$allDataCount / (int)$pageLimit)) : ?>
        <a href="<?php echo $_SERVER['PHP_SELF'] . '?page=' . $page+1 ?>" >下一页</a>
    <?php else: ?>
        <a class="close">下一页</a>
    <?php endif ?>
</p>
</body>
</html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议