簡體   English   中英

如何基於php條件隱藏和顯示html表中的列

[英]How to hide and show a column in html table based on a php condition

如何根據php條件在html表中隱藏和顯示列。

$gid = $_SESSION['gid'];

if(gid == NO) 
{
      //display whole table 

}
else{
        // Dont dispaly the certain columns like gst,sgst.
}

桌子看起來像

    <table class="table">
    <thead>
    <tr>
    <th class="border-0 text-uppercase small font-weight-bold">Sl no</th>
    <th class="border-0 text-uppercase small font-weight-bold">Spares Particulars</th>
    <th class="border-0 text-uppercase small font-weight-bold">Amount</th>
    <th class="border-0 text-uppercase small font-weight-bold">Quantity</th>
    <th class="border-0 text-uppercase small font-weight-bold">GST %</th>
    <th class="border-0 text-uppercase small font-weight-bold">GST AMT</th>
    <th class="border-0 text-uppercase small font-weight-bold">CGST %</th>
    <th class="border-0 text-uppercase small font-weight-bold">CGST AMT</th>
    <th class="border-0 text-uppercase small font-weight-bold">SGST %</th>
    <th class="border-0 text-uppercase small font-weight-bold">SGST AMT</th>
    <th class="border-0 text-uppercase small font-weight-bold">Total AMT</th>
     </tr>
        </thead>
        <tbody>
    <tr>
<td><?php echo $i ?></td>
<td><?php echo $sparen ?></td>
<td><?php echo $row9['amt'] ?></td>
<td><?php echo $row9['quant'] ?></td>
<td><?php echo $row9['gstp'] ?></td>
<td><?php echo round($row9['amt']*($row9['gstp']/100)) ?></td>
<td><?php echo round($row9['gstp']/2) ?></td>
<td><?php echo round(($row9['amt']*($row9['gstp']/100))/2) ?></td>
<td><?php echo round($row9['gstp']/2) ?></td>
<td><?php echo round(($row9['amt']*($row9['gstp']/100))/2) ?></td>
<td><?php echo round((($row9['amt']*($row9['gstp']/100))+$row9['amt'])*$row9['quant']) ?></td>

</tr>                            
</tbody>
</table>

如果gid = YES gst,gstamt,cgst,cgstamt,sgst,sgstamt我不希望顯示gst,gstamt,cgst,cgstamt,sgst,sgstamt類的列。 問題是我嘗試使用js,但這無法正常工作。 有人可以幫我解決一個簡單的問題。

您可以為該任務使用PHP三元運算符和CSS顯示屬性

$ShowHide = ($gid == 'NO') ? 'block' : 'none';
<td style="display:<?php echo $ShowHide; ?>;"><?php echo round($row9['gstp']/2) ?></td>

我認為這是完成任務的一種簡單方法。 $ShowHide將根據$gid值將顯示屬性設置為td

<?php
$gid = $_SESSION['gid'];
?>

<table class="table">
    <thead>
    <tr>
        <th class="border-0 text-uppercase small font-weight-bold">Sl no</th>
        <th class="border-0 text-uppercase small font-weight-bold">Spares Particulars</th>
        <th class="border-0 text-uppercase small font-weight-bold">Amount</th>
        <th class="border-0 text-uppercase small font-weight-bold">Quantity</th>
        <?php if ($gid != 'YES') { ?>
        <th class="border-0 text-uppercase small font-weight-bold">GST %</th>
        <th class="border-0 text-uppercase small font-weight-bold">GST AMT</th>
        <th class="border-0 text-uppercase small font-weight-bold">CGST %</th>
        <th class="border-0 text-uppercase small font-weight-bold">CGST AMT</th>
        <th class="border-0 text-uppercase small font-weight-bold">SGST %</th>
        <th class="border-0 text-uppercase small font-weight-bold">SGST AMT</th>
        <th class="border-0 text-uppercase small font-weight-bold">Total AMT</th>
        <? } ?>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td><?php echo $i ?></td>
        <td><?php echo $sparen ?></td>
        <td><?php echo $row9['amt'] ?></td>
        <td><?php echo $row9['quant'] ?></td>
        <?php if ($gid != 'YES') { ?>
        <td><?php echo $row9['gstp'] ?></td>
        <td><?php echo round($row9['amt'] * ($row9['gstp'] / 100)) ?></td>
        <td><?php echo round($row9['gstp'] / 2) ?></td>
        <td><?php echo round(($row9['amt'] * ($row9['gstp'] / 100)) / 2) ?></td>
        <td><?php echo round($row9['gstp'] / 2) ?></td>
        <td><?php echo round(($row9['amt'] * ($row9['gstp'] / 100)) / 2) ?></td>
        <td><?php echo round((($row9['amt'] * ($row9['gstp'] / 100)) + $row9['amt']) * $row9['quant']) ?></td>
        <? } ?>
    </tr>
    </tbody>
</table>

在頂部設置標志變量值:

 $gid = $_SESSION['gid'];

    if(gid == NO) 
    {
         $flag = true;

    }
    else{
            $flag = false;
    }


    <table class="table">
        <thead>
        <tr>
        <th class="border-0 text-uppercase small font-weight-bold">Sl no</th>
        <th class="border-0 text-uppercase small font-weight-bold">Spares Particulars</th>
        <th class="border-0 text-uppercase small font-weight-bold">Amount</th>
        <th class="border-0 text-uppercase small font-weight-bold">Quantity</th>
        <th class="border-0 text-uppercase small font-weight-bold">GST %</th>
        <th class="border-0 text-uppercase small font-weight-bold">GST AMT</th>
        <th class="border-0 text-uppercase small font-weight-bold">CGST %</th>
        <th class="border-0 text-uppercase small font-weight-bold">CGST AMT</th>
<?php if($flag) { ?>
        <th class="border-0 text-uppercase small font-weight-bold">SGST %</th>
        <th class="border-0 text-uppercase small font-weight-bold">SGST AMT</th>
<?php } ?>
        <th class="border-0 text-uppercase small font-weight-bold">Total AMT</th>
         </tr>
            </thead>
            <tbody>
        <tr>
    <td><?php echo $i ?></td>
    <td><?php echo $sparen ?></td>
    <td><?php echo $row9['amt'] ?></td>
    <td><?php echo $row9['quant'] ?></td>
    <td><?php echo $row9['gstp'] ?></td>
    <td><?php echo round($row9['amt']*($row9['gstp']/100)) ?></td>
    <td><?php echo round($row9['gstp']/2) ?></td>
    <td><?php echo round(($row9['amt']*($row9['gstp']/100))/2) ?></td>

<?php if($flag) { ?>    
    <td><?php echo round($row9['gstp']/2) ?></td>
    <td><?php echo round(($row9['amt']*($row9['gstp']/100))/2) ?></td>
<?php } ?>

    <td><?php echo round((($row9['amt']*($row9['gstp']/100))+$row9['amt'])*$row9['quant']) ?></td>

    </tr>                            
    </tbody>
    </table>

並簽入HTML部分以顯示/隱藏TD標題及其各自的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM