...

View Full Version : foreach problems, only returning last item



JustinSainton
05-07-2008, 10:23 AM
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 :D

Here's the 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?

nikos101
05-07-2008, 10:47 AM
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?

JustinSainton
05-07-2008, 10:56 AM
Like this? It produced the same results



<?
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 {
?>

PappaJohn
05-07-2008, 03:31 PM
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.

JustinSainton
05-07-2008, 04:37 PM
That makes sense. Any thoughts on how I would change the code to make it work?

aedrin
05-07-2008, 04:43 PM
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):



$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.

JustinSainton
05-07-2008, 05:35 PM
That definitely makes sense...did I implement it wrong here? I must have, because now it just returns a blank screen....


$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>';

JustinSainton
05-07-2008, 05:46 PM
Got it...first and last periods....blast!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum