Help with mail() array syntax.

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
richie--c
Forum Newbie
Posts: 6
Joined: Fri Dec 14, 2007 6:16 am

Help with mail() array syntax.

Post by richie--c »

Hope someone can help here... probably a simple fix but just cant see it...

I'm sending an order confirmation email populated with data posted back from paypal payment standard. All $_POST data is received for all items purchased.. eg item 1, 2, 3 4... etc... when I list these items in the email... like this...

$orderemailcontent .= "$item_number1 - $item_name1 - $quantity1 - $mc_gross_1 \n";

The correct values are pulled and displayed in the email...

However, when I try to use a 'while' loop I do not get the same data.

Here's the code......................

Code: Select all

$orderemail 	= "me@myemail.co.uk";
			$ordersubject 	= "Order confirmation";
			$orderheaders 	= "From: you@youremail.com\n";
  			$orderheaders 	. "MIME-Version: 1.0\n"
		   			 		. "Content-Transfer-Encoding: 7bit\n"
		   			 		. "Content-type: text/html;  charset = \"iso-8859-1\";\n\n";


$orderemailcontent = "emailtext....";

$count = 1;

while ($count <= $num_cart_items){

		//the following two lines pull the correct informatioin from $_POST data
		$orderemailcontent .= "$item_number1  -  $item_name1 - $quantity1 - $mc_gross_1 \n";
		$orderemailcontent .= "$item_number2  -  $item_name2 - $quantity2 - $mc_gross_2 \n";
		
		//test variations of syntatax using $count.....for variable $item_number[$count] none of which work...
		$orderemailcontent .= "Item Number EG 1: $item_number[$count] \n";
		$orderemailcontent .= "$item_number[$count]. \n";
		$orderemailcontent .= "Item Number EG 2: \$item_number[$count] \n";
		$orderemailcontent .= "\$item_number[$count]. \n";
		$orderemailcontent .= "Item Number EG 3: ".$item_number[$count]." \n";
		$orderemailcontent .= "Item Number EG 4: ".$item_number[1]." \n";
		$orderemailcontent .= "Item Number EG 5: ".$item_number.$count." \n";
		$orderemailcontent .= "Item Number EG 6: ".$item_number['1']." \n";
		$orderemailcontent .= "Item Number EG 7: ".$item_number['$count']." \n";
		$orderemailcontent .= "Item Number EG 8: ".$item_number["$count"]." \n";
		
		$orderemailcontent .= " \n\n";
			
$count=$count+1;
}


$orderemailcontent .= "end email ext";

mail($orderemail, $ordersubject, $orderemailcontent, $orderheaders);

Here's the result of the email..................


Item Number EG 1: t
t.
Item Number EG 2: $item_number[1]
$item_number[1].
Item Number EG 3: t
Item Number EG 4: t
Item Number EG 5: it2m_number21
Item Number EG 6: t
Item Number EG 7: i
Item Number EG 8: t
40 - cataloguenumber-artist-title-label- qty- 11.49
28 - cataloguenumber-artist-title-label- qty- 11.49


Item Number EG 1: 2
2.
Item Number EG 2: $item_number[2]
$item_number[2].
Item Number EG 3: 2
Item Number EG 4: t
Item Number EG 5: it2m_number22
Item Number EG 6: t
Item Number EG 7: i
Item Number EG 8: 2
40 - cataloguenumber-artist-title-label- qty- 11.49
28 - cataloguenumber-artist-title-label- qty- 11.49

Can anyone point me in the right direction as to the syntax I need to use for the while loop.. and shed some light on the reason for the results that are produced in the email..eg. No 5 where 'item' is relaced with 'it2m', dont get that one at all or the 't' or 'i'???

Thanks
Rich
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

Just read your entire post:

What I suggest doing is a print_r($item_number) so you can make sure all of the values are properly in the array. It appears that the values in the array may be incorrect as I would have expected:

Code: Select all

$orderemailcontent .= "Item Number EG 3: ".$item_number[$count]." \n"; 
      $orderemailcontent .= "Item Number EG 4: ".$item_number[1]." \n";
those to work.
richie--c
Forum Newbie
Posts: 6
Joined: Fri Dec 14, 2007 6:16 am

Post by richie--c »

Hi waradim,

yep your right, thats exacly what you do...and the same with a string .= I tried that here...

Code: Select all

$orderemailcontent .= "Item Number EG 3: ".$item_number[$count]." \n";
still didnt work... I did check other scripts and that is generally how its done, but doesnt seem to work for me.

that line give me a 't'

the result should be firstly 40, and then 28 as per the results for:

Code: Select all

$orderemailcontent .= "$item_number1  -  $item_name1 - $quantity1 - $mc_gross_1 \n";
$orderemailcontent .= "$item_number2  -  $item_name2 - $quantity2 - $mc_gross_2 \n";
40 - cataloguenumber-artist-title-label- qty- 11.49
28 - cataloguenumber-artist-title-label- qty- 11.49
richie--c
Forum Newbie
Posts: 6
Joined: Fri Dec 14, 2007 6:16 am

Post by richie--c »

Hi waradim, thanks for your reply...

does the fact that I get the correct result from the following not show that the values are properly in the array :

Code: Select all

$orderemailcontent .= "$item_number1 \n";
$orderemailcontent .= "$item_number2 \n";
or is print_r($item_number) checking for something different

Is when I try to replace the 1, or 2 with [$count] that I get problems.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

The syntax you're looking for is

Code: Select all

"${"item_number$count"} - ${"item_name$count"} - ${"quantity$count"} ....."
yes, double quotes inside double quotes.

However, I find this extremely ugly, it'd be much better to use arrays instead, e.g.

Code: Select all

$cart = array(
	array('name' => 'item foo', 'quantity' => 10, 'price' => 100),
	array('name' => 'item bar', 'quantity' => 10, 'price' => 100),
	etc
)
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Post by Chalks »

I don't know if this makes a difference, or if you took it into account already, but usually arrays start at index 0. You have "$count" assigned to 1 initially. Looks like an "off by one" error to me.
richie--c
Forum Newbie
Posts: 6
Joined: Fri Dec 14, 2007 6:16 am

Post by richie--c »

Hi stereofrog...

Thanks for your post... that works perfectly...

Code: Select all

"${"item_number$count"} - ${"item_name$count"} - ${"quantity$count"} ....."
Do you have any reference that I can look at explaining the syntax...?

Still on a steep learning curve...

And thanks for the pointer for using arrays for the same... will have a go with that too.

Thanks again.
richie--c
Forum Newbie
Posts: 6
Joined: Fri Dec 14, 2007 6:16 am

Post by richie--c »

Hi Chalks,

Thanks for your post... yep your right, arrays start at 0...

On this occasion my order confirmation email is using data integrated with 'Paypal Payments Standard' which requires item 1 in the array to be item 1 (not the usual first array item 0).

Thanks for the heads up though..
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

I think ${...} syntax is described in the manual, "Language syntax" section. Don't know for sure.
As a general rule, expression in curly brackets are used to dynamically generate variables' and object properties' names, for example:

Code: Select all

$foo = "hi\n";

echo $foo; 
echo ${"foo"};
echo ${ chr(102) . str_repeat('o', 2) };
richie--c
Forum Newbie
Posts: 6
Joined: Fri Dec 14, 2007 6:16 am

Post by richie--c »

Thanks to all.....much appreciated.
Post Reply