Title: foreach if else
Last modified: August 21, 2016

---

# foreach if else

 *  Resolved [everyeurocounts](https://wordpress.org/support/users/everyeurocounts/)
 * (@everyeurocounts)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/foreach-if-else/)
 * I Have a issue using a mvc type plugin (groupbuying if anyone else uses)
 * In the controller for the cart view i have these
 *     ```
       $line_items = array(
           'subtotal' => array(
           'label' => self::__( 'Subtotal' ),
           'data' => gb_get_formatted_money( $cart->get_subtotal() ),
           'weight' => 10,
        ),
           'discount' => array('label' => self::__( 'Facebook Discount @100%') ,
           'data' => gb_get_formatted_money( $facebookdisc ),
           'weight' => 11,
        ),
           'total' => array(
           'label' => self::__( 'Total' ),
           'data' => gb_get_formatted_money( $cart->get_total()
        ),
           'weight' => 1000,
        ),
       );
           $line_items = apply_filters( 'gb_cart_line_items', $line_items, $cart );
           uasort( $line_items, array( get_class(), 'sort_by_weight' ) );
       ```
   
 * and then in the view
 *     ```
       <tbody>
        <?php foreach ( $items as $item ): ?>
          <tr class="cart-line-item-row-<?php esc_attr_e( $key ); ?>">
        <?php foreach ( $columns as $key => $label ): ?>
          <td class="cart-<?php esc_attr_e( $key ); ?>">
        <?php if ( isset( $item[$key] ) ) { echo $item[$key]; } ?>
          </td>
        <?php endforeach; ?>
          </tr>
        <?php endforeach; ?>
   
       <?php foreach ( $line_items as $key => $line ) : //issues here!
        if ( $line['weight'] = 11) : ?>
         <tr class="cart-line-item cart-line-item-row-<?php esc_attr_e($key); ?>">
         <th scope="row" colspan="<?php echo count($columns)-1; ?>"><?php esc_html_e( $line['label'] ); ?></th>
         <td class="cart-line-item-<?php esc_attr_e($key); ?>"><?php esc_html_e($line['data']); ?></td>
         </tr>
       <?php ;
         else : ?>
         <tr> this sucks </tr>
         <?php endif; ?>
         <?php endforeach; ?>
       </tbody>
       ```
   
 * The issues occur on the 2nd foreach statement.
 * If i use just the if statement, all the $lines will show, if i include the else
   statement it doesn’t print the line which i gather to mean its seeing all the
   weights as 11 or i’ve mucked up somewhere!
 * Anyone any ideas of why its not working (BTW if i take out the if statement altogether
   it prints all the $line_items)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/foreach-if-else/#post-4057598)
 * In the line `if ( $line['weight'] = 11) : ?>` you are assigning the value `11`
   to the array item `$line['weight']`. I believe you want to _compare_ the array
   item to `11`, not _assign_ it. In which case you use the comparison operator `
   ==`, not the assignment operator `=`. This is what the line should be:
    `if (
   $line['weight'] == 11) : ?>`
 * It helps to put the value on the left side of comparisons so if a `=` is accidentally
   used, an error is thrown. Value on the right is a valid expression and no error
   is thrown. So this is slightly better:
    `if ( 11 == $line['weight']) : ?>`
 *  Thread Starter [everyeurocounts](https://wordpress.org/support/users/everyeurocounts/)
 * (@everyeurocounts)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/foreach-if-else/#post-4057617)
 * Thank you bcworkz!
 * That was exactly it. Ill put the values on the left from now on!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘foreach if else’ is closed to new replies.

## Tags

 * [else](https://wordpress.org/support/topic-tag/else/)
 * [foreach](https://wordpress.org/support/topic-tag/foreach/)
 * [if](https://wordpress.org/support/topic-tag/if/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 2 replies
 * 2 participants
 * Last reply from: [everyeurocounts](https://wordpress.org/support/users/everyeurocounts/)
 * Last activity: [12 years, 10 months ago](https://wordpress.org/support/topic/foreach-if-else/#post-4057617)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
