Jump to content

color change based on variable


timmah1

Recommended Posts

I have a site that generates values for particular stocks when the user checks their portfolio.

I have it set up so that they can see if they lost money, or gained money based upon what they purchased the stock for, minus what the last day trade was.

 

I want to be able to show if they lost money in red, and if they made money in green, but how would I do that?

 

Meaning, if this variable ($margin) is negative, show it red, but if it's positive, show it green

$margin = number_format ($value-$total_bought, 2);

 

Can anybody help me out?

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/127330-color-change-based-on-variable/
Share on other sites

There are a number of ways to do this. Here's one:

<?php
$margin = $value - $total_bought;
$disp_color = ($margin < 0)?'red':'green';
echo '<span style="color:' . $disp_color . '">' . number_format($margin,2) . '</span>';
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.