mail() & arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DSM
Forum Contributor
Posts: 101
Joined: Thu May 02, 2002 11:51 am
Location: New Mexico, USA

mail() & arrays

Post 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);
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
DSM
Forum Contributor
Posts: 101
Joined: Thu May 02, 2002 11:51 am
Location: New Mexico, USA

Post by DSM »

Thx, worked perfect.
Post Reply