正如标题所说,我想从我的分页系统中隐藏一些页面。因为它从中选择的表非常大,所以它创建了数千个数字。
所以我想要它就像 第一页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>
答案 0 :(得分:3)
您的分页问题的解决方案是这样的:
使用超全球$_GET
,
// current page
$current_page = isset($_GET['page']) ? $_GET['page'] : 1;
查找页面的超集范围,例如1-10或1-20等。例如,如果$total_pages = 30
,则此超集范围将为1-30。此步骤的代码如下:
// superset range of pages
$superset_range = range(1, $total_pages);
找到要显示的子页范围,例如1-7或3-9等。例如,如果$total_pages = 30
则此子集范围为1-7或3-9,或者6-12等,它可以是1到30之间的任何连续七页。此外,必要时调整此范围。此步骤的代码如下:
// subset range of pages to display
$subset_range = range($current_page - 3, $current_page + 3);
// adjust the range(if required)
foreach($subset_range as $p){
if($p < 1){
array_shift($subset_range);
if(in_array($subset_range[count($subset_range) - 1] + 1, $superset_range)){
$subset_range[] = $subset_range[count($subset_range) - 1] + 1;
}
}elseif($p > $total_pages){
array_pop($subset_range);
if(in_array($subset_range[0] - 1, $superset_range)){
array_unshift($subset_range, $subset_range[0] - 1);
}
}
}
最后,相应地显示分页链接和点。此步骤的代码如下:
// display intermediate pagination links
if($subset_range[0] > $superset_range[0]){
echo " ... ";
}
foreach($subset_range as $p){
echo "<a href='index.php?page=$p'>$p</a>";
}
if($subset_range[count($subset_range) - 1] < $superset_range[count($superset_range) - 1]){
echo " ... ";
}
所以,这是完整代码:
// your code
$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);
if($total_records > $per_page){
echo "<center><a href='index.php?page=1'>First Page</a> ";
// current page
$current_page = isset($_GET['page']) ? $_GET['page'] : 1;
// Superset range of pages
$superset_range = range(1, $total_pages);
// subset range of pages to display
$subset_range = range($current_page - 3, $current_page + 3);
// adjust the range(if required)
foreach($subset_range as $p){
if($p < 1){
array_shift($subset_range);
if(in_array($subset_range[count($subset_range) - 1] + 1, $superset_range)){
$subset_range[] = $subset_range[count($subset_range) - 1] + 1;
}
}elseif($p > $total_pages){
array_pop($subset_range);
if(in_array($subset_range[0] - 1, $superset_range)){
array_unshift($subset_range, $subset_range[0] - 1);
}
}
}
// display intermediate pagination links
if($subset_range[0] > $superset_range[0]){
echo " ... ";
}
foreach($subset_range as $p){
echo "<a href='index.php?page=$p'>$p</a>";
}
if($subset_range[count($subset_range) - 1] < $superset_range[count($superset_range) - 1]){
echo " ... ";
}
echo " <a href='index.php?page=$total_pages'>Last Page</a></center> ";
}
}