IF statement = OR AND OR

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
amanxman
Forum Newbie
Posts: 1
Joined: Tue Nov 03, 2009 3:12 pm

IF statement = OR AND OR

Post by amanxman »

Hey,

I have a PHP statement which basically says:

variableA = 1 OR variableA = 2 AND variableB = Z AND variableC = N OR variableC = M

actual statement is below, but the above is easier to read!:

Code: Select all

 
$where = "location = 'ALL NORTH' OR location = 'Andreas' OR location = 'Ballaugh' OR location = 'Bride' OR location = 'Jurby' OR location = 'Lezayre' OR location = 'Maughold' OR location = 'Ramsey' OR location = 'St Judes' OR location = 'Sulby' AND sales_types_available LIKE '%Buy%' AND show_on_website = '1' AND development_type = 'HOUSING' OR development_type = '3 Bed House' OR development_type = '4 Bed House' OR development_type = '5 Bed House +' OR development_type = '1 Bed Bungalow' OR development_type = '2 Bed Bungalow' OR development_type = '3 Bed Bungalow' OR development_type = '4 Bed Bungalow'";
 
the above statement results in variable A being ignored if varaible C matches - because of the OR
i.e
Variable A = 1 or 2
AND Varaible B = Z
OR Variable C = M or N


what I want is variableA to apply, and then variableC to apply seperately
so:
Variable A = 1 or 2
AND Varaible B = Z
AND Variable C = M or N

is that possible? or do i need to split it down to two statements?

Ta


SOLVED with parenthesis

Code: Select all

 
  
 $where = "(location = 'ALL NORTH' OR location = 'Andreas' OR location = 'Ballaugh' OR location = 'Bride' OR location = 'Jurby' OR location = 'Lezayre' OR location = 'Maughold' OR location = 'Ramsey' OR location = 'St Judes' OR location = 'Sulby') AND (sales_types_available LIKE '%Buy%') AND (show_on_website = '1') AND (development_type = 'APARTMENTS' OR development_type = '1 Bed Apartment' OR development_type = '2 Bed Apartment' OR development_type = '3 Bed Apartment +')";
 
simple!!!
sureshmaharana
Forum Commoner
Posts: 30
Joined: Thu Jul 03, 2008 4:20 am
Contact:

Re: IF statement = OR AND OR

Post by sureshmaharana »

Try this:

Code: Select all

 
$where = "(location = 'ALL NORTH' OR location = 'Andreas' OR location = 'Ballaugh' OR location = 'Bride' OR location = 'Jurby' OR location = 'Lezayre' OR location = 'Maughold' OR location = 'Ramsey' OR location = 'St Judes' OR location = 'Sulby') AND (sales_types_available LIKE '%Buy%' AND show_on_website = '1') AND (development_type = 'HOUSING' OR development_type = '3 Bed House') OR development_type = '4 Bed House' OR development_type = '5 Bed House +' OR development_type = '1 Bed Bungalow' OR development_type = '2 Bed Bungalow' OR development_type = '3 Bed Bungalow' OR development_type = '4 Bed Bungalow')";
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: IF statement = OR AND OR

Post by VladSun »

Use a single IN operator instead of multiple OR operations
http://dev.mysql.com/doc/refman/5.0/en/ ... unction_in
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply