1

How to hide and show a column in html table based on a php condition.

$gid = $_SESSION['gid'];

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

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

The table would look like

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

I don't want the columns like gst,gstamt,cgst,cgstamt,sgst,sgstamt to be displayed if the gid=YES. The problem is I tried js for it but that's not working. Can someone help me out with an easier solution.

1
  • set a variable at page top and add IF condition in HTML part to show or hide by checking that value. Commented Dec 10, 2018 at 6:31

3 Answers 3

2

You can use PHP ternary operator and css display property for that task

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

I think this one of simple method to do the task. $ShowHide will set display property to td according to $gid value.

Sign up to request clarification or add additional context in comments.

4 Comments

this not working properly, the values are yet displaying but outside the table.
@VipulRao this is only for one td you have to follow same step for all as you wanted
@VipulRao can you let me know what are you getting in style="??" ?
Even thats none
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>

Comments

1

Set flag variable value at top:

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

and check in HTML part to show/hide TD title and their respective values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.