Forum Moderators: coopster

Message Too Old, No Replies

PHP multiple array values

         

sblades

6:46 pm on Apr 6, 2005 (gmt 0)

10+ Year Member



Hi all,

I've lurked here for quite some time, but this is my first posting.

I have an order form with checkboxes for customers to select items for purchase (Page1). Submitting that form takes the customer to a confirmation page (Page2), then finally, they submit their order to our site's payment gateway. Our payment gateway issues a Merchant Receipt email that goes out to my client's fulfillment house.

Our fulfillment house has very specific requirements for the formatting of the Merchant Receipt email. In particular, the part which lists the customer's purchase items must appear exactly like so...

ITEM01 : CF
QTY01 : 60

ITEM02 : DCF
QTY02 : 120

ITEM03 : XCF
QTY03 : 180

Here's my code...

<?php

if (isset($_POST['product01'])) {
$codes[] = "CF";
$qtys[] = "60";
}
if (isset($_POST['product02'])) {
$codes[] = "DCF";
$qtys[] = "120";
}

if (isset($_POST['product03'])) {
$codes[] = "XCF";
$qtys[] = "180";
}

$cn = count($codes);
$ci = 0;
while($ci < $cn) {
$ci++;
}

$qn = count($qtys);
$qi = 0;
while($qi < $qn) {
$qi++;
}

$TCA_codes = $codes;
$TCA_qtys = $qtys;

if (!empty($TCA_codes)) {
foreach($TCA_codes as $cid => $pcode){
foreach($TCA_qtys as $qid => $pqty){
if ($cid == $qid){
$cnumber = $cid + 1;
$qnumber = $qid + 1;
$Purchases = "ITEM0{$cnumber} : {$pcode}\nQTY0{$qnumber} : {$pqty}\n";
print '<pre>';
print "$Purchases";
print '</pre>';
}
}
}
}
?>

With no way of accessing our payment gateway's sendmail for tests (aside from placing "live" orders), I'm testing the email's output in HTML, as a Page3.

Testing the code above, I have no problem getting it to print what I want on Page2 (but only inside the brackets, as above). When I try to pass $Purchases to Page3, it only returns the values for the last selected item...

ITEM03 : XCF
QTY03 : 180

I presume this is a variable scope problem. Can someone suggest a solution?

ironik

10:25 pm on Apr 6, 2005 (gmt 0)

10+ Year Member



You need to change the assignment for purchases to

$Purchases .= "ITEM0{$cnumber} : {$pcode}\nQTY0{$qnumber} : {$pqty}\n";

Which also means you need to set it before the loop:

$Purchases = "";

It'd probably be best if you used a temporary variable in the loop called purchase, then assign the purchase to purchase(s):

$Purchases = "";
if (!empty($TCA_codes)) {
foreach($TCA_codes as $cid => $pcode){
foreach($TCA_qtys as $qid => $pqty){
if ($cid == $qid){
$cnumber = $cid + 1;
$qnumber = $qid + 1;
$Purchase = "ITEM0{$cnumber} : {$pcode}\nQTY0{$qnumber} : {$pqty}\n";
$Purchases .= $purchase;
print '<pre>';
print "$Purchase";
print '</pre>';
}
}
}
}

That way purchase holds the current purchase in the loop, and Purchases holds the final value you will send.

sblades

11:05 pm on Apr 6, 2005 (gmt 0)

10+ Year Member



Very clever. Splitting up & concatenating the variable to surround the loop did the job.

Thanks much, ironik.

coopster

11:58 am on Apr 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



And welcome to WebmasterWorld, sblades. Glad you're no longer a lurker ;)