首页 文章

如何基于php条件隐藏和显示html表中的列

提问于
浏览
1

如何基于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 这样的coloums . 问题是我尝试了js,但那不起作用 . 有人可以通过更简单的解决方案帮助我 .

3 回答

  • 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

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

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

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

  • 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 Headers 及其各自的值 .

相关问题