Table 选择时分页不会转到不同的页面

Table pagination not going to a different page when selected

我将分页添加到我创建的 table 中,此后我一直无法让它正常工作。

table 限制有效,仅此而已。如果我 select "First, Last or the page number" 它只是重新加载页面,但新页面不显示。

如果我将页面限制设置为较低的数字,如 5 和 select 'Last Page',当页面加载时它显示 =1,就像它不知道还有其他页面一样。

<?php
$con = mysqli_connect("localhost","root","","bfb");
$per_page=20;

    if(isset($_POST["page"])) {

    $page = $_POST["page"];
    }
    else {
        $page = 1;
    }

//Page will start from 0 and multiply per page
    $start_from = ($page-1)*$per_page;

//Selecting the data from the table but with limit
    $query = "SELECT * FROM orders LIMIT $start_from, $per_page";
    $result = mysqli_query($con, $query);
?>

<table class="tableproduct">
    <tr>
        <th class="thproduct">Order ID</th>
        <th class="thproduct">Customer Name</th>
        <th class="thproduct">Product</th>
        <th class="thproduct">Total Price</th>
        <th class="thproduct"></th>
        <th class="thproduct"></th>
    </tr>
  <?php
      if( $result ){    
         while($row = mysqli_fetch_assoc($result)) :
   ?>
 <form method="POST" action="orderhistory.php">
    <tr>
        <td class="tdproduct"><?php echo $row['order_id']; ?> </td>
        <td class="tdproduct"><?php echo $row['customer_name']; ?> </td> 
        <td class="tdproduct"><?php echo $row['product_name']; ?> </td>
        <td class="tdproduct"><?php echo $row['total_price']; ?> </td>
        <td class="tdproduct"><a href='editorderhistory.php?id=<?php echo $row['id']; ?>'>EDIT</a></td>
            <input type="hidden" name="product_id" value="<? echo $row['id']; ?>"/>
        <td class="tdproduct"><input name="delete" type="submit" value="DELETE "/></td>
    </tr>
</form>
<?php   
    endwhile; 
    }
?>
                    </table>
<?php
 //Count the total records
if ($result != false) {
$total_records = mysqli_num_rows($result);
    if ($total_records == 0) {
        echo 'No results founds.';
    }
}

//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records /$per_page);
?>
<span class="spandarkblue">
<?php
//Going to first page
echo "<center><a href='orderhistory.php?page=1'>".'First Page'."</a>   ";

for ($i=1; $i<=$total_pages; $i++) {

echo "<a href='orderhistory.php?page=".$i."'>".$i."</a>   ";
};
// Going to last page
echo "<a href='orderhistory.php?page=$total_pages'>".'Last Page'."</a></center>";
?>

有什么想法吗?

在您的代码中发现以下问题。

1) 您正在尝试使用 POST 从 URL 获取页面值,因为您需要 GET 方法从 URl 获取值。使用 POST 返回空值,因此 $page 值始终设置为 1

所以使用 $_GET["page"] 而不是 $_POST["page"]

2) 您正在通过考虑正在执行的查询的行数来准备分页。但是当你添加 limits 时,你的 $total_records 总是等于或小于 $per_page 值,导致只有一个页码。

使用以下代码生成 pazination

$query1 = "SELECT count(*) as totalRecords FROM orders";
$result1 = mysqli_query($con, $query1);
if ($result1) {
      $row1 = mysqli_fetch_assoc($result1);
      $total_records = $row1['totalRecords'];
     if ($total_records == 0) {
         echo 'No results founds.';
      }
}

而不是下面的代码

if ($result != false) {
   $total_records = mysqli_num_rows($result);
   if ($total_records == 0) {
      echo 'No results founds.';
   }
}

希望对你有帮助。

您需要获取 所有 数据以了解您的记录总数,然后在 for() 循环中仅显示所需的数据,因此删除 LIMIT $start, $per_page 从您的查询中,在此节点中您将始终有 20 个或更少的结果

在您的 while() 中将数据保存在数组中,如下所示:

$index = 0;
while($row = mysqli_fetch_assoc($result)) {
    $ordID[$index] = $row["order_id"];
    // The rest of your data...
    $index++; // Increment the index
}

然后你只显示想要的数据:

for($i = (($page-1)*$perPage); $i < min(($page*$perPage), $total); $i++) {
    echo $orderID[$i];
    // And so on...
}

min()函数returns两个变量之间的最小值,这样在最后一页,如果你有17个产品而不是20个你不会有错误。