如果没有数据,如何隐藏表格并使用jQuery或JavaScript显示表单


how to hide the table if there is no data and show the form using jquery or javascript

我正在获取表格格式的记录并在前端显示数据,但是如果没有数据,我需要隐藏包括表标题在内的总表。

<table style="width:87%;">
    <tr class="spaces">
        <th>S.No.</th>
        <th style="width:20%;">Rent Earned</th>
        <th>House</th>
        <th>Address of Property</th>
    </tr>
    <?php include "rentaldetails.php";
        while($row = mysql_fetch_array($result))
        {?>
    <tr>
        <td>
            <?php echo $row['house_details_id'];?>
        </td>
        <td>
            <?php echo $row['rental_annual_rent'];?>
        </td>
        <td>
            <?php echo $row['rental_tax_paid'];?>
        </td>
        <td>
            <?php echo $row['rental_town'];?>
        </td>
        <td style="width:21%;"><a class="button add" onClick="document.location.href='income_tax.php'">Edit Details</a></td>
        <td style="width:21%;"><a class="buttons delete" href="deleterental.php?id=<?php echo $row['house_details_id'];?>" onclick="return confirm('Are you sure to delete');" class="table-icon delete">Delete Property</a></td>
        <td></td>
    </tr>
    <?php
     }
    ?>
</table>
<form method="POST" action="details.php">
<th>Address Line</th>
<td><input type="text" name="address" value=""  /></td>
<th>Town/City</th>
<td><input type="text" name="city" value="" /></td>
<button type="submit"  class = "medium" style="background-color: #a9014b;float:left;">Save</button>

如果数据库中没有数据,我需要隐藏这些总表,并且应该显示表单。

<?php include "rentaldetails.php";
if(mysql_num_rows($result)== 0){
    $dispNone = "display:none";
}else{
    $dispNone = "display:block";
}
?>

<table style="width:87%;"  style="<?=$dispNone?>">
    <tr class="spaces">
        <th>S.No.</th>
        <th style="width:20%;">Rent Earned</th>
        <th>House</th>
        <th>Address of Property</th>
    </tr>
    <?php include "rentaldetails.php";
        while($row = mysql_fetch_array($result))
        {?>
    <tr>
        <td>
            <?php echo $row['house_details_id'];?>
        </td>
        <td>
            <?php echo $row['rental_annual_rent'];?>
        </td>
        <td>
            <?php echo $row['rental_tax_paid'];?>
        </td>
        <td>
            <?php echo $row['rental_town'];?>
        </td>
        <td style="width:21%;"><a class="button add" onClick="document.location.href='income_tax.php'">Edit Details</a></td>
        <td style="width:21%;"><a class="buttons delete" href="deleterental.php?id=<?php echo $row['house_details_id'];?>" onclick="return confirm('Are you sure to delete');" class="table-icon delete">Delete Property</a></td>
        <td></td>
    </tr>
    <?php
     }
    ?>
</table>

编辑:

.PHP:

if(mysql_num_rows($result)== 0){
    $dispNone = "display:none";
    $dispForm = "display:block";

}else{
    $dispNone = "display:block";
    $dispForm = "display:none";
}
?>

.HTML:

<form style="width:87%;"  style="<?=$dispForm?>">
</form>

编辑2:

<table  style="width:87%;<?=$dispNone?>">
<form  style="width:87%;<?=$dispForm?>">
您可以使用

if else 条件来显示和隐藏总计表如下所示:

<?php 
if(mysqli_num_rows($result) > 0){ ?>
  <table style="width:87%;">
      <tr class="spaces">
          <th>S.No.</th>
          <th style="width:20%;">Rent Earned</th>
          <th>House</th>
          <th>Address of Property</th>
      </tr>
      <?php include "rentaldetails.php";
                  while($row = mysql_fetch_array($result))
                  {?>
      <tr>
          <td>
              <?php echo $row['house_details_id'];?>
          </td>
          <td>
              <?php echo $row['rental_annual_rent'];?>
          </td>
          <td>
              <?php echo $row['rental_tax_paid'];?>
          </td>
          <td>
              <?php echo $row['rental_town'];?>
          </td>
          <td style="width:21%;"><a class="button add" onClick="document.location.href='income_tax.php'">Edit Details</a></td>
          <td style="width:21%;"><a class="buttons delete" href="deleterental.php?id=<?php echo $row['house_details_id'];?>" onclick="return confirm('Are you sure to delete');" class="table-icon delete">Delete Property</a></td>
          <td></td>
      </tr>
      <?php } ?>
  </table>
<?php } ?>
您可以使用

if else语句来执行此操作。

