Page 1 of 1

mail() & arrays

Posted: Wed Jun 05, 2002 3:55 pm
by DSM
Working on a mail() function, and I need to be able to grab an array and insert the array items into the body of my email.

I am using a for loop right now, but I only get the last item in the array printed into the email. Any ideas?

Code: Select all

$num_rows = count($item_name);
for($i = 0; $i < $num_rows; ++$i):
$items = "&#1111;$item_name&#1111;$i](*$qty&#1111;$i])]";
endfor;
			
$headers .= "Content-Type: text/html; charset=iso-8859-1\n"; 
$headers .= "From: Website<sales@website.com>\n"; 
$mail = "<html><body>New COD Order<br><br>
For:$full_name<br>
Email:$email<br>
Phone:$phone<br>
Address:$address<br>
City:$city<br>
State:$state<br>
Zip Code:$zip_code<br><br>
Item Name(s)$items<br><br>
Shipping:$shipping<br>
COD:$cod<br>
Total:$total
</body></html>"; 

$to = "me@website.com"; 
$subject = "COD order"; 

mail($to,$subject,$mail,$headers);

Posted: Wed Jun 05, 2002 4:05 pm
by twigletmac
It's because each time the for loop iterates you replace $items with new data - so what gets printed out is the last result. You need to concenate the string if you want it to work so:

Code: Select all

$items = '';
for($i = 0; $i < count($item_name); ++$i) &#123; 
	$items .= "&#1111;$item_name&#1111;$i](*$qty&#1111;$i])]"; 
&#125;
Hope it helps,
Mac

Posted: Wed Jun 05, 2002 4:13 pm
by DSM
Thx, worked perfect.