php实现一个简单的MySQL分页

一、案例演示:

二、php 代码

bash 复制代码
<?php
$servername = "localhost";  // MySQL服务器名称或IP地址  
$username = "root";     // MySQL用户名  
$password = "123456";     // MySQL密码  
$dbname = "test";       // 要连接的数据库名称  
$port = "3307";       // 要连接的数据库名称  

// 创建连接  
$dbc = new mysqli($servername, $username, $password, $dbname, $port);

// 检查连接是否成功  
if ($dbc->connect_error) {  
    die("连接失败: " . $dbc->connect_error);  
}  
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>分页</title>
    <link rel="stylesheet" href="./css/bootstrap.min.css">
    <script src="./js/jquery.min.js"></script>
    <script src="./js/bootstrap.min.js"></script>
    <style>
        body {
            width: 100%;
            overflow: hidden;
            background: url("background.jpeg") no-repeat;
            background-size: cover;
        }
        .table-container {
            width: 80%;
            margin: 0 auto;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            background-color: white;
        }
        th, td {
            border: 1px solid black;
            padding: 8px;
            text-align: center;
        }
        th {
            background-color: #f2f2f2;
        }
        .pagination {
            display: flex;
            justify-content: center;
        }
    </style>
</head>

<body>
    
    <h1 style="text-align: center"><strong>分页</strong></h1>
    <div class="table-container">
        <table class="table table-hover">
            <tr>
                <th>id</th>
                <th>姓名</th>
            </tr>
            <?php
                // 定义每页显示的记录数
                $pageSize = 10;

                // 获取当前页码,默认为第一页
                $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

                // 计算当前页码对应的记录偏移量
                $offset = ($page - 1) * $pageSize;

                $sql = "SELECT * FROM page order by id asc LIMIT $offset, $pageSize";

                $res = mysqli_query($dbc, $sql);
                // 计算总记录数
                $totalSql = "SELECT COUNT(*) AS total FROM page";
                $totalResult = mysqli_query($dbc, $totalSql);
                $totalRow = mysqli_fetch_assoc($totalResult);
                $total = $totalRow['total'];

                // 计算总页数
                $totalPages = ceil($total / $pageSize);

                foreach ($res as $row) {
                    echo "<tr>";
                    echo "<td>{$row['id']}</td>";
                    echo "<td>{$row['name']}</td>";
                    echo "</tr>";
                }
            ?>
        </table>
    </div>
    <div style="text-align: center; margin-top: 20px;">
        <ul class="pagination">
            <?php
            // 生成分页链接
            for ($i = 1; $i <= $totalPages; $i++) {
                // 根据当前页添加特殊样式
                $activeClass = ($i == $page) ? 'active' : '';
                echo "<li class='$activeClass'><a href='index.php?page=$i'>$i</a></li>";
            }
            ?>
        </ul>
    </div>
</body>

</html>
相关推荐
Shi_haoliu6 小时前
SolidTime 在 Rocky Linux 9.5 上的完整部署流程
linux·运维·nginx·postgresql·vue·php·laravel
千层冷面7 小时前
数据库分库分表
java·数据库·mysql·oracle
清风拂山岗 明月照大江9 小时前
MySQL运维
运维·数据库·mysql
进击的CJR9 小时前
redis哨兵实现主从自动切换
mysql·ffmpeg·dba
crossaspeed9 小时前
MySql三大日志——(八股)
数据库·mysql
59678515411 小时前
css浮动
前端·css·html
松涛和鸣11 小时前
DAY55 Getting Started with ARM and IMX6ULL
linux·服务器·网络·arm开发·数据库·html
成工小白12 小时前
网络复习(1)
服务器·网络·php
墨香幽梦客12 小时前
数据库选型对比:MySQL、Oracle与PostgreSQL的企业应用场景分析
数据库·mysql·oracle
恃宠而骄的佩奇13 小时前
蚁剑 php一句话木马简单免杀(编码)绕过360,火绒
开发语言·web安全·php·免杀·一句话木马·火绒安全