• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

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

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. . . ??
0
FairyBusiness
Asked:
FairyBusiness
  • 4
  • 3
  • 2
1 Solution
 
PranjalShahCommented:
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

0
 
zappafan2k2Commented:
It looks like a switch statement would work well in this case.
foreach($cart as $item) {
    switch($item[0]) {
      case 'b':
            $table = 'bracelets';
            echo cart_details($item, $table, $column='name');
            echo cart_details($item, $table, $column='price');
            break;
      case 'n':
            $table = 'necklaces';
            echo cart_details($item, $table, $column='name');
            echo cart_details($item, $table, $column='price');
            break;
      case 'e':
            $table = 'earrings';
            echo cart_details($item, $table, $column='name');
            echo cart_details($item, $table, $column='price');
            break;
      }
}

Open in new window

0
 
FairyBusinessAuthor Commented:
Thanks!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PranjalShahCommented:
I am not sure if you tried my solution. Dont need points but just wanted to know if my solution was wrong.
0
 
FairyBusinessAuthor Commented:
I copied and pasted it but I got the same error.  What did you do differently??
0
 
PranjalShahCommented:
IF it didnt work, then its ok..nvm
0
 
zappafan2k2Commented:
@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].
0
 
PranjalShahCommented:
Ya see that now..didnt notice there was a break in between..
0
 
FairyBusinessAuthor 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.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now