【问题标题】:pagination in php not workingphp中的分页不起作用
【发布时间】:2017-11-10 14:11:45
【问题描述】:

我是 php 新手。我必须在 php 中显示带有分页的 mysql 记录。在我的代码中,记录正在正确获取,但分页不起作用。如果我单击任何页面,它会显示页码,它只显示完整记录。

display.php

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<html>

<body>
<?php
    $conn = new mysqli("localhost:3306", "root", "", "task");

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    $per_page= 30;
    $sql="SELECT * FROM data";
    $result= $conn->query($sql);


    if(isset($_GET['page']) && is_numeric($_GET['page'])){
        $page=$_GET['page'];
    }
    else{
        $page = 1;
    }
    $start =($page - 1) * $per_page;
    $sql=$sql." LIMIT $start,$per_page";
    $query2 = $conn->query($sql);
?>
<table class="table table-bordered table-striped">
<thead>  
    <tr>  
    <th>Ticket ID</th>  
    <th>Tocken Number</th>
    <th>Status</th>  
    </tr>  
<thead>  
<tbody>  
<?php  
while ($row = $result->fetch_assoc()) {  
?>  
            <tr>  
            <td><?php echo $row["ticket_id"]; ?></td>  
            <td><?php echo $row["tocken_no"]?></td>
            <td><?php echo $row["status"] ?></td>   
            </tr>  
<?php  
};  
?>  
</tbody>  
</table>  
<?php
$row_cnt = $result->num_rows;
    $pages = ceil($row_cnt/$per_page);

$page_link="<div class='pagination'>";

for($i=1; $i<=$pages; $i++)
{
$page_link .= "<a href='display.php?page=".$i."'>".$i."</a>";  
}
echo $page_link . "</div>";
?>

</body>
</html>

提前致谢。

【问题讨论】:

    标签: php mysql pagination


    【解决方案1】:

    你错过了代码中的一件事,从

    更改代码
    <?php  while ($row = $result->fetch_assoc()) {  ?>
    

    <?php  while ($row = $query2->fetch_assoc()) {   ?> // $query2 you should pass the variable of limit query
    

    这应该可行!

    【讨论】:

      猜你喜欢
      • 2017-10-10
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 2014-08-16
      相关资源
      最近更新 更多