gpt4 book ai didi

php - 使用sql从php分页系统中隐藏一些页面

转载 作者:行者123 更新时间:2023-12-03 23:52:07 27 4
gpt4 key购买 nike

正如标题所说,我想从我的分页系统中隐藏一些页面。因为它从中选择的表很大,所以它创建了数千个数字。

所以我希望它是这样的第一页 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>

最佳答案

分页问题的解决方案是这样的:

  • 使用超全局$_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 " ...&nbsp;";
    }
    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 "&nbsp;... ";
    }

所以,这是完整的代码:

// 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 " ...&nbsp;";
}
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 "&nbsp;... ";
}

echo " <a href='index.php?page=$total_pages'>Last Page</a></center> ";
}
}

关于php - 使用sql从php分页系统中隐藏一些页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39430918/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com