0

I'm creating a custom search form and when I try and sort the results I get all the objects displayed instead of the matched criteria. The reason I discovered was that some of the inputs from the form don't have a default value and when this is not declared in the conditional statement later on (for sorting) it just shows all the objects, whether the other requirements are met or not. I tried applying an OR statement with the specific variables able to be empty, but it gave the same result. Like so -

<?php if ($bedrooms >= $min_rooms 
          && $bedrooms <= $max_rooms 
          && $space >= $min_space 
          && $space <= $max_space 
          && $price >= $min_price 
          && $price <= $max_price 
          && $sel_type == $type 
          || $sel_type == '' 
          && $country == $sel_country 
          || $sel_country == '' ) { ?>

(See the last two statements) I was thinking of checking each variable in the conditional statement before including it but it feels like unnecessary code. How would you do it?

1
  • Custom Post Types. I figure it's easier just sending the values with $_POST to the result page. Commented Aug 1, 2010 at 15:46

2 Answers 2

3

The && operator has a higher precedence than the || operator, so your expression is currently grouped like this, probably not what you want:

($bedrooms >= $min_rooms && $bedrooms <= $max_rooms && $space >= $min_space && $space <= $max_space && $price >= $min_price && $price <= $max_price && $sel_type == $type)
||
($sel_type == '' && $country == $sel_country)
||
($sel_country == '' )

Try adding parentheses like this to achieve correct grouping:

($bedrooms >= $min_rooms && $bedrooms <= $max_rooms && $space >= $min_space && $space <= $max_space && $price >= $min_price && $price <= $max_price && ($sel_type == $type || $sel_type == '') && ($country == $sel_country || $sel_country == '') )
Sign up to request clarification or add additional context in comments.

Comments

1

Your expression may fail as the && operator has a higher precedence than the || operation. That means an expression like this:

… && $sel_type == $type || $sel_type == ''

is equivalent to this (operator precedence highlighted by using parentheses):

(… && $sel_type == $type) || $sel_type == ''

To fix that put the || expressions in parentheses:

$bedrooms >= $min_rooms && $bedrooms <= $max_rooms && $space >= $min_space && $space <= $max_space && $price >= $min_price && $price <= $max_price && ($sel_type == $type || $sel_type == '') && ($country == $sel_country || $sel_country == '')

Additionally, your expression is probably easier to read and to maintain if you use some helper functions like a between function:

function between($val, $min, $max) {
    return $min <= $val && $val <= $max;
}

Then your expression reads:

between($bedrooms, $min_rooms, $max_rooms) && between($space, $min_space, $max_space) && between($price, $min_price, $max_price) && ($sel_type == $type || $sel_type == '') && ($country == $sel_country || $sel_country == '')

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.