实现效果
准备工作
创建数据表和导入测试数据
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(30) DEFAULT NULL COMMENT '账号',
`email` varchar(30) DEFAULT NULL COMMENT '密码',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `users` VALUES ('1', 'admin', 'admin@qq.com');
INSERT INTO `users` VALUES ('2', 'xiaoming', '123456@qq.com');
INSERT INTO `users` VALUES ('3', 'xiaoming1', '123456@qq.com');
INSERT INTO `users` VALUES ('4', 'xiaoming2', '123456@qq.com');
INSERT INTO `users` VALUES ('5', 'xiaoming3', '123456@wy.com');
INSERT INTO `users` VALUES ('6', 'xiaoming4', '123456@wy.com');
INSERT INTO `users` VALUES ('7', 'xiaoming5', '123456@wy.com');
INSERT INTO `users` VALUES ('8', 'xiaoming6', '123456@wy.com');
INSERT INTO `users` VALUES ('9', 'xiaoming7', '123456@wy.com');
目录
一、php实现分页功能一(跳转方式)
connect_error) { die("连接失败:" . $conn->connect_error);}// 每页显示的记录数$records_per_page = 3;// 获取当前页数,默认为第一页if (isset($_GET['page']) && is_numeric($_GET['page'])) { $current_page = $_GET['page'];} else { $current_page = 1;}// 计算总记录数$query = "SELECT COUNT(*) AS total_records FROM users";$result = $conn->query($query);$row = $result->fetch_assoc();$total_records = $row['total_records'];// 计算总页数$total_pages = ceil($total_records / $records_per_page);// 计算偏移量$offset = ($current_page - 1) * $records_per_page;// 查询数据$query = "SELECT * FROM users LIMIT $offset, $records_per_page";$result = $conn->query($query);?> 分页功能示例 ID 用户名 邮箱 fetch_assoc()): ?>
二、使用ajax实现php分页功能
connect_error) { die("连接失败:" . $conn->connect_error);}// 每页显示的记录数$records_per_page = 3;// 获取当前页数,默认为第一页if (isset($_GET['page']) && is_numeric($_GET['page'])) { $current_page = $_GET['page'];} else { $current_page = 1;}// 计算总记录数$query = "SELECT COUNT(*) AS total_records FROM users";$result = $conn->query($query);$row = $result->fetch_assoc();$total_records = $row['total_records'];// 计算总页数$total_pages = ceil($total_records / $records_per_page);// 计算偏移量$offset = ($current_page - 1) * $records_per_page;// 查询数据$query = "SELECT * FROM users LIMIT $offset, $records_per_page";$result = $conn->query($query);// 构建返回的JSON数据$data = array();while ($row = $result->fetch_assoc()) { $data[] = $row;}$response = array( 'data' => $data, 'current_page' =>(int) $current_page, 'total_pages' => $total_pages);echo json_encode($response);
分页功能示例(Ajax方式) ID 用户名 邮箱
来源地址:https://blog.csdn.net/weixin_39934453/article/details/133272589