php实现一个简单的MySQL分页

devtools/2024/10/11 13:26:41/

一、案例演示:

在这里插入图片描述

html" title=php>php__2">二、html" title=php>php 代码

<?html" title=php>php
$servername = "localhost";  // MySQL服务器名称或IP地址  
$username = "root";     // MySQL用户名  
$password = "123456";     // MySQL密码  
$dbname = "test";       // 要连接的数据库名称  
$port = "3307";       // 要连接的数据库名称  // 创建连接  
$dbc = new html" title=mysql>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><?html" title=php>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 = html" title=mysql>mysqli_query($dbc, $sql);// 计算总记录数$totalSql = "SELECT COUNT(*) AS total FROM page";$totalResult = html" title=mysql>mysqli_query($dbc, $totalSql);$totalRow = html" title=mysql>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"><?html" title=php>php// 生成分页链接for ($i = 1; $i <= $totalPages; $i++) {// 根据当前页添加特殊样式$activeClass = ($i == $page) ? 'active' : '';echo "<li class='$activeClass'><a href='index.html" title=php>php?page=$i'>$i</a></li>";}?></ul></div>
</body></html>

http://www.ppmy.cn/devtools/53883.html

相关文章

解决Qt中 -lGL无法找到的问题

在使用Qt Creator创建并编译新项目时&#xff0c;可能会遇到以下错误&#xff1a; /usr/bin/ld: cannot find -lGL collect2: error: ld returned 1 exit status make: *** [untitled1] Error 1 18:07:41: The process "/usr/bin/make" exited with code 2. Error w…

计算机网络 —— 应用层(DNS域名系统)

计算机网络 —— 应用层&#xff08;DNS域名系统&#xff09; 什么是DNS域名的层次结构域名分类 域名服务器的分类域名解析方式递归查询&#xff08;Recursive Query&#xff09;迭代查询&#xff08;Iterative Query&#xff09;域名的高速缓存 我们今天来看DNS域名系统 什么…

二叉树和数的概念;以及二叉树中堆的实现和堆排序(向上和向下调整算法)大白话 上篇

每周一篇小博客&#xff0c;你我都没小烦恼&#xff1b;这次来讲讲数据结构中的重点内容&#xff0c;二叉树的堆 此次总共分为三篇&#xff0c;上中下&#xff1b;分别讲的是堆、链式结构二叉树、经典例题的讲解&#xff1b;接下就是树的概念和堆 目录 1.树概念及结构 1.2树…

【重要】《HTML趣味编程》专栏内资源的下载链接

目录 关于专栏 博主简介 专栏内资源的下载链接 写在后面 关于专栏 本专栏将持续更新,至少含有30个案例,后续随着案例的增加可能会涨价,欢迎大家尽早订阅!(订阅后可查看专栏内所有文章,并且可以下载专栏内的所有资源) 博主简介 ⭐ 2024年百度文心智能体大赛 Top1⭐…

oracle case when 的用法

在Oracle数据库中&#xff0c;CASE WHEN语句是一个条件表达式&#xff0c;它允许你基于不同的条件执行不同的操作。该语句在SELECT、UPDATE、DELETE语句以及PL/SQL块中都非常有用。下面是关于CASE WHEN在Oracle中的详细用法&#xff1a; 一、基本语法 CASE WHEN condition1 …

Talk|新加坡国立大学贾鑫宇:适用于高自由度机器人的运动控制器

本期为TechBeat人工智能社区第600期线上Talk。 北京时间6月13日(周四)20:00&#xff0c;新加坡国立大学博士生—贾鑫宇的Talk已经准时在TechBeat人工智能社区开播&#xff01; 他与大家分享的主题是: “适用于高自由度机器人的运动控制器”&#xff0c;向大家系统地介绍了如何通…

threejs 光影投射-与场景进行交互(六)

效果 场景中有三个立方体,三种颜色.点击变成红色,再点恢复自身原有颜色 代码 import ./style.css import * as THREE from three import { OrbitControls } from three/examples/jsm/controls/OrbitControls.js import { log } from three/examples/jsm/nodes/Nodes.js//…

开发中遇到的错误 - @SpringBootTest 注解爆红

我在使用 SpringBootTest 注解的时候爆红了&#xff0c;ait 回车也导不了包&#xff0c;后面发现是因为没有加依赖&#xff1a; <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId>…