实现效果
准备工作
创建数据表和导入测试数据
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实现分页功能一(跳转方式) 文章来源:https://www.toymoban.com/news/detail-730267.html
二、使用ajax实现php分页功能文章来源地址https://www.toymoban.com/news/detail-730267.html
一、php实现分页功能一(跳转方式)
<?php
// 连接数据库
$conn = new mysqli('localhost', 'root', 'root', 'aaa');
// 检查连接是否成功
if ($conn->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);
?>
<!DOCTYPE html>
<html>
<head>
<title>分页功能示例</title>
</head>
<body>
<h1>用户列表</h1>
<table>
<tr>
<th>ID</th>
<th>用户名</th>
<th>邮箱</th>
</tr>
<?php while ($row = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['email']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<div>
<?php if ($current_page > 1): ?>
<a href="?page=1">首页</a>
<a href="?page=<?php echo $current_page - 1; ?>">上一页</a>
<?php endif; ?>
当前页:<?php echo $current_page; ?> / <?php echo $total_pages; ?>
<?php if ($current_page < $total_pages): ?>
<a href="?page=<?php echo $current_page + 1; ?>">下一页</a>
<a href="?page=<?php echo $total_pages; ?>">最后一页</a>
<?php endif; ?>
</div>
</body>
</html>
二、使用ajax实现php分页功能
<?php
// 连接数据库
$conn = new mysqli('localhost', 'root', 'root', 'aaa');
// 检查连接是否成功
if ($conn->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);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>分页功能示例(Ajax方式)</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
// 初始化加载第一页数据
loadPageData(1);
// 点击分页链接时,发送Ajax请求加载对应页数的数据
$(document).on('click', '.pagination-link', function(e) {
e.preventDefault();
var page = $(this).data('page');
loadPageData(page);
});
function loadPageData(page) {
$.ajax({
url: 'test22.php',
type: 'GET',
data: { page: page },
dataType: 'json',
success: function(response) {
// 清空表格数据和分页链接
$('#user-table tbody').empty();
$('.pagination').empty();
// 更新表格数据
$.each(response.data, function(index, user) {
var row = '<tr>' +
'<td>' + user.id + '</td>' +
'<td>' + user.username + '</td>' +
'<td>' + user.email + '</td>' +
'</tr>';
$('#user-table tbody').append(row);
});
// 更新分页链接
var pagination = '';
if (response.current_page > 1) {
pagination += ' <a href="#" class="pagination-link" data-page="1">首页</a>';
pagination += ' <a href="#" class="pagination-link" data-page="' + (response.current_page - 1) + '">上一页</a>';
}
pagination += '当前页:' + response.current_page + ' / ' + response.total_pages;
if (response.current_page < response.total_pages) {
pagination += '> <a href="#" class="pagination-link" data-page="' + (response.current_page + 1) + '">下一页</a>';
pagination += ' <a href="#" class="pagination-link" data-page="' + response.total_pages + '">最后一页</a>';
}
$('.pagination').html(pagination);
},
error: function() {
alert('加载数据失败');
}
});
}
});
</script>
</head>
<body>
<h1>用户列表</h1>
<table id="user-table">
<tr>
<th>ID</th>
<th>用户名</th>
<th>邮箱</th>
</tr>
<tbody></tbody>
</table>
<div class="pagination"></div>
</body>
</html>
到了这里,关于php实现分页功能跳转和ajax方式实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!