Page 1 of 1

stat comparison

Posted: Wed Jul 06, 2005 1:27 pm
by SidewinderX
hello im working on a script to compair user statistics. I have 20 different stats stored in a database. what i have done so far is take the stats and find the difference.

Code: Select all

$stat=stat1-stat2;
echo $stat;
something like that but a little more complex.

the first thing i want done is if stat1-stat2=0 i want it to display 0 in blue text, if stat1-stat2=any posative number i want it to display the number in green text and if stat1-stat2=any negative number i want it to be displayed in red text. I know im going to use an If statement, but i dont know how to make the if statement determine whether the value is posative, negative or 0.

Once that is done i want the script to take the absolute value of $stat and display it as a posative number. The user will know his number is negative, equal or posative by the color or the text. So i was wondering if someone could give me a few functions i could look at that might help me out. Ill give them a try and when i have troubell with those ill let you know.

Any help is appreciated.
Thanks.

Posted: Wed Jul 06, 2005 1:33 pm
by John Cartwright

Code: Select all

$stat = $stat1 - $stat2;

if ($stat == 0) {
  $color = 'blue';
}
elseif ($stat > 0 {
  $color = 'green';
}
elseif ($stat < 0 {
  $color = 'red';
}

echo '<span style="color: '.$color.'">'.$stat.'</span>';
$stat > 0 {
$color = 'green';
}
elseif ($stat < 0 {
$color = 'red';
}

echo '<span style="color: '.$color.'&quote;&gt;'.$stat.'&lt;/span&gt;';

lt;/span>';

en';
}
elseif ($stat < 0 {
$color = 'red';
}

echo '<span style="color: '.$color.'">'.$stat.'&lt;/span&gt;';

== 0) {
$color = 'blue';
}
elseif ($stat > 0 {
$color = 'green';
}
elseif ($stat < 0 {
$color = 'red';
}

echo '<span style="color: '.$color.'">'.$stat.'</span>';

[/php:1:3$stat = $stat1 - $stat2;

if ($stat == 0) {
$color = 'blue';
}
elseif ($stat > 0 {
$color = 'green';
}
elseif ($stat < 0 {
$color = 'red';
}

echo '<span style="color: '.$color.'">'.$stat.'&amp$stat = $stat1 - $stat2;

if ($stat == 0) {
$color = 'blue';
}
elseif ($stat > 0 {
$color = 'green';
}
elseif ($stat < 0 {
$color = 'red';
}

echo '<span style="color: '.$color.'">'.$stat.'</span>';


Posted: Wed Jul 06, 2005 1:37 pm
by SidewinderX
wow! that was a quick reply.

as for the absolute value would i just do

Code: Select all

echo '<span style=&quote;color: '.$color.'&quote;>'.abs($stat).'</span>';
?

Posted: Wed Jul 06, 2005 3:59 pm
by pickle
Here's an obfuscated way of doing the same thing. It's untested but I've seen nested statements like this before

Code: Select all

$color = (($stat1-$stat2) > 0) ? "green" : (($stat1-$stat2) == 0) ? "blue" : "red";
Are you familiare with the: "(clause) ? do if evaluates true : do if evaluates false" form of if/else? like this before

Code: Select all

$color = (($stat1-$stat2) > 0) ? "green" : (($stat1-$stat2) == 0) ? "blue" : "red";
Are you familiare with the: "(clause) ? do if evaluates true : do if evaluates false" form of if/else?]

Are you familiare with the: "(clause) ? do if evaluates true : do if evaluates false" form of if/else?
$color = (($stat1-$stat2) > 0) ? "green" : (($stat1-$stat2) == 0) ? "blue" : "red";


Are you familiare with the: "(clause) ? do if evaluates true : do if evaluates false" form of if/else?

Posted: Wed Jul 06, 2005 5:22 pm
by timvw
Here is complex ;)

Code: Select all

SELECT (stat1 - stat2) AS stat,
       IF(stat < 0, 'red', IF(stat > 0, 'green', 'blue')) AS color
FROM table
And then simply echo $row['color'] ;)