2015-04-02 82 views
-1

如果没有从MySQL返回的行,我想隐藏类“产品列表”中的div。 我与DIV代码如下:隐藏div,如果没有从while循环返回的行PHP

$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> 
+0

http://php.net/manual/en/control-structures.if.php – Scuzzy 2015-04-02 13:42:43

+0

我已经尝试了条件,但如何使用while循环中声明的变量? – 2015-04-02 13:46:21

+0

然后告诉我们'if/else' – 2015-04-02 13:48:52

回答

2

只需使用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 } ?> 
+0

感谢您的帮助!但它仍然显示div – 2015-04-02 13:53:04

+0

谢谢!工作.. – 2015-04-02 14:26:10

+0

@MayurPatel请参阅http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – 2015-04-02 14:42:56