开发者

PHP: Change color of text based on $value

开发者 https://www.devze.com 2022-12-30 06:33 出处:网络
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, Or开发者_运维问答ange.Greater than 11, Red.function getProperColor($num

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, Or开发者_运维问答ange. Greater than 11, Red.


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>


$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>";


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>


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>


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';


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>


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>";


Ternary operator for your simple example.

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


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?


I found this plugin:

http://www.jnathanson.com/blog/client/jquery/heatcolor/index.cfm#examples


Assuming one works with ranges than this is quite flexible:

function getProperColor($range, $value)
{
    foreach($range as $key => $color)
    {
        if ($value <= $key)
            return $color;
    }
    return $color;
}

$colorRange = array(
    5 => 'green',
    10 => 'orange',
    11 => 'red'
);

for($i=-1;$i<16;$i+=2)
{
    echo "$i:" . getProperColor($colorRange, $i) . "\n";
}

This will output:

-1:green
1:green
3:green
5:green
7:orange
9:orange
11:red
13:red
15:red


<?php
$var='2';
?>
 <html>
  <body>
   <p>Hello Welcome.I am
     <?php 
      if($var>0 && $var<=5)
        echo '<p style=color:Green;>';
      else if($var>=6 && $var<= 10)
        echo '<p style=color:orange;>';
      else
         echo '<p style=color:red;>';
?>
        I am a Developer.
     </p>
     </body>


why not a switch case ? it would be cleaner this way

   $color = "";
   switch(true){
      case ($var > 0 && $var <= 5):
            $color = '#00FF00';
      break;
      case ($var >= 6 && $var <= 10):
            $color = '#00FF00';
      break;
      case ($var >= 11):
            $color = '#00FF00';
      break;
      default:
            $color = '#000000';


$color="green";
$text="foo";
echo wrapColor($color, $text);

function wrapColor($color, $text){
return "<span style=color:$color>$text</span>";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号