Q: How to access all results from Php loop in an if statement I am trying to figure out why I can only access last result from a loop. I have looked at many post on here and I can only find where OPs are asking how to create the loop
If I have a table named shirts and a column named colors
In the colors column the entiries are:
red
blue
green

and in my php page:
$user = new User();
$userid = $user->data()->id;

 $choices = array();
   if ($colorResults = $db->query("SELECT * FROM shirts WHERE sellerId = $userid")){
     if($colorResults->num_rows){
           while($row = $colorResults->fetch_object()){
                $choices[] = $row;
           }
           $colorResults->free();
      }
 }

foreach($choices as $choice){
   if($choice->color == 'red'){
       echo 'Yes color is red'
   } else {
      echo 'No color';
      }
}

When i display results
Results:
red
blue 
green

Heres the problem
if($choice->color == 'green'){
  echo 'Yes color is green'
} else {
   echo 'No color';
}

This will output successfully
Yes color is green

but if I change the code to:
if($choice->color == 'red'){
   echo 'Yes color is red'
} else {
   echo 'No color';
}

This result output will echo
No color

If one of the colors is red why will the if statement only see the last result
and how can I access all results to make the if statement true.

A: First, I think part of the initial confusion is that $choices is an array of objects, not an object itself, so using if ($choices->color == 'red') will get you a 

Notice: Trying to get property of non-object

which you may not see depending on your error reporting setting. But at any rate, if you use if ($choices->color == 'anything') you will always get 'No color.' So I assume that if it matches for green then you must actually be using if ($choice->color == 'green/red/whatever') and the other thing is just a typo.
The part where you have it listing all the colors of all the choices is fine (or at least, it was fine when you originally added it before subsequent edits.)
foreach ($choices as $choice) {
   echo $choice->color . '<br>';
}

What it sounds like is happening based on the results you are seeing is that you are checking the value of $choice->color after the foreach loop. $choice will remain set after the foreach loop, but it will always be the last value in the loop. Now, I am not sure exactly what you are going for here, but it looks like you want to list all the colors returned by your query and then check if a particular one is included. You could do that like this:
$color_included = false; // The color you're looking for has not yet been found

foreach($choices as $choice) {
    echo $choice->color . '<br>'; // List all the colors from your query
    // Check if they are the one you are looking for
    if ($choice->color == 'red') $color_included = true;
}
// Print the result
echo ($color_included) ? 'Yes color is red' : 'No color';

