Reputation: 1633
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.
Upvotes: 1
Views: 1714
Reputation: 262
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.
Upvotes: 1
Reputation: 3541
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.
Upvotes: 2
Reputation: 750
<?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>
Upvotes: 1