if(!empty(mysql_fetch_array($result))){
   //Here your datatable 
}else{
   //echo 'No Data';
}
Try this:

    <?php 
include "rentaldetails.php";
    if(!empty(mysql_fetch_array($result))){?>
    <table style="width:87%;">
    <tr class="spaces" >
    <th>S.No.</th>
    <th style="width:20%;">Rent Earned</th>
    <th>House</th>
    <th>Address of Property</th>
    </tr>
    <?php 
        while($row = mysql_fetch_array($result))
        {?>
    <tr>
    <td><?php echo $row['house_details_id'];?></td>
    <td><?php echo $row['rental_annual_rent'];?></td>
    <td><?php echo $row['rental_tax_paid'];?></td>
    <td><?php echo $row['rental_town'];?></td>
    <td style="width:21%;"><a class="button add" onClick="document.location.href='income_tax.php'">Edit Details</a></td>
    <td style="width:21%;"><a class="buttons delete" href="deleterental.php?id=<?php echo $row['house_details_id'];?>" onclick="return confirm('Are you sure to delete');" class="table-icon delete" >Delete Property</a></td>
    <td></td>
    </tr>
    <?php
     }
    ?>   
    </table>
 <?php   } ?>
您可以使用

mysql_num_rows()来隐藏<table>它将在获取记录之前检查no的行:

<?php
if(mysql_num_rows($result) > 0){
?>
<table style="width:87%;">
    <tr class="spaces">
        <th>S.No.</th>
        <th style="width:20%;">Rent Earned</th>
        <th>House</th>
        <th>Address of Property</th>
    </tr>
        <?php include "rentaldetails.php";
        while($row = mysql_fetch_array($result))
        {?>
            <tr>
                <td>
                    <?php echo $row['house_details_id'];?>
                </td>
                <td>
                    <?php echo $row['rental_annual_rent'];?>
                </td>
                <td>
                    <?php echo $row['rental_tax_paid'];?>
                </td>
                <td>
                    <?php echo $row['rental_town'];?>
                </td>
                <td style="width:21%;"><a class="button add" onClick="document.location.href='income_tax.php'">Edit Details</a></td>
                <td style="width:21%;"><a class="buttons delete" href="deleterental.php?id=<?php echo $row['house_details_id'];?>" onclick="return confirm('Are you sure to delete');" class="table-icon delete">Delete Property</a></td>
                <td></td>
            </tr>
    <?php
     }
    ?>
</table>
<?
}
?>

旁注:

请使用mysqli_*PDO扩展,因为mysql_*在 PHP 7 中已弃用并关闭。

Jquery 解决方案:

$(document).ready(function(){
  if ($("#mytable td").length == 0){
    $("#mytable").hide();  
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="mytable" style="width:87%;">
    <tr class="spaces">
        <th>S.No.</th>
        <th style="width:20%;">Rent Earned</th>
        <th>House</th>
        <th>Address of Property</th>
    </tr>
</table>

如果您需要隐藏整个表格,您可以使用它HTML 表标记中的 PHP 代码,如果您的查询不返回任何结果,它应该隐藏整个表。

<?php echo (mysql_num_rows($result)>0)?'visible':'hidden'; ?> 

所以基本上你隐藏表格的整个代码如下所示:

<table <?php echo (mysql_num_rows($result)>0)?'visible':'hidden'; ?>  style="width:87%;">
    <tr class="spaces">
        <th>S.No.</th>
        <th style="width:20%;">Rent Earned</th>
        <th>House</th>
        <th>Address of Property</th>
    </tr>
    <?php include "rentaldetails.php";
        while($row = mysql_fetch_array($result))
        {?>
    <tr>
        <td>
            <?php echo $row['house_details_id'];?>
        </td>
        <td>
            <?php echo $row['rental_annual_rent'];?>
        </td>
        <td>
            <?php echo $row['rental_tax_paid'];?>
        </td>
        <td>
            <?php echo $row['rental_town'];?>
        </td>
        <td style="width:21%;"><a class="button add" onClick="document.location.href='income_tax.php'">Edit Details</a></td>
        <td style="width:21%;"><a class="buttons delete" href="deleterental.php?id=<?php echo $row['house_details_id'];?>" onclick="return confirm('Are you sure to delete');" class="table-icon delete">Delete Property</a></td>
        <td></td>
    </tr>
    <?php
     }
    ?>
</table>
<form method="POST" action="details.php">
<th>Address Line</th>
<td><input type="text" name="address" value=""  /></td>
<th>Town/City</th>
<td><input type="text" name="city" value="" /></td>
<button type="submit"  class = "medium" style="background-color: #a9014b;float:left;">Save</button>