This topic is locked

If and Else If

12/6/2011 9:31:17 PM
PHPRunner General questions
A
angelcause author

I am wanting to apply a multiple conditional formatting on a field value. The field is basically a BurnRate indicator and will change color on different percentages, but I can't figure out the code, the below is not working for me.
Thanks in advance

if ($value < 100) {

$color="green";

} else if($value < 90) {

$color="red";

}

else if($value < 80) {

$color="blue";

}

else if($value < 70) {

$color="purple";

}

$value="<span style='color: " . $color . ";'>$value</span>";
C
cgphp 12/7/2011
if ($value < 100 AND $value >= 90) {

$color="green";

} else if($value < 90 AND $value >= 80) {

$color="red";

} else if($value < 80 AND $value >= 70) {

$color="blue";

} else if($value < 70) {

$color="purple";

}

$value="<span style='color: " . $color . ";'>".$value."</span>";
D
dafriend 12/7/2011



... the below is not working for me.


The problem is that after the first evaluation none of the remaining tests will be made. If $value is less than 100 the subsequent else conditions won't be tested. Multiple solutions exist.

First, test for the lowest value first and then for successively higher values in the remaining else if statements.

Second, you could change all your less than operators (<) to greater than (>). Make the first test value 90.

Third, simply drop all the 'else' conditions - make this a series of stand-alone if statements.

Last, (but not least) Cristian's solution.

C
CK. 12/7/2011

What I suggest is do it in this way:-
if ($value < 70) {

$color="purple";

} else if ($value < 80) {

$color="blue";

} else if ($value < 90) {

$color="red";

} else {

$color="green";

}

$value="<span style='color: " . $color . ";'>".$value."</span>";
This make whatever < 70 turn purple and whatever > 90 turn green. Your existing code is whatever > 100 will be default color (in most case - BLACK).
Regards,

php.Newbie