如何根据php条件隐藏和显示html表中的列

时间:2018-12-10 06:28:50

标签: javascript php html css

如何根据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之类的列。问题是我尝试使用js,但这无法正常工作。有人可以帮我解决一个简单的问题。

3 个答案:

答案 0 :(得分:2)

您可以为该任务使用PHP三元运算符和css display属性

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

我认为这是完成任务的一种简单方法。 $ShowHide将根据td的值将显示属性设置为$gid

答案 1 :(得分:1)

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

答案 2 :(得分:1)

在顶部设置标志变量值:

 $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标题及其各自的值。