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

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

Solution 2:

You can do it by following code.

<?phpif($e=="All")
{
?><liclass="active"><palign="center"><ahref="salary_report_combined.php">Salary Report(Combined)</a></p></li><?php
}else{
?><liclass="active"><palign="center"><ahref="salary_report.php">Salary Report(Individual)</a></p></li><?php
}
?>

Solution 3:

You could do it like this:

<liclass="<?phpecho$e == "all" ? 'active' : ''; ?>><p align="center"><ahref="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"')?>><palign="center"><ahref="salary_report.php">Salary Report(Individual)</a></p></li>
<li<?=($e=='All'?' class="active"':'')?>><palign="center"><ahref="salary_report_combined.php">Salary Report(Combined)</a></p></li>

Solution 5:

<liclass="<?phpecho$e == "All" ? 'active' : ''; ?>><p align="center"><ahref="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?"