jaylam13 Posted August 24, 2011 Share Posted August 24, 2011 Hi, Im very new to php and still learning so please forgive me if there is a very simple solution. I am trying to write a script to enter salinity measurements. The values that are checked run from 1.000 - 1.089 going up by one, so for example 1.001, 1.002, 1.003 etc etc. So lets say the set vaule of $desiredsalinity is 1.010 this is what I want the script to do: 1.070 - three lower than pre set vaulue, show red message 1.080 - two lower than pre set value, show yellow message 1.090 - one lower than pre set value, show green message 1.010 - equal to pre set value, show green message 1.011 - one higher than pre set value, show green message 1.012 - two higher than pre set value, show yellow message 1.013 - three higher than pre set value, show red message This is the code I have but cant seem to get it to work, it did work for increments of 10 but not 1: if( round( $Actualsalinity, 10 ) == round( $Desiredsalinity2, 10 ) ) { $actualsalinitycolor = $diarygreen; $salinitymessage=$salinitygreenmessage; $salinityadvice=$salinityadvicegreen; } elseif( abs( $Actualsalinity - $Desiredsalinity2 ) <= .020 ) { $actualsalinitycolor = $diaryyellow; $salinitymessage=$salinityyellowmessage; $salinityadvice=$salinityadviceyellow; } else { $actualsalinitycolor = $diaryred; $salinitymessage=$salinityredmessage; $salinityadvice=$salinityadvicered; } Any help would be appreciated. Kind Regards Jay Quote Link to comment https://forums.phpfreaks.com/topic/245579-if-help-please/ Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 you have round precision set to 10; Quote Link to comment https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261321 Share on other sites More sharing options...
jaylam13 Posted August 24, 2011 Author Share Posted August 24, 2011 you have round precision set to 10; I know thats the code for when I had it set to 10's but I have tried adjusting it but cannot seem to get it to do what I want Quote Link to comment https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261322 Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 precision = 100. Quote Link to comment https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261324 Share on other sites More sharing options...
jaylam13 Posted August 24, 2011 Author Share Posted August 24, 2011 precision = 100. That works, almost. It goes yellow when it is one higher or lower, I need it to go green when its one higher or lower then red if its three lower or higher. Thanks Jay Quote Link to comment https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261325 Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 why not use something like this: <?php $desiredsalinity = 1.010; $salinity = 1.080; $diff = abs($desiredsalinity - $salinity) * 1000; if($diff == 0){ echo "green"; }else if($diff == 1){ echo "yellow"; }else if($diff >= 3){ echo "red"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261329 Share on other sites More sharing options...
jaylam13 Posted August 24, 2011 Author Share Posted August 24, 2011 why not use something like this: <?php $desiredsalinity = 1.010; $salinity = 1.080; $diff = abs($desiredsalinity - $salinity) * 1000; if($diff == 0){ echo "green"; }else if($diff == 1){ echo "yellow"; }else if($diff >= 3){ echo "red"; } ?> Because the desiredsalinity is different for each user. When they setup their account they enter what their desired salinity is which is stored in a sql db then when they go to this page it checks it so the desiredsalinity is not static. Quote Link to comment https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261338 Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 man, that was just an EXAMPLE you could adapt for your needs. Of course the values would come from a database or something. it was just a way to narrow down the differences to one single digit, making it easier for you to define results for each range. (since you were stuck with the precision thing) Quote Link to comment https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261339 Share on other sites More sharing options...
jaylam13 Posted August 24, 2011 Author Share Posted August 24, 2011 man, that was just an EXAMPLE you could adapt for your needs. Of course the values would come from a database or something. it was just a way to narrow down the differences to one single digit, making it easier for you to define results for each range. (since you were stuck with the precision thing) I have changed it to fit my needs but everything just satys green no matter what I set the salinity to. This is confusing, might have to re think how to do it completely. Quote Link to comment https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261341 Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 ok. what exactly are the color options? red if difference is greater than .003 yellow if difference is only .001 green if there is no difference. what about when difference is .002 ? Quote Link to comment https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261342 Share on other sites More sharing options...
jaylam13 Posted August 24, 2011 Author Share Posted August 24, 2011 ok. what exactly are the color options? red if difference is greater than .003 yellow if difference is only .001 green if there is no difference. what about when difference is .002 ? if the difference is up to 2 either way = green if the difference is over 2 but not higher than 4 either way = yellow if the difference is over 4 either way = red Thanks Jay Quote Link to comment https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261357 Share on other sites More sharing options...
PFMaBiSmAd Posted August 24, 2011 Share Posted August 24, 2011 ^^^ That's not what the first post in the thread stated. You stated +/- 1 or equal is green; +/- 2 is yellow; +/- 3 (or more) is red. Are you sure? Quote Link to comment https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261361 Share on other sites More sharing options...
jaylam13 Posted August 24, 2011 Author Share Posted August 24, 2011 ^^^ That's not what the first post in the thread stated. You stated +/- 1 or equal is green; +/- 2 is yellow; +/- 3 (or more) is red. Are you sure? Sorry for the confusion, yes im sure Quote Link to comment https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261362 Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 I'm doing this based on your initial post, assuming the ranges are from 1.000 to 1.089, so there are 99 possible values. but we only need to worry about 3 of them 1. difference < 2 either way = green 2. difference > 2 and < 5 either way = yellow 3. difference > 4 either way = red <?php $diff = abs($Desiredsalinity2 - $Actualsalinity) * 1000; if($diff < 2){ // echo 'GREEN'; $actualsalinitycolor = $diarygreen; $salinitymessage=$salinitygreenmessage; $salinityadvice=$salinityadvicegreen; }else if($diff < 5){ // echo 'YELLOW'; $actualsalinitycolor = $diaryyellow; $salinitymessage=$salinityyellowmessage; $salinityadvice=$salinityadviceyellow; }else{ // echo 'RED'; $actualsalinitycolor = $diaryred; $salinitymessage=$salinityredmessage; $salinityadvice=$salinityadvicered; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261363 Share on other sites More sharing options...
jaylam13 Posted August 24, 2011 Author Share Posted August 24, 2011 I'm doing this based on your initial post, assuming the ranges are from 1.000 to 1.089, so there are 99 possible values. but we only need to worry about 3 of them 1. difference < 2 either way = green 2. difference > 2 and < 5 either way = yellow 3. difference > 4 either way = red <?php $diff = abs($Desiredsalinity2 - $Actualsalinity) * 1000; if($diff < 2){ // echo 'GREEN'; $actualsalinitycolor = $diarygreen; $salinitymessage=$salinitygreenmessage; $salinityadvice=$salinityadvicegreen; }else if($diff < 5){ // echo 'YELLOW'; $actualsalinitycolor = $diaryyellow; $salinitymessage=$salinityyellowmessage; $salinityadvice=$salinityadviceyellow; }else{ // echo 'RED'; $actualsalinitycolor = $diaryred; $salinitymessage=$salinityredmessage; $salinityadvice=$salinityadvicered; } ?> Perfect, thank you so much for your help. Now that it works I can tweak it to work with different ranges. Appreciate it Jay Quote Link to comment https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261364 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.