We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Is it possible to put if statements in a foreach loop?

FairyBusiness
on
Medium Priority
322 Views
Last Modified: 2012-05-11
Hi,  is this possible?

foreach($cart as $item) {
      if($item[0] == 'b') {
            $table = 'bracelets';
            echo cart_details($item, $table, $column='name');
            echo cart_details($item, $table, $column='price');
            break;
      }
      elseif($item[0] == 'n') {
            $table = 'necklaces';
            echo cart_details($item, $table, $column='name');
            echo cart_details($item, $table, $column='price');
            break;
      }
      else($item[0] == 'e') {
            $table = 'earrings';
            echo cart_details($item, $table, $column='name');
            echo cart_details($item, $table, $column='price');
            break;
      }
}

because the way I have it right now its not working. . . ??
Comment
Watch Question

Try it like this..
foreach($cart as $item) {
      if($item == 'b') {
            $table = 'bracelets';
            echo cart_details($item, $table, $column='name');
            echo cart_details($item, $table, $column='price');
            break;
      }
      elseif($item == 'n') {
            $table = 'necklaces';
            echo cart_details($item, $table, $column='name');
            echo cart_details($item, $table, $column='price');
            break;
      }
      else($item == 'e') {
            $table = 'earrings';
            echo cart_details($item, $table, $column='name');
            echo cart_details($item, $table, $column='price');
            break;
      }
}

Open in new window

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks!
I am not sure if you tried my solution. Dont need points but just wanted to know if my solution was wrong.

Author

Commented:
I copied and pasted it but I got the same error.  What did you do differently??
IF it didnt work, then its ok..nvm
@PranjalShah,

If you are using if ... else if ... else, the break; statement will break your foreach loop, not the if statement.  If...else if ... else does not require a break.

@FairyBusiness,

The only thing PranjalShah changed was comparing $item instead of $item[0].
Ya see that now..didnt notice there was a break in between..

Author

Commented:
oh I needed the [0] because i was trying to determine which table the product belonged to.  if ts b17 its a bracelet.  n4 is a necklace, etc.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.