WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] foreach if else (3 posts)

  1. everyeurocounts
    Member
    Posted 12 months ago #

    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)

  2. bcworkz
    Member
    Posted 12 months ago #

    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']) : ?>

  3. everyeurocounts
    Member
    Posted 12 months ago #

    Thank you bcworkz!

    That was exactly it. Ill put the values on the left from now on!

Reply

You must log in to post.

About this Topic

Tags