繁体   English   中英

如何使用 PHP 打印表中的所有行而不是一行?

[英]How to Print all the rows in a table instead of one using PHP?

我有一个五行的表,我写了一个 php 代码来打印表中的所有行。 但不幸的是,我错过了一些代码,它实际上触发了获取数据的连续性,而不是从表中获取一行。

我的代码如下:

<?php
$sql2 = "SELECT * FROM useraddmoneyhistory ORDER BY date DESC";
$result2 = $conn->query($sql2); 
if ($result2->num_rows > 0) {
    $index = 0; 
    while($row2 = $result2->fetch_assoc()) { 
        $index++; 
        ?>                    
        <table class="table table-hover" >
            <tr>
                <th>ID</th>
                <th>Mobile</th>
                <th>Earlier Balance</th>
                <th>Amount</th>
                <th>Discount</th>
                <th>Date</th>
            </tr>

            <tr >
                <td><?php echo ($row2["id"]); ?></td>
                <td><?php echo ($row2["mobile"]); ?></td>
                <td><?php echo ($row2["preamount"]); ?></td>
                <td><?php echo ($row2["amount"]); ?></td>
                <td><?php echo ($row2["amountdis"]); ?></td>
                <td><?php echo ($row2["date"]); ?></td>
            </tr>
        </table>

任何帮助是极大的赞赏...

您必须将 html 放入循环中,在代码中whileif语句未关闭。

<table class="table table-hover" >
<tr>
<th>ID</th>
<th>Mobile</th>
<th>Earlier Balance</th>
<th>Amount</th>
<th>Discount</th>
<th>Date</th>
</tr>

<?php
if ($result = $mysqli->query($query)) {

/* fetch associative array */
while ($row = $result->fetch_assoc()) {
    ?> 
    ... HTML 'tr' CODE ...
<?php

} // close while
  $result->free();
} //close if
print "</tr></table>";
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM