truegilly Posted January 25, 2007 Share Posted January 25, 2007 Hiwas wondering is anyone could shed any light on this no doute simple problem.lets say i have some variables, [color=red]$Actual [/color]and [color=red]$Target[/color].[code]$Target = 5;$Actual = 3;[/code]Both are integers and get their values from a database.see the example below its all very simple[code]if ($Target == $Actual) { $colour = "green"; return $colour; } else if ($Actual < $Target) { $colour = "red"; return $colour; } else { $Actual > $Target; $colour = "blue"; return $colour; }[/code]what i would like to add to this statement is if $actual is less than the $Target by 2, it will return "Amber". so the expression will be something like this...[code]if ($Actual < $Target by 2) {return "amber"}[/code]can anyone explain how this is done ?thanks in advance :PTruegilly Link to comment https://forums.phpfreaks.com/topic/35653-variable-conparison-tests/ Share on other sites More sharing options...
suzzane2020 Posted January 25, 2007 Share Posted January 25, 2007 if(($Actual-$Target)==2){ $color="Amber"; return $color;} Link to comment https://forums.phpfreaks.com/topic/35653-variable-conparison-tests/#findComment-168875 Share on other sites More sharing options...
suzzane2020 Posted January 25, 2007 Share Posted January 25, 2007 sorry te other way aroundif(($Target-$Actual)==2){ $color="Amber"; return $color;} Link to comment https://forums.phpfreaks.com/topic/35653-variable-conparison-tests/#findComment-168877 Share on other sites More sharing options...
truegilly Posted January 25, 2007 Author Share Posted January 25, 2007 Thanks dude, ill have a play ! ;) Link to comment https://forums.phpfreaks.com/topic/35653-variable-conparison-tests/#findComment-168879 Share on other sites More sharing options...
rantsh Posted January 25, 2007 Share Posted January 25, 2007 Suzzane2020 has given you the only way you can achieve this...You may also useif($Target==$Actual-2) or if($Target+2==$Actual)... hehehehehehe :p... but the point is that I don't think there's any other real solution.[quote]if(($Target-$Actual)==2){ $color="Amber"; return $color;}[/quote] Link to comment https://forums.phpfreaks.com/topic/35653-variable-conparison-tests/#findComment-168880 Share on other sites More sharing options...
truegilly Posted January 25, 2007 Author Share Posted January 25, 2007 thanks for your help,heres my code. the problem is is the red section seem to take over and doesn't allow the amber to show, could you adjust my code so it will work ?[code] if ($Target == $Actual) { $colour = "green"; return $colour; } if(($Actual-$Target)==2) { $color="Amber"; return $color; } else if ($Actual < $Target) { $colour = "red"; return $colour; } else { $Actual > $Target; $colour = "blue"; return $colour; }[/code]thanksTruegilly :D Link to comment https://forums.phpfreaks.com/topic/35653-variable-conparison-tests/#findComment-168882 Share on other sites More sharing options...
suzzane2020 Posted January 25, 2007 Share Posted January 25, 2007 add a part over hereelse if (($Actual < $Target) &&( ($Actual-$Target)!=2)) { $colour = "red"; return $colour; } Link to comment https://forums.phpfreaks.com/topic/35653-variable-conparison-tests/#findComment-168883 Share on other sites More sharing options...
truegilly Posted January 25, 2007 Author Share Posted January 25, 2007 Hi guys,thanks for all your help. Its now working and the code is below for anyone who has a similar problem[code] if ($Target == $Actual) { $colour = "green"; return $colour; } else if(($Target-$Actual)==2 || ($Target-$Actual)==1) { $color="Amber"; return $color; } else if ($Actual < $Target) { $colour = "red"; return $colour; } else { $Actual > $Target; $colour = "blue"; return $colour; }[/code]Truegilly :P Link to comment https://forums.phpfreaks.com/topic/35653-variable-conparison-tests/#findComment-168885 Share on other sites More sharing options...
Jenk Posted January 25, 2007 Share Posted January 25, 2007 You'll need to tweak that.If your actual is less than your target by 1, it will be red, but if it's 2, it will be amber, and greater than two are back to red. Link to comment https://forums.phpfreaks.com/topic/35653-variable-conparison-tests/#findComment-168914 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.