简体   繁体   中英

Hide specific row of a table based on the value of a Variable

I am working with an Invoice in php. I want to show the DUE part only if there is any due in the invoice otherwise it should remain hide. In my printOrder.php the codes are given below:

Here is the total code of printOrder.php file:

    <?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 :&nbsp;&nbsp;<b><?php echo $invoiceNo; ?></b><br>Invoice Date :&nbsp;&nbsp;<b><?php echo $orderDate; ?></b><br><br>Paid Amount :&nbsp;&nbsp;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>&nbsp;(<?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 : &nbsp;&nbsp;'.$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">&nbsp;&nbsp;Customer Signature&nbsp;&nbsp;</span><span style="float:right;border-top:1px solid #ccc;margin-right:20px">&nbsp;&nbsp;For Sylhet Gym Supplements&nbsp;&nbsp;</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();

?>

I have changed few things to accomplish my want. After that the code looks like above.

Simply check the due value right, I assume you get due amount 0 or null or some value

<?php 
    if($due>0&&$due!=""){
    ?>
        <tr>
            <th colspan="2"><?php echo $due; ?></th>
        </tr>
    <?php
    }
?>

If your $due variable does not help you to decide, then check the variable which decides that due part, include the due part inside an if condition, something like following (General PHP)

if(condition){ //this tr will be printed only if your condition will be true.
   echo '<tr>.....</tr>';
}

UPDATE: Example displaying conditional row in a table
Single PHP File

<?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>

Use colspan property of the td and th to match with the number of columns in a table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM