timmah1 Posted October 7, 2008 Share Posted October 7, 2008 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 More sharing options...
kenrbnsn Posted October 7, 2008 Share Posted October 7, 2008 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 Link to comment https://forums.phpfreaks.com/topic/127330-color-change-based-on-variable/#findComment-658673 Share on other sites More sharing options...
timmah1 Posted October 7, 2008 Author Share Posted October 7, 2008 Perfect! Thank you very much kenrbnsn I've never seen $disp_color, I'm going to have to look in that more now. Thanks again Link to comment https://forums.phpfreaks.com/topic/127330-color-change-based-on-variable/#findComment-658678 Share on other sites More sharing options...
timmah1 Posted October 7, 2008 Author Share Posted October 7, 2008 ok, am I blind? Where is the 'Topic Solved' link? Link to comment https://forums.phpfreaks.com/topic/127330-color-change-based-on-variable/#findComment-658679 Share on other sites More sharing options...
kenrbnsn Posted October 7, 2008 Share Posted October 7, 2008 The "Topic Solved" button got lost in the upgrade of the forum software. $disp_color is just a variable that I'm setting based on the value of $margin. I'm then using it in a style tag to set the color. Ken Link to comment https://forums.phpfreaks.com/topic/127330-color-change-based-on-variable/#findComment-658681 Share on other sites More sharing options...
timmah1 Posted October 7, 2008 Author Share Posted October 7, 2008 Thank you again, I really appreciate your help Link to comment https://forums.phpfreaks.com/topic/127330-color-change-based-on-variable/#findComment-658684 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.