如果 PHP 中的 while 循环没有返回任何行,则隐藏 div
Hide div, if no rows returned from while loop in PHP
如果 MySQL 没有返回任何行,我想用 class "product-list" 隐藏 div。
我的 div 代码如下:
$sql_product = "select * from `product_detail`";
$result_product = $this->db->query($sql_product);
<div class="table-responsive product-list">
<h5>Product Detail</h5>
<table class="table table-striped">
<thead>
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>MRP</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($result_product)){
$product_name = $row['product_name'];
$category_name = $row['category_name'];
$quantity = $row['quantity'];
$product_mrp = $row['mrp'];
$amount = $row['amount'];
?>
<tr>
<th><?php echo $category_name; ?> / <?php echo $product_name; ?></th>
<th><?php echo $quantity; ?></th>
<th><?php echo $product_mrp; ?></th>
<th><?php echo $amount; ?></th>
</tr>
<?php } ?>
</tbody>
</table>
</div>
只需使用IF语句如下:
<?php
$sql_product = "select * from `product_detail`";
$result_product = $this->db->query($sql_product);
if ($result_product->num_rows > 0) { ?>
<div class="table-responsive product-list">
...
</div>
<?php } ?>
如果 MySQL 没有返回任何行,我想用 class "product-list" 隐藏 div。 我的 div 代码如下:
$sql_product = "select * from `product_detail`";
$result_product = $this->db->query($sql_product);
<div class="table-responsive product-list">
<h5>Product Detail</h5>
<table class="table table-striped">
<thead>
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>MRP</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($result_product)){
$product_name = $row['product_name'];
$category_name = $row['category_name'];
$quantity = $row['quantity'];
$product_mrp = $row['mrp'];
$amount = $row['amount'];
?>
<tr>
<th><?php echo $category_name; ?> / <?php echo $product_name; ?></th>
<th><?php echo $quantity; ?></th>
<th><?php echo $product_mrp; ?></th>
<th><?php echo $amount; ?></th>
</tr>
<?php } ?>
</tbody>
</table>
</div>
只需使用IF语句如下:
<?php
$sql_product = "select * from `product_detail`";
$result_product = $this->db->query($sql_product);
if ($result_product->num_rows > 0) { ?>
<div class="table-responsive product-list">
...
</div>
<?php } ?>