Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/245579-if-help-please/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261338
Share on other sites

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)

Link to comment
https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261339
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261341
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261357
Share on other sites

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;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261363
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/245579-if-help-please/#findComment-1261364
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.