最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

Iam getting the records in tabular format and displaying the data in front end but if there is no data i need to hide total table including table headings.here is my code.

<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='ine_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>

I need to hide these total table if there is no data in database and should display the form.

Iam getting the records in tabular format and displaying the data in front end but if there is no data i need to hide total table including table headings.here is my code.

<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='ine_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>

I need to hide these total table if there is no data in database and should display the form.

Share edited Feb 9, 2016 at 6:48 user5891511 asked Feb 9, 2016 at 5:31 user5891511user5891511 713 silver badges9 bronze badges 2
  • 3 Why not place the entire table in a if ($result) in php? – Tah Commented Feb 9, 2016 at 5:35
  • Hi Can anyone tell me if the table data is empty i need to display a form how can we show a form if the table data is empty.my form starts once the tabel s pleted as i updated my ode – user5891511 Commented Feb 9, 2016 at 6:46
Add a ment  | 

7 Answers 7

Reset to default 2
<?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='ine_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>

EDIT :

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>

EDIT 2:

<table  style="width:87%;<?=$dispNone?>">

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

You can use if else condition to show and hide total table like below:

<?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='ine_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 } ?>

You can do it using if else statement.

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='ine_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   } ?>

You can use mysql_num_rows() for hiding the <table> it will check no's rows before fetching the records as:

<?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='ine_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>
<?
}
?>

Side Note:

Please use mysqli_* or PDO extension because mysql_* is deprecated and close in PHP 7.

Jquery solution:

$(document).ready(function(){
  if ($("#mytable td").length == 0){
    $("#mytable").hide();  
  }
});
<script src="https://ajax.googleapis./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>

If you need to hide your entire table you can just use this PHP code in your HTML table tag and it should hide your entire table if your query does not return any results.

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

So basically your entire code to hide the table would be like below:

<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='ine_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>

发布评论

评论列表(0)

  1. 暂无评论