【问题标题】:Table pagination, content is not showing in php表格分页,内容未在 php 中显示
【发布时间】:2019-03-31 18:15:31
【问题描述】:

enter image description here问题是我的表格内容没有显示。它正在计数并显示我数据库中的相同行,但没有显示内容。你能帮帮我吗?

index.php

<?php  
include('db.php');

$limit = 2;  
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };  
$start_from = ($page-1) * $limit;  

$sql = "SELECT * FROM sales_order ORDER BY `so_id` ASC LIMIT $start_from, $limit";  
$rs_result = mysqli_query($conn, $sql);  
?>  
<table class="table table-bordered table-striped">  
<thead>  
<tr>  
<th>title</th>  
<th>body</th>  
</tr>  
<thead>  
<tbody>  
<?php  
while ($row = mysqli_fetch_assoc($rs_result)) {  
?>  
            <tr>  
            <td><? echo $row["so_customer"]; ?></td>  
            <td><? echo $row["so_address"]; ?></td>  
            </tr>  
<?php  
};  
?>  
</tbody>  
</table>  
<?php  
$sql = "SELECT COUNT('so_id') FROM sales_order";  
$rs_result = mysqli_query($conn, $sql);  
$row = mysqli_fetch_row($rs_result);  
$total_records = $row[0];  
$total_pages = ceil($total_records / $limit);  
$pagLink = "<div class='pagination'>";  
for ($i=1; $i<=$total_pages; $i++) {  
             $pagLink .= "<a href='index.php?page=".$i."'>".$i."</a>";  
};  
echo $pagLink . "</div>";  ?>

【问题讨论】:

  • 这不会导致您的问题,但 so_id 不应在您的查询中用单引号括起来,它应该用反引号 (`) 或根本没有引号。
  • 我改了,但什么也没发生
  • 您是否检查过查询中的错误?如果将 $rs_result = mysqli_query($conn, $sql); 更改为 $rs_result = mysqli_query($conn, $sql) or die(mysqli_error($conn)); 会发生什么?您还需要更改这两个查询中的引号。
  • 这不是唯一的错误,但这些也是COUNT('so_id'),删除那些或使用刻度。
  • @FunkFortyNiner 你是什么意思?对不起,我刚接触这个。

标签: php html mysqli pagination


【解决方案1】:

您之前编辑的问题几乎没有留下什么问题,尤其是this one,您将单引号从ORDER BY 'so_id' 更改为使用反引号:

ORDER BY `so_id`

有关所谓的“标识符限定符”的更多信息,请参阅以下参考资料:

然后关于&lt;? echo。这些就是所谓的short tags,如果它们无法使用,您需要将它们更改为&lt;?php echo&lt;?=,它们也是如此。

然后你在COUNT('so_id') 中的引用。要么删除它们,要么用反引号替换它们。

COUNT(`so_id`)

最后;一旦页面加载,您就会收到未定义的索引通知:

<td><? echo $row["so_customer"]; ?></td>  
<td><? echo $row["so_address"]; ?></td>  

因此,为了避免/修复这些错误,请将它们替换为以下内容:

<td><?php if(isset($row['so_customer'])){ echo $row["so_customer"]; } ?></td>  
<td><?php if(!empty($row['so_address'])){ echo $row["so_address"]; }?></td>  

启用 PHP 的错误报告和 MySQL 的错误处理,在开发/调试期间会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 2013-06-27
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多