tags:

views:

227

answers:

10

What's the easiest way to change a text's color based on a variable?

For example: If $var is between 1-5, green. Between 6-10, Orange. Greater than 11, Red.

+2  A: 

Something like this trio of if statements:

if ($var < 10) $color = '#FF8000';
if ($var < 6) $color = '#00FF00';
if ($var >= 10) $color = '#FF0000';

echo "<span style=\"color: $color;\">This text is colored.</span>";
JYelton
Be careful - in this particular example, anything from 0 - 5 (well, technically, -infinity to 5, but you get the idea) will always be assigned '#FF8000' (which I don't believe is correct).
ABach
anything less than 5 wont work since it would be overwritten since anything less than 5 will be less than 10. One can use elseif to get out of this situation
nik
My original answer had the if statements in an order that would cause a problem, where <6 was evaluated before <10. Technically elseif's would be better. I left this as original, but changed the order so that they'll work properly. My apologies on the original one, I was just aiming for something that was easy to read.
JYelton
+3  A: 
$color = "#000000";

if (($v >= 1) && ($v <= 5))
   $color = "#00FF00";
else if (($v >= 6) && ($v <= 10))
   $color = "#FF9900";
else if ($v >= 11)
   $color = "#FF0000";

echo "<span style=\"color: $color\">Text</span>";
LukeN
You switched between `$v` and `$var` in the second if block. ;)
ABach
Thanks for pointing that out, fixed :)
LukeN
+1 this is probably the best solution, and doesn't have any "open-ended" evaluations (such as in mine which will happily take values less than 1 and still use green color).
JYelton
This answer has "Greater than OR EQUAL to 11" to be red. Not the same as "Greater than"..
zaf
So what's in the case of $v being equal to 11? Black again?
LukeN
@LukeN We don't know. The OP has failed to clarify what happens when n=11.
zaf
I think, sometimes healthy assumption is okay. This looks like some rating system where low = good and high = bad, so there'd be no sense in leaving undefined spots in the definition. :)
LukeN
+1  A: 

A simple solution might be to do something like this...

if ($var < 6)
    $style="0F0";
else if ($var < 11)
    $style="F50";
else
   $style = "F00";

?><div style="color:#<?php echo $style; ?>">blar</div>
rikh
+5  A: 
function getProperColor($number)
{
    if ($var > 0 && $var <= 5)
        return '#00FF00';
    else if ($var >= 6 && $var <= 10)
        return = '#FF8000';
    else if ($var >= 11)
        return = '#FF0000';
}

And use it like this

<div style="background-color: <?=getProperColor($result['number'])?>;"><?=$result["title"]?></div>
sshow
Is a function overkill for a simple operation like this?
Kevin Brown
If you keep an external file for functions, as I always do, I'd say this is a more readable way to do it.
sshow
A function is probably your best bet in terms of clarity and organization.
ggfan
You're right. I'm passing several variables through, and the function definitely was the solution!
Kevin Brown
Unbelievable - the wrong answer gets the green tick! This answer has been edited and still does not follow the OP rules.
zaf
What's wrong about this?
Kevin Brown
@Kevin and I suppose @sshow, you wanted "Greater than 11" to be red. This answer is "Greater than OR EQUAL to 11" to be red. Spot the difference now?
zaf
Technically, the OP also asked for values *between* 1 and 5, and *between* 6 and 10, and *greater* than 11; which means 1, 5, 6, 10, and 11 are all excluded from consideration. Most people who answered (including myself) read into the question that perhaps Kevin didn't word it completely literally (sorry Kevin if you did). So we took it to mean that 1 *through* 5 are green, 6 *through* 10 are orange and anything 11 and greater are red.
JYelton
+2  A: 

You need to actually use elseif statements if your going to use a set of if statements,

 if ($var < 6) $color = '#00FF00';
elseif ($var < 10) $color = '#FF8000';
elseif ($var > 10) $color = '#FF0000';
fafnirbcrow
If $var == 10 this will not set $color, though you're right, elseif's are better than a trio of if's, as in my answer.
JYelton
-1 for not being correct.
zaf
A: 
$color="green";
$text="foo";
echo wrapColor($color, $text);

function wrapColor($color, $text){
return "<span style=color:$color>$text</span>";
}
Dire
I don't get this one. What good is it?
LukeN
This example doesn't really address @Kevin Brown's question; it will always print out `<span style=color:green>foo</span>` (which is not what he was asking for)
ABach
+1  A: 

Are color values indexed by constants? I would prepare hash map

$colorMap[0] = '#00FF00'; //green
$colorMap[1] = '#0000FF'; //blue
$colorMap[2] = '#FF0000'; //red
$colorMap[3] = '#330000'; //dark red

and so on. Then use CSS

<span style="color: <?php echo $colorMap[$var]; ?>;">desired color</span>
doc
Nice approach, but this isn't exactly what I need. :)
Kevin Brown
+1  A: 

I'll use CSS colors and also highlight the fact that the number 11 does not map to any color according to your rules making most of the answers invalid :)

<?php

$color=getColor(11);

function getColor($n){

    // Is number between 1 and 5?
    if($n>=1 && $n<=5) return "green";

    // Is number between 6 and 10?
    if($n>=6 && $n<=10) return "orange";

    // Is number greater than 11
    if($n>11) return "red";

    // Return default (black) for all other numbers
    return "black";

}

?>

<span style='color:<?=$color?>'>Text</span>
zaf
A: 

I would use CSS classes instead of inline styling the color... Let CSS work...

<?php
$var = 5;
$class = (($var < 6) ? 'greenclass' : (($var < 11) ? 'orangeclass' : 'redclass' ))
?>
<span class="<?php echo $class?>">text</div>

If none of these answers are the one you expect, what exactly are you trying to accomplish? Can you give more info?

acmatos
+1  A: 

Ternary operator for your simple example.

  $color = ($var < 6) ? '#FF8000' :  (($var < 10) ? '#00FF00' : '#FF0000');
alexganose