stat comparison

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

stat comparison

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post 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>';
?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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'] ;)
Post Reply