使用while循环在html表中显示php数据

时间:2019-01-30 21:21:03

标签: php html

每次使用while循环从数据库中回显信息时,第一个结果仅显示在一行中,而其余结果作为原始文本打印在表外。

<table class="table table-striped">
<thead>
        <tr class="bg-info">
        <th>Id</th>
        <th>Customer</th>
        <th>Order</th>
        <th>Title</th>
        <th>Quantity</th>
        <th>Total</th>
        <th>Paid</th>
        <th>Created Time</th>
        <th>Updated Time</th>
        <th>Update Order</th>
        <th>Delete Order</th>
    </tr>
</thead>
    <tbody>
       <?php
        if($sql->num_rows > 0){
       while($data = $sql->fetch_array()):
        ?>
        <tr>
        <td><?php echo $database->escape_value($data['custom_id']); ?></td>
        <td><?php echo $database->escape_value($data['order_id']); ?></td>
        <td><?php echo $database->escape_value($data['title']); ?></td>
        <td><?php echo $database->escape_value($data['quantity']); ?></td>
        <td><?php echo $database->escape_value($data['total']); ?></td>
        <td><?php echo $database->escape_value($data['paid']); ?></td>
        <td><?php echo $database->escape_value($data['created_at']); ?></td>
        <td><?php echo $database->escape_value($data['updated_at']); ?></td>
        <td><a href="update_order.php?id=<?php echo $database->escape_value($data['id']); ?>">Update Order</a></td>
        <td><a href="delete_order.php?id=<?php echo $database->escape_value($data['id']); ?>" onClick="return confirm('Are you sure you want to delete this order?'); ">Delete</a></td>
        </tr>
    </tbody>
</table>
<?php endwhile; }else 
    echo "<div class='btn bg-danger'>search not found</div>";
?>

伙计们,只有第一个结果会正确地显示在表格中,我希望所有数据都显示在表格中。

1 个答案:

答案 0 :(得分:0)

您正在关闭循环内的表。重要的是,您只能在一段时间内保留该行。

然后请小心正确关闭所有括号。

<thead>
        <tr class="bg-info">
        <th>Id</th>
        <th>Customer</th>
        <th>Order</th>
        <th>Title</th>
        <th>Quantity</th>
        <th>Total</th>
        <th>Paid</th>
        <th>Created Time</th>
        <th>Updated Time</th>
        <th>Update Order</th>
        <th>Delete Order</th>
    </tr>
</thead>
    <tbody>
       <?php
        if($sql->num_rows > 0){
            while($data = $sql->fetch_array()){
        ?>
        <tr>
        <td><?php echo $database->escape_value($data['custom_id']); ?></td>
        <td><?php echo $database->escape_value($data['order_id']); ?></td>
        <td><?php echo $database->escape_value($data['title']); ?></td>
        <td><?php echo $database->escape_value($data['quantity']); ?></td>
        <td><?php echo $database->escape_value($data['total']); ?></td>
        <td><?php echo $database->escape_value($data['paid']); ?></td>
        <td><?php echo $database->escape_value($data['created_at']); ?></td>
        <td><?php echo $database->escape_value($data['updated_at']); ?></td>
        <td><a href="update_order.php?id=<?php echo $database->escape_value($data['id']); ?>">Update Order</a></td>
        <td><a href="delete_order.php?id=<?php echo $database->escape_value($data['id']); ?>" onClick="return confirm('Are you sure you want to delete this order?'); ">Delete</a></td>
        </tr>
        <?php
            }
        }
        ?>
    </tbody>
</table>
<?php 
if($sql->num_rows == 0){    
    echo "<div class='btn bg-danger'>search not found</div>";
}
?>