Displaying CSS style per a PHP variable value



Often you want to change a class of something depending on the state of something else. 

Here we will show you how to do this

 

# 1 > Overview

Best is to place our code into a HTML TAG: 

EG if this is a table you can place our code in the
<TR <?php //code will go here ?>> .... </TR>
OR
<TH <?php //code will go here ?>> .... </TH>

It can also be in a SPAN
<span <?php //code will go here ?>> ..... </span>

It can also be in a DIV
<div <?php //code will go here ?>> ..... </div>

 

 


# 2 > PHP Code

The code is simple

Let's say you see a php variable like:
-> $notification['Notification']['email']
-> If you code you can use any php variable

And we want to change our class if the email is TRUE (meaning it was emailed)
<?php if ($notification['Notification']['email']): ?>class_act<?php else: ?>class<?php endif; ?>

So when we add this to our DIV

<div class="<?php if ($notification['Notification']['email']): ?>class_act<?php else: ?>class<?php endif; ?>" >

Test goes here

</div>

Now when the 'email' is true the class_act will be used
-> When it is NOT TRUE class will be used. 

You can now create the following styles

<style>
.class_act {
     color: green;
}

.class {
     color: black;
}

</style>

This will make the text green when the email is sent and black when it is NOT sent


# 3 > PHP code complex

If your PHP variable is not a simple TRUE / FALSE
-> It might be a value like 'sent'

We then just modify our code to be:

<?php if ($notification['Notification']['email'] == 'sent'): ?>class_act<?php else: ?>class<?php endif; ?>

This means if our variable array is equal to 'sent' (not the 2 equal signs)

Now the class 'class_act' will only be displayed if 'sent' is set into the variable array


Other Instructions

Below are many other instructions that show you how to use your UpdateCase application