我以表格格式获取记录并在前端显示数据,但如果没有数据,我需要隐藏包括表格标题在内的总表格。这是我的代码。
<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>
如果数据库中没有数据并且应该显示表单,我需要隐藏这些总表。
答案 0 :(得分:2)
您可以使用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 } ?>
答案 1 :(得分:2)
您可以使用if else
statement来完成此操作。
if(!empty(mysql_fetch_array($result))){
//Here your datatable
}else{
//echo 'No Data';
}
答案 2 :(得分: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='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?>">
答案 3 :(得分:1)
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 } ?>
答案 4 :(得分:1)
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>
答案 5 :(得分:1)
您可以使用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中关闭。
答案 6 :(得分:0)
如果你需要隐藏整个表,你可以使用它 HTML表格标签中的PHP代码,如果您的查询没有返回任何结果,它应该隐藏整个表格。
<?php echo (mysql_num_rows($result)>0)?'visible':'hidden'; ?>
&#13;
所以基本上你隐藏表的整个代码如下所示:
<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>
&#13;