![](/img/trans.png)
[英]how to hide data table show entries in javascript and jquery?
[英]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>
如果數據庫中沒有數據,我需要隱藏這些總計表,並應顯示表格。
您可以使用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';
}
<?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?>">
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 } ?>
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>
您可以使用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中關閉。
如果您需要隱藏整個表,則可以在HTML table標記中使用此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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.