Php 选中时,表分页不会转到其他页面

Php 选中时,表分页不会转到其他页面,php,html,mysqli,pagination,Php,Html,Mysqli,Pagination,我在前一段时间创建的表a中添加了分页功能,此后一直无法使其正常工作 表格限制有效,但仅此而已。如果我选择“First、Last或page number”,它只会重新加载页面,但不会显示新页面 如果我将页面限制设置为一个较低的数字,如5,并选择“Last page”,当页面加载时,它会显示=1,就像它不知道还有其他页面一样 <?php $con = mysqli_connect("localhost","root","","bfb"); $per_page=20; if(isset

我在前一段时间创建的表a中添加了分页功能,此后一直无法使其正常工作

表格限制有效,但仅此而已。如果我选择“First、Last或page number”,它只会重新加载页面,但不会显示新页面

如果我将页面限制设置为一个较低的数字,如5,并选择“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获取页面值,其中需要获取从URL获取值的方法。使用POST返回空值,因此
$page
值始终设置为1

因此,使用
$\u GET[“page”]
而不是
$\u POST[“page”]

2) 您正在通过考虑正在执行的查询的行数来准备分页。但是,由于您添加了
限制
,您的
$total\u记录
始终等于或小于$per\u页面值,因此只会产生一个页码

使用以下代码生成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...
}
for($i=($page-1)*$perPage);$i

min()。调试它们的值应该是一个好的开始,即使使用die()是所有分页链接(如first、pageno、last)都指向第1页,或者它们指向url中的不同页码???@SameerK它们都指向第1页当我做了答案的第二部分时,它现在转到与最后一页不同的页面,但它仍然没有显示额外的页面。它现在只显示“第一个”“最后一个”。它也呼应了“未发现结果”。。。我将其设置为每页5个结果,并且我有20多条记录。打成平局时,我在查询中犯了一个错误,我没有考虑行数。我已经编辑了我的答案,请检查。明白了!我很感激。你知道有没有什么方法可以让Ajax起作用,这样我就不必经常重新加载页面了?谢谢,当然你可以使用Ajax。因为我不能在这里分享完整的代码,给你指出一些参考和.Perfect。再次感谢!