Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Oct 2005
    Location
    Newberg, Oregon
    Posts
    252
    Thanks
    1
    Thanked 0 Times in 0 Posts

    foreach problems, only returning last item

    Hello all,

    Any help on this would be a huge help. If it makes me look any better, I'm working with someone else's shoddy coding

    Here's the code

    PHP Code:
    <?
    if(isset($_POST['submit'])) {
    $to       "sdsd"// change to your email address
    $name     $cart->shipping_info['first_name'] . " " $cart->shipping_info['last_name'];
    $email    $cart->shipping_info['email'];
    $address  $cart->shipping_info['address'] . "<br />" $cart->shipping_info['address2'] . "<br />" $cart->shipping_info['city'] . ", " $cart->shipping_info['state'] . " " $cart->shipping_info['zip'] . "<br />" $cart->shipping_info['country'] . "<br />";
    $phone      $cart->shipping_info['phone'];
    foreach(
    $cart->cart_items as $key => $item)    {
        
    $img $item->info['img'];
        
    $iname $item->info['name'];
        
    $size $item->info['size'];
        
    $qty $item->info['qty'];
        
    $price $item->info['price'];
        
    $subtotal $item->info['subtotal'];
        }
    $order '     <table cellspacing="1" cellpadding="1" class="cart_table" width="100%">
                                <tr>
                                    <th colspan="3"></th>
                                    <th width="100"></th>
                                    <th width="60"></th>
                                    <th width="60"></th>
                                    <th width="40" style="text-align: center">Qty</th>
                                    <th width="40" style="text-align: center">Price</th>
                                    <th width="60" style="text-align: center">Subtotal</th>
                                </tr> 
                                <tr>
                                    <td></td>
                                    <td><img src="' 
    $img '" /></td>
                                    <td> ' 
    .$iname '</td>
                                    <td>' 
    $size '</td>
                                    <td></td>
                                    <td></td>
                                    <td align="center">' 
    $qty '</td>
                                    <td>$ ' 
    .$price '</td>
                                    <td>$ ' 
    .$subtotal '</td>
                                </tr>
                                <tr>
                                    <th colspan="8" style="text-align: right">Subtotal:&nbsp;&nbsp;</th>
                                    <th>$<span id="cart_subtotal">' 
    $cart->subtotal '</span>&nbsp;&nbsp;</th>
                                </tr>
                                <tr>
                                    <th colspan="8" style="text-align: right">Total:&nbsp;&nbsp;</th>
                                    <th>$<span id="cart_total"> ' 
    .$cart->get_total() . '</span>&nbsp;&nbsp;</th>
                                </tr>                           
                            </table>'
    ;
    $d        date('l dS \of F Y h:i:s A');
    $sub      "From  - Wine Order ";
    $headers  "From: $name <$email>\n";  
    $headers .= "Content-Type: text/html; charset=iso-8859-1\n";
    $mes     .= "You have received an order from $name - their email address is $email - Here is their information <br /><br />";
    $mes     .= "Name: $name <br />";
    $mes     .= "Address:<br /> ".$address."<br />";
    $mes     .= 'Phone: '.$phone."<br /><br />";
    $mes     .= 'Order: '.$order."<br /><br />";
    $mes     .= 'Date & Time: '.$d;

    if (empty(
    $name) || empty($email) || empty($address) || empty($phone))
    {
         echo 
    "   <h4>Sorry all fields are required.</h4>";
         echo 
    $mes;
         
    }
    elseif(!
    ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"$email)) {
         print 
    "   <h4>Sorry the email address you entered looks like it's invalid.</h4>";
    }
    else
    {
         
    mail($to$sub$mes$headers);
         print 
    "   <h4><center>Thank you, ".$name.", for joining our mailing list.<br>We will get back to you as soon as possible</center></h4>"
         echo 
    $mes;
    }

    } else {
    ?>
    Basically, it sends shopping cart results to an email. The foreach loop at the top is only returning the last item in the array, not all of them. The same exact for each loop is used on a different section, successfully, so I'm not sure why it's not working up here. It is late, so I may be missing something easy...any thoughts?

  • #2
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,007
    Thanks
    59
    Thanked 10 Times in 10 Posts

    Question

    PHP Code:
    foreach($cart->cart_items as $key => $item)    { 
        
    $img $item->info['img']; 
        
    $iname $item->info['name']; 
        
    $size $item->info['size']; 
        
    $qty $item->info['qty']; 
        
    $price $item->info['price']; 
        
    $subtotal $item->info['subtotal']; 
        } 
    shouldn't that closing bracket come after your table generating code?


  • #3
    Regular Coder
    Join Date
    Oct 2005
    Location
    Newberg, Oregon
    Posts
    252
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Like this? It produced the same results

    PHP Code:
    <?
    if(isset($_POST['submit'])) {
    $to       "sdsd"// change to your email address
    $name     $cart->shipping_info['first_name'] . " " $cart->shipping_info['last_name'];
    $email    $cart->shipping_info['email'];
    $address  $cart->shipping_info['address'] . "<br />" $cart->shipping_info['address2'] . "<br />" $cart->shipping_info['city'] . ", " $cart->shipping_info['state'] . " " $cart->shipping_info['zip'] . "<br />" $cart->shipping_info['country'] . "<br />";
    $phone      $cart->shipping_info['phone'];
    foreach(
    $cart->cart_items as $key => $item)    {
        
    $img $item->info['img'];
        
    $iname $item->info['name'];
        
    $size $item->info['size'];
        
    $qty $item->info['qty'];
        
    $price $item->info['price'];
        
    $subtotal $item->info['subtotal'];
    $order '     <table cellspacing="1" cellpadding="1" class="cart_table" width="100%">
                                <tr>
                                    <th colspan="3"></th>
                                    <th width="100"></th>
                                    <th width="60"></th>
                                    <th width="60"></th>
                                    <th width="40" style="text-align: center">Qty</th>
                                    <th width="40" style="text-align: center">Price</th>
                                    <th width="60" style="text-align: center">Subtotal</th>
                                </tr> 
                                <tr>
                                    <td></td>
                                    <td><img src="' 
    $img '" /></td>
                                    <td> ' 
    .$iname '</td>
                                    <td>' 
    $size '</td>
                                    <td></td>
                                    <td></td>
                                    <td align="center">' 
    $qty '</td>
                                    <td>$ ' 
    .$price '</td>
                                    <td>$ ' 
    .$subtotal '</td>
                                </tr>
                                <tr>
                                    <th colspan="8" style="text-align: right">Subtotal:&nbsp;&nbsp;</th>
                                    <th>$<span id="cart_subtotal">' 
    $cart->subtotal '</span>&nbsp;&nbsp;</th>
                                </tr>
                                <tr>
                                    <th colspan="8" style="text-align: right">Total:&nbsp;&nbsp;</th>
                                    <th>$<span id="cart_total"> ' 
    .$cart->get_total() . '</span>&nbsp;&nbsp;</th>
                                </tr>                           
                            </table>'
    ;

        }

    $d        date('l dS \of F Y h:i:s A');
    $sub      "From  - Wine Order ";
    $headers  "From: $name <$email>\n";  
    $headers .= "Content-Type: text/html; charset=iso-8859-1\n";
    $mes     .= "You have received an order from $name - their email address is $email - Here is their information <br /><br />";
    $mes     .= "Name: $name <br />";
    $mes     .= "Address:<br /> ".$address."<br />";
    $mes     .= 'Phone: '.$phone."<br /><br />";
    $mes     .= 'Order: '.$order."<br /><br />";
    $mes     .= 'Date & Time: '.$d;

    if (empty(
    $name) || empty($email) || empty($address) || empty($phone))
    {
         echo 
    "   <h4>Sorry all fields are required.</h4>";
         echo 
    $mes;
         
    }
    elseif(!
    ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"$email)) {
         print 
    "   <h4>Sorry the email address you entered looks like it's invalid.</h4>";
    }
    else
    {
         
    mail($to$sub$mes$headers);
         print 
    "   <h4><center>Thank you, ".$name.", for joining our mailing list.<br>We will get back to you as soon as possible</center></h4>"
         echo 
    $mes;
    }

    } else {
    ?>

  • #4
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    Each time through your loop, you are overwriting the value in $order so, naturally, by the time you send out your email only the last item is shown.

  • #5
    Regular Coder
    Join Date
    Oct 2005
    Location
    Newberg, Oregon
    Posts
    252
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That makes sense. Any thoughts on how I would change the code to make it work?

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    My sympathy for you having to work with that code. That is bad quality.

    This is how it should work (you should be able to apply this the existing code):

    Code:
    $order = | table header |
    for each $item in $cart
        $order += | item row |
    endfor
    $order += | table footer |
    So you'd have to separate out the header and footer, loop through the items and remember to append (with: .= ) rather than just assigning.

  • Users who have thanked aedrin for this post:

    JustinSainton (05-07-2008)

  • #7
    Regular Coder
    Join Date
    Oct 2005
    Location
    Newberg, Oregon
    Posts
    252
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That definitely makes sense...did I implement it wrong here? I must have, because now it just returns a blank screen....
    PHP Code:
    $order =      '<table cellspacing="1" cellpadding="1" class="cart_table" width="100%">
                                <tr>
                                    <th colspan="3"></th>
                                    <th width="100"></th>
                                    <th width="60"></th>
                                    <th width="60"></th>
                                    <th width="40" style="text-align: center">Qty</th>
                                    <th width="40" style="text-align: center">Price</th>
                                    <th width="60" style="text-align: center">Subtotal</th>
                                </tr> 
                                <tr>'
    ;
        foreach(
    $cart->cart_items as $key => $item)    {
        
                    
    $order .= '<td></td>';
                    
    $order .= '<td><img src="' $item->info['img'] . '" /></td>';
                    
    $order .= '<td><img src="' $item->info['name'] . '" /></td>';
                    
    $order .= '<td><img src="' $item->info['size'] . '" /></td>';
                    
    $order .= '<td></td>';
                    
    $order .= '<td></td>';
                    
    $order .= '<td align="center"><img src="' $item->info['qty'] . '" /></td>';
                    
    $order .= '<td><img src="' $item->info['price'] . '" /></td>';
                    
    $order .= '<td><img src="' $item->info['subtotal'] . '" /></td>';
    }
    $order. = '            </tr>
                                <tr>
                                    <th colspan="8" style="text-align: right">Subtotal:&nbsp;&nbsp;</th>
                                    <th>$<span id="cart_subtotal">' 
    $cart->subtotal '</span>&nbsp;&nbsp;</th>
                                </tr>
                                <tr>
                                    <th colspan="8" style="text-align: right">Total:&nbsp;&nbsp;</th>
                                    <th>$<span id="cart_total"> ' 
    .$cart->get_total() . '</span>&nbsp;&nbsp;</th>
                                </tr>                           
                            </table>'


  • #8
    Regular Coder
    Join Date
    Oct 2005
    Location
    Newberg, Oregon
    Posts
    252
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Got it...first and last periods....blast!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •