Skip to content Skip to sidebar Skip to footer

How To Apply A CSS Class Depending On A Value Contained In PHP Variable?

I'm using PHP, MySQL, HTML and CSS. Depending upon value in my PHP variable I want to apply a CSS class to an element. How should I do this? My code snippet of PHP and HTMl( eleme

Solution 1:

Try with the ternary operator like

<li class="<?php echo $e == "All" ? 'active' : ''; ?>><p align="center"><a href="salary_report_combined.php">Salary Report(Combined)</a></p></li>

Solution 2:

You can do it by following code.

<?php 
if($e=="All")
{
?>
<li class="active"><p align="center"><a href="salary_report_combined.php">Salary Report(Combined)</a></p></li>
<?php
}else{
?>
<li class="active"><p align="center"><a href="salary_report.php">Salary Report(Individual)</a></p></li>
<?php
}
?>

Solution 3:

You could do it like this:

<li class="<?php echo $e == "all" ? 'active' : ''; ?>><p align="center"><a href="salary_report_combined.php">Salary Report(Combined)</a></p></li>

Solution 4:

I don't understand why nobody says about second <li>. Just flip the statements to make another <li> "active"

<?php
$e=$_POST['users']; 
?>
<li<?=($e=='All'?'':' class="active"')?>><p align="center"><a href="salary_report.php">Salary Report(Individual)</a></p></li>
<li<?=($e=='All'?' class="active"':'')?>><p align="center"><a href="salary_report_combined.php">Salary Report(Combined)</a></p></li>

Solution 5:

<li class="<?php echo $e == "All" ? 'active' : ''; ?>><p align="center"><a href="salary_report_combined.php">Salary Report(Combined)</a></p></li>

Post a Comment for "How To Apply A CSS Class Depending On A Value Contained In PHP Variable?"