php - 根据变量的值隐藏表格的特定行
问题描述
我正在使用 php 中的发票。我只想在发票中有任何到期时才显示到期部分,否则它应该保持隐藏。在我的 printOrder.php 中,代码如下:
这是 printOrder.php 文件的总代码:
<?php
require_once 'core.php';
$orderId = $_POST['orderId'];
$sql = "SELECT order_date, client_name, client_address, client_contact, sub_total, vat, total_amount, discount, grand_total, paid, due, invoice_no FROM orders WHERE order_id = $orderId";
$orderResult = $connect->query($sql);
$orderData = $orderResult->fetch_array();
$orderDate = $orderData[0];
$clientName = $orderData[1];
$clientAddress = $orderData[2];
$clientContact = $orderData[3];
$subTotal = $orderData[4];
$vat = $orderData[5];
$totalAmount = $orderData[6];
$discount = $orderData[7];
$grandTotal = $orderData[8];
$paid = $orderData[9];
$due = $orderData[10];
$invoiceNo = $orderData[11];
$orderItemSql = "SELECT order_item.product_id, order_item.rate, order_item.quantity, order_item.total,
product.product_name, order_item.categories_name, order_item.subcategory_name, order_item.brand_name FROM order_item
INNER JOIN product ON order_item.product_id = product.product_id
WHERE order_item.order_id = $orderId";
$orderItemResult = $connect->query($orderItemSql);
$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
?>
<table border="0" cellpadding="5" style="width:100%;font-size:10px;border:0px solid black;text-align:center;margin-bottom:15px;">
<thead>
<tr>
<th colspan="2">
<img style="float:left" src="assests/images/logo.jpg" width="80"/>
</th>
<th></th>
<th colspan="5">
<div>
<div style="font-size:14px; text-align:right; font-weight:bold">Sylhet Gym Supplements & Online Store</div>
<div style="font-size:8px; text-align:right; font-weight:normal; line-height:10px; padding:4px 0 4px 30px">One of the best UK and USA suppliers in Gym Food Supplements to support A healthy lifestyle. We supply To retailer Bodybuilding.com supplement Online Market, Sylhet, Bangladesh</div>
<div style="font-size:9px; text-align:right; font-weight:normal">R.B Complex (Level-4), East Zindabazar, Sylhet</div>
<div style="font-size:9px; text-align:right; font-weight:normal; line-height:18px">Cell: 01725-275252</div>
</div>
</th>
</tr>
<tr>
<th colspan="8" style="text-align:left;margin-left:0;padding-left:0"><span style="font-size:10px;background-color:#000;color:#fff;padding:2px 15px 4px">Invoice Details</span></th>
</tr>
<tr>
<th colspan="4" bgcolor="#f4f4f4" style="padding:5px 15px; text-align:left"><b>Mr. <?php echo $clientName; ?><b><br><p style="font-weight:normal;line-height:14px;"><?php echo $clientAddress; ?><br><?php echo $clientContact; ?></p></th>
<th colspan="4" bgcolor="#f4f4f4" style="line-height:14px;padding:5px 15px; font-weight:normal;text-align:right">Invoice No : <b><?php echo $invoiceNo; ?></b><br>Invoice Date : <b><?php echo $orderDate; ?></b><br><br>Paid Amount : Tk <span style="font-size:12px;font-weight:bold"><?php echo $paid; ?></span> Only</th>
</tr>
<tr>
<th colspan="8"></th>
</tr>
</thead>
<tbody>
<tr>
<th bgcolor="#f4f4f4">Sl.</th>
<th bgcolor="#f4f4f4" colspan="4" style="text-align:center">Product Description</th>
<th bgcolor="#f4f4f4" style="width:80px">Rate</th>
<th bgcolor="#f4f4f4" style="width:80px">Qty</th>
<th bgcolor="#f4f4f4" style="text-align:right;width:80px">Total</th>
</tr>
<?php
$x = 1;
while($row = $orderItemResult->fetch_array()) {
?>
<tr>
<th style="font-size:9px; font-weight:normal;"><?php echo $x; ?></th>
<th colspan="2" style="font-size:10px; font-weight:bold; text-transform:uppercase; text-align:left"><?php echo $row[4]; ?></th>
<th colspan="2" style="font-size:9px; font-weight:normal; text-align:left">A Product of <b><?php echo $row[7]; ?></b><br>It\'s Flavor is <b><?php echo $row[5]; ?></b> (<?php echo $row[6]; ?>)</th>
<th style="font-size:9px; font-weight:normal;"><?php echo $row[1]; ?></th>
<th style="font-size:9px; font-weight:normal;"><?php echo $row[2]; ?></th>
<th style="font-size:9px; font-weight:normal;text-align:right"><?php echo $row[3]; ?></th>
</tr>
<?php
$x++;
} // /while
?>
<tr>
<th></th>
</tr>
<tr>
<th colspan="8" style="border-top:1px solid #eee"></th>
</tr>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th colspan="3" style="font-size:9px; font-weight:bold; line-height:8px; text-align:right">Total Amount</th>
<th style="font-size:9px; font-weight:normal; line-height:8px; text-align:right"><?php echo $totalAmount; ?></th>
</tr>
<tr>
<?php
if ($due > 0) {
echo '<th colspan="3" style="border:1px solid #ccc">Condition : '.$due.' Tk will be T.T</th>';
} else {
echo '<th colspan="3"></th>';
}
?>
<th></th>
<th colspan="3" style="font-size:9px; font-weight:bold; line-height:8px; text-align:right">Discount</th>
<th style="font-size:9px; font-weight:normal; line-height:8px; text-align:right"><?php echo $discount; ?></th>
</tr>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th colspan="3" style="font-size:9px; font-weight:bold; line-height:8px; text-align:right">Grand Total</th>
<th style="font-size:9px; font-weight:normal; line-height:8px; text-align:right"><?php echo $grandTotal; ?></th>
</tr>
<tr>
<th colspan="8" bgcolor="#f4f4f4" style="text-align:right;text-transform:capitalize">Amount (In Words) : <?php echo $f->format($grandTotal); ?> Taka Only.</th>
</tr>
</tbody>
<tfoot>
<tr>
<th colspan="8" style="position:absolute; width:100%; bottom:50px;"><span style="float:left;border-top:1px solid #ccc"> Customer Signature </span><span style="float:right;border-top:1px solid #ccc;margin-right:20px"> For Sylhet Gym Supplements </span></th>
</tr>
<tr>
<th colspan="8" style="background-color: #eee; color: #555; position:absolute; width:100%; height:9px; line-height:9px; bottom:0; font-size: 7px">Powered By 7CoderIT. For assistance call on 01744-123456</th>
</tr>
</tfoot>
</table>
<?php
$connect->close();
?>
我改变了一些事情来实现我的愿望。之后代码如上所示。
解决方案
只需检查正确的到期值,我假设您得到到期金额 0 或 null 或某个值
<?php
if($due>0&&$due!=""){
?>
<tr>
<th colspan="2"><?php echo $due; ?></th>
</tr>
<?php
}
?>
如果您的 $due 变量不能帮助您做出决定,请检查决定到期部分的变量,将到期部分包含在 if 条件中,如下所示(通用 PHP)
if(condition){ //this tr will be printed only if your condition will be true.
echo '<tr>.....</tr>';
}
更新:在表单个PHP 文件中显示条件行的示例
<?php
$total_amount = 100;
$due = 20;
$data = array();
?>
<table>
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php
foreach($data as $d){
?>
<tr>
<td></td>
<td></td>
</tr>
<?php
}
?>
<tr>
<th colspan="2"><?php echo $total_amount ?></th>
</tr>
<?php
if($due>0&&$due!=""){
?>
<tr>
<th colspan="2"><?php echo $due; ?></th>
</tr>
<?php
}
?>
</tbody>
</table>
使用 td 和 th 的 colspan 属性来匹配表中的列数。
推荐阅读
- python - python在字典中附加一个带有字典的数组
- dictionary - 如何在 Map 中找到最小元素并返回一个元组(键,最小元素)?
- excel - 为什么当我在 excel VBA 上运行这个宏时什么都没有发生?我错过了什么吗?
- html - 为什么我的第一行的高度大于其他行 HTML
- python - 如何在单个 Django 视图中跨多个模型进行查询?
- c++ - 我的程序无法正常工作,我的函数不断抛出分段错误
- python - Python从链接读取计时器值
- android - Google 重置密钥证书后,Firebase Google 登录不起作用
- r - spatstat 拟合混合模型中估计的置信区间
- laravel - 找不到 Laravel 类控制器,因为路由非常区分大小写