1

I am a bit stuck on an if statement on my PHP code. I know this is probably a basic question for most of you out there, but i am stuck, and i would like some assistance.

I have 2 variables $max and $ min, which i have to compare with 2 other max and mins called $valor and $minimo in order to check if they are meeting somewhere. What i basically have is a set of values like min=20 and max=30.

I want to compare those with the other max and min, and check if somewhere the values meet, like if the min of the second one is 29. I want it to enter the if statement.

Here's the statement i got right now. But it's not working, and i just can't get the logic on it. Any help?

EDIT: Added an example of what i am trying to achieve in the comments.

if ($min >= $valor && $min <= $minimo || $max >= $valor && $max <= $minimo)
    {
        //Do nothing
    }
else
    {
        $queryq = "INSERT INTO precos_area (id_tecido,area_minima,area_maxima,preco) VALUES ('".$id_tipo."', '".$min."', '".$max."', '".$price."')";
        $resultsq = mysql_query($queryq) or die(mysql_error());
    }
2
  • || $max >= $valor && $max <= $minimo) is max ever goig to be Bigger than valor AND smaller than minimo at the same time? Commented Aug 27, 2015 at 8:43
  • My idea is for the values from one max and one min, not to intertwine with the ones of the other. Imagine a line. One max and one min show a segment to be painted. The other shows another segment which has to be painted with a different colour. My wish is for an if that checks if they ever cross, in order to do a statement if they do Commented Aug 27, 2015 at 8:47

3 Answers 3

2

I have a feeling the 2nd max and min are the wrong way round

if (($min >= $minimo && $min <= $valor ) || ($max >= $minimo && $max <= $valor ))

EDIT - to catch the situation that gview mentions you probably should check both ranges. This will then catch a situation where one range is completely within another range:-

if (($min >= $minimo && $min <= $valor ) 
|| ($max >= $minimo && $max <= $valor )
|| ($minimo >= $min && $minimo <= $max ) 
|| ($valor >= $min && $valor <= $max ))
Sign up to request clarification or add additional context in comments.

1 Comment

What about the case where $min < $minimo AND $max > $valor. The lines overlap in that case, but your logic would not find it true. Is that a problem?
1

Use

if (($min >= $valor && $min <= $minimo) || ($max >= $valor && $max <= $minimo))

4 Comments

Thanks. I tried grouping them. But it's still not working. Can someone help me out a bit on the logic on this? I made a comment on my main question further explaining what i am trying to achieve
can you define your values here please .
For example. $min is 10 and $max is 20. I want to check if whatever values are inserted on $minimo and $valor don't cross with those. Kinda like trying to paint 2 different areas with different colours. They can't cross anywhere in between. So $minimo can't be between 10 and 20, nor can $valor be in those values too
Kickstart solved it first. But thanks alot for your help too. You solved the 1st part of the problem too, so i'm upvoting your answer. Thanks :)
0

I think what you are looking for is something like this:

<?php

$min = 10;
$max = 20;
$valor = 25;
$minimo = 30;

if (($min >= $minimo && $min <= $valor ) || ($max >= $minimo && $max <= $valor )) {
print "win";
}

else {
print "lose";
}

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.