I have 4 different dynamic variables that change per user (age, gender, country, approval rating)

What I need to do if create a conditional if statement where it checks to see if all or any are correct. I can get it to work with all, and can get it to check if the user's age is within the accepted age range using the below code, but it skips the rest of one one of them is correct. I need it to check each one seperately.

Example:

$myRating = $_SESSION['approval'];
      $myGender = $_SESSION['gender'];
      $myCountry = $_SESSION['country'];
      $myAge = $_SESSION['age'];
      
      $taskRating = $thisTask['ap_perc'];
      $taskGender = $thisTask['gender'];
      $taskCountry = $thisTask['country'];
      $minAge = $thisTask['min_age'];
      $maxAge = $thisTask['max_age'];
      
      if($myAge >= $minAge & $myAge <= $maxAge OR $myRating >= $taskRating OR $myGender == $taskGender OR $myCountry == $taskCountry & $myRating >= $taskRating & $myGender == $taskGender & $myCountry == $taskCountry){

Ideally, the "task" will have requirements, such as Age Range (say 18-25), Gender, Country, and approval rating. If a user creates a task with the following requirements:

Ages 18-25 Female Only, in the US with an approval rating of 95% it works fine. However, if only a one or two variables are set (default with no requirements is "all" then it doesn't work. For example:

Ages 18-25, All Genders, in the US with any approval rating. This doesn't work.

How are you checking for "all"? If your requirement variable for "country" isn't set, meaning it should accept all countries, then if($myCountry == $taskCountry) won't work because $taskCountry is not set to anything. For example, if $myCountry is set to "USA" then you will be attempting to validate "USA" == "" , which of course won't work.

Also, you are using "OR" between the conditions, which means that if even one condition returns TRUE, the if statement will go ahead and execute its block. That's why it's skipping the rest of the tests.

At the end of the big if statement you seem repeat a bunch of the conditions. It looks like the last few comparisons are meant to check to make sure that all are true. You should separate conditions that should work together using parentheses. I'm not sure where they should go, but it looks like after the first $myCountry == $taskCountry you should have used an OR instead of an &, and then put one set of parentheses around the last 3 conditions since it checks to make sure they are all valid. I also noticed that you left the age tests out of this last set of conditions. You might want to edit it to this, if I understand correctly:

<?php
if(...($myCountry == $taskCountry) || (($myAge >= $minAge && $myAge <= $maxAge) && $myRating >= $taskRating && $myGender == $taskGender && $myCountry == $taskCountry)){
?>

Note that I use double pipes (||) instead of OR. I also prefer to use double ampersands, which is a logical operator, as opposed to the & which is a bitwise operator.

Personally, my recommendation is to make this easier to work with by splitting the one big if statement into a bunch of smaller ones, and have them flag if something's wrong. This way checks each condition separately, and can help visualize what's going wrong better IMO:

$all_ok = false; // test to see if all fields validate
$age_ok = false;
$rating_ok = false;
$gender_ok = false;
$country_ok = false;

if(($myAge >= $minAge & $myAge <= $maxAge) && ($myRating < $taskRating) && ($myGender != $taskGender) && ($myCountry != $taskCountry)) 
   $all_ok = true;
if($myAge >= $minAge & $myAge <= $maxAge)
   $age_ok = true;
if($myRating >= $taskRating)
   $rating_ok = true;
if($myGender == $taskGender)
   $gender_ok = true;
if($myCountry == $taskCountry)
   $country_ok = true;

// examples
if($all_ok) { ... } // if all are ok, do this.
if($age_ok) { ... } // if age is ok, do this.
if(($age_ok && $rating_ok) || $gender_ok) { ... } // if BOTH the age AND the rating are ok, then do this. If the gender is ok, then do this. If the age is ok, but the rating  and gender are not, don't do this.

Thanks, I had figured it out a few min after I posted this but your way is cleaner, so I'm going to re-write it tonight.

Mike

Just a stab at the dark, but what I think you want is "||" for OR.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.