【问题标题】:Table pagination not going to a different page when selected选择时表格分页不会转到其他页面
【发布时间】:2015-08-13 02:23:06
【问题描述】:

我在不久前创建的表格中添加了分页功能,但从那以后我一直无法让它正常工作。

餐桌限制有效,但仅此而已。如果我选择“第一个、最后一个或页码”,它只会重新加载页面,但不会显示新页面。

如果我将页面限制设置为 5 之类的低数字并选择“最后一页”,当页面加载时它会显示 =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>";
?>

有什么想法吗?

【问题讨论】:

  • 您的某个变量可能由于某种原因未正确设置。调试它们的值应该是一个好的开始,即使使用 die()
  • 所有的分页链接(比如第一个、pageno、last)是指向第 1 页还是指向 url 中的不同页码??
  • @SameerK 他们都指向第 1 页

标签: php html mysqli pagination


【解决方案1】:

您需要获取所有数据以了解您的记录总数,然后在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
}

然后你 chan 只显示所需的数据:

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

min() 函数返回两个变量之间的最小值,这样在最后一页中,如果您有 17 个产品而不是 20 个产品,则不会出错。

【讨论】:

    【解决方案2】:

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

    1) 您正在尝试使用 POST 从 URL 获取页面值,而您需要使用 GET 方法从 URl 获取值。使用 POST 返回 null 值,所以 $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.';
       }
    }
    

    希望对你有帮助。

    【讨论】:

    • 当我回答你的第二部分时,它现在转到与最后一页不同的页面,但它仍然没有显示其他页面。它现在只显示“第一个”“最后一个”。它还与“未找到结果”相呼应……我将其设置为每页 5 个结果,并且我有超过 20 条记录。
    • 在绑定时,我在没有考虑行数的查询中犯了错误。我已经编辑了我的答案,请检查。
    • 知道了!我很感激。您知道是否有任何方法可以使用 Ajax 进行这项工作,这样我就不必经常重新加载页面?
    • 谢谢,当然你可以使用 ajax。因为我不能在这里分享完整的代码,所以给你一些参考onlinewebapplication.com/pagination-jquery-php-ajax-mysqlthesoftwareguy.in/ajax-pagination-with-php-and-mysql
    • 完美。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    • 2017-09-11
    • 2015-11-22
    相关资源
    最近更新 更多