Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

conditional statements to change colour? 1

Status
Not open for further replies.

eHanSolo

Technical User
May 24, 2004
260
GB
hi there,

i was wondering if there's a way to conditionally change the colour of a value depending on what that value is? say, if it's less than zero then is there some php code i can use to set it to red?

any ideas/suggestions would be greatly appreciated!

e
 
This is fairly easy by using PHP to produce HTML/CSS markup that changes the color of the output.
Code:
<?
$i = 4;
if ($i<0){
   $format = "<span style=\"color: red\">%d</span>";
} else {
   $format = "%d";
}
sprintf($format,$i);
?>
 
thanks for that... but i'm can't get it to work... here's what i've done:

<td class='whateverClass'>
<? if($myVal<0){
$format = "<span stlye=\"color: red\">%d</span>";
} elseif($myVal>0){
$format = "<span stlye=\"color: blue\">%d</span>";
} else {
$format = "<span stlye=\"color: black\">%d</span>";
}sprintf($format,$myVal); ?>
</td>

Can you see where i've gone wrong?

thanks.

e
 
Several things to note:
1. Typo: style not stlye
2. sprintf only returns a string that is formatted. You still will have to either echo() or print() the string.
 
thanks for the that...

i'm using:

echo sprintf($format,$myVal);

and it's only the '%d' is coming up as a different colour.

any ideas?


e
 
Here's literal code that works:
Code:
<?php
$i = $_GET['val'];
if ($i<0){
   $format = "<span style=\"color: red\">%d</span>";
} elseif($i>0) {
   $format = "<span style=\"color: blue\">%d</span>";
} else {
 $format = "%d";
}
echo(sprintf($format,$i));
?>

Try this in a test script to see that it works. Append a GET param test.php?val=1 etc. to see.
You can also paste your code so we can examine what's wrong. I f a literal %d is shown you're doing something wrong (which we can't see since there's no cde...)
 
cool thanks... it's the colour thing is working now but it's only displaying the first digit in either red or blue.
So if i have 16.34%... it'll show up as 1 (in blue).

Also, if i have something like -2.34%. The negative sign disappears and ony '2' appears in red.

i'd like all the numbers including the negative sign to appear... is this possible?

thank you very much!

e
 
paste the code you are using between
Code:
 tags like DRJ478 asks.
 
here is my code:

<td class='PT_Data c4'>
<? echo $wantVal; ?>%</td>


that's all it is... been trying to amend this so it outputs it in either red or blue...
 
Ok, buddy. Here's the rundown:

The values you supply need to be formatted in a specific way. The sprintf function holds as first parameter $format, a formatting string.

To learn how to fomat them to hold percent signs, show decimals etc. you should investigate the documentation at
'16.34%' also is not a number, it is a string. Once you see in the documentation what %d means you'll understand. Please read the docs and if you have problems post back. It is important that you put that effort in so you actually understand how it works and so you can solve similar problems in the future.
 
cant use switch for this then ?


switch($i){

case 0:
$format = "<span style=\"color: red\">$i</span>";
break;
case $i < 6:
$format = "<span style=\"color: blue\">$i</span>";
break;
case $i < 11:
$format = "<span style=\"color: ornage\">$i</span>";
break;
case $i > 10:
$format = "<span style=\"color: pink\">$i</span>";
break;

}





______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Thanks for all your help... it's working really well!

Big thanks go to DRJ478!! :)

Merry xmas and a happy new year :)


e
 
KarveR
The switch statement does not work that way.
The switch statement only evalues the expression in the actual switch() - once.
The case statements list the possible outcomes for that evaluated expression but do not evaluate anything. So, switch($i) is a value, the case statements only can list instances of values that match or not. Further comparisons like $i>6 are not possible.
For something like that if/elseif needs to be used.
 
Try it DJ ;)

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Yeah - there's a mixture of things at work that happen to work out, but they do not change the accuracy of the statement I made before.

The first case is the only one that actually uses the value of $i, and it is the only one that uses $i as supplied in the switch() statement. All other use boolean values and PHP gracefully converts any integer other than '0' to TRUE.

The most correct way (without mixing integer and boolean) of writing the switch as you suggested is:
Code:
switch(TRUE){

        case FALSE:
        $format = "<span style=\"color: red\">$i</span>";
        break;
        case $i < 6:
        $format = "<span style=\"color: blue\">$i</span>";
        break;
        case $i < 11:
        $format = "<span style=\"color: ornage\">$i</span>";
        break;
        case $i > 10:
        $format = "<span style=\"color: pink\">$i</span>";
        break;

}

Yes, it works, but not by comparison betwenn $i in the switch and the expressions in the cases (I know, you never said that, but that's the impression I got. Sorry). And admittedly, it looks better than any if/elseif could ever look.
 
I think once you try the code, it certainly places your previous statement on shaky ground.

You can do it that way, it does evaluate correctly and as you say, it looks very neat.

Yours however doesn't return the correct output.

I'd say that mine then, is the most correct way.


______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Im not say its programmatically correct, I'm saying it works, and looks neat, and to be told its not possible when quite clearly it achieves the end result is factually incorrect.

it *is* possible, as proven and while probably not the 'most correct' way of doing things, is certainly an alternative to be considered.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
so - given that it's christmas - everyone is right!

the nice thing about this community is that people care. [I'd love it if someone responded to my posts though...]
 
I wish that people would actually read the posts more carefully.
DRJ478 said:
Yes, it works... And admittedly, it looks better than any if/elseif could ever look.

The statement, however, that the expression in switch() is only evaluated once is not anything that's up for discussion. That's just a fact.

To make KarveR feel better I will revise the statement:
before said:
Further comparisons like $i>6 are not possible.
after said:
Further evaluations in the case statements will provide a result that will be compared to the result of the expression evaluated in the switch() statement.

And to leave no doubt I post this again: Yes, it works.

Happy Holidays.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top