【发布时间】:2017-01-18 17:54:50
【问题描述】:
正如标题所说,我想从我的分页系统中隐藏一些页面。因为它选择的表很大,所以它创建了数千个数字。
所以我希望它像 第一页 1 2 3 4 ... 最后一页
如果我点击页面,它应该会显示这个 首页 1 2 3 4 5 6 7 8 ... 最后一页
如果我点击第 8 页,它应该会显示这个 第一页 ... 4 5 6 7 8 9 10 11 ... 最后一页
这是我的php分页脚本
<!DOCTYPE html>
<html>
<head>
<title>PHP Pagination</title>
</head>
<body>
<table align="center" border="2" cellpadding="3">
<tr>
<th>Name</th>
<th>quality</th>
<th>RequiredLevel</th>
</tr>
<?php
$con = mysqli_connect('127.0.0.1', 'root', 'ascent', 'world');
$per_page=500;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}else{
$page=1;
}
$start_from = ($page-1) * $per_page;
$stmt = $con->prepare("SELECT name, Quality, RequiredLevel FROM item_template LIMIT $start_from, $per_page");
$stmt->execute();
$stmt->bind_result($name, $quality, $reqlvl);
$stmt->store_result();
while($stmt->fetch()) {
if($stmt->num_rows > 0) {
echo "<tr align='center'>";
echo "<td>$name</td>";
echo "<td>$quality</td>";
echo "<td>$reqlvl</td>";
echo "</tr>";
}
}
?>
</table>
<div>
<?php
$stmt = $con->prepare("SELECT * FROM item_template");
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0) {
$total_records = $stmt->num_rows;
$total_pages = ceil($total_records / $per_page);
echo "<center><a href='index.php?page=1'>First Page</a>";
for ($i=1; $i<=$total_pages; $i++) {
if($page < 4)
echo "<a href='index.php?page=$i'>$i</a> ";
}
echo "<a href='index.php?page=$total_pages'>Last Page</a></center> ";
}
?>
</div>
</body>
</html>
【问题讨论】:
-
如果你不会渲染它,你不应该查询整个表,使用偏移量/限制只查询记录的子集。
-
你可以从codeigniter分页库github.com/bcit-ci/CodeIgniter/blob/…获取想法
-
我在其他查询中使用了限制。这段代码只是我的分页系统。所以它只会为我的页面创建链接。也许我应该只发布整个脚本?
标签: php mysqli pagination hide