Jump to content

need a foreach


zang8027

Recommended Posts

I am a little confused here. I have a cart that submits items to paypal, then paypal sends back a serialized URL to my page with the information.

 

So if i use $_GET['item_name1'], it gives me what ever item one is. If i change it to item_name2, if there is a 2, it gives me that name. Same with quantity.

 

Now, i need a way to say, get ALL the params that are sent back, (its in a array thats serialized so i would need $key => $value) but I am not quite sure on how to do this.

 

Here is the code that paypal generated for me..

 

<?php
.......

// parse the data
$lines = explode("\n", $res);
$keyarray = array();
if (strcmp ($lines[0], "SUCCESS") == 0) {
for ($i=1; $i<count($lines);$i++){
list($key,$val) = explode("=", $lines[$i]);
$keyarray[urldecode($key)] = urldecode($val);
}
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment

$itemname2 = $keyarray['item_name2'];
$amount = $keyarray['mc_gross'];
$quantity2 = $keyarray['quantity2'];

echo ("<p><h3>Thank you for your purchase!</h3></p>");

echo ("<b>Payment Details</b><br>\n");
echo ("<li>Name: $userSession</li>\n");
echo ("<li>Item: $itemname2</li>\n");
echo ("<li>Amount: $amount</li>\n");
echo ("<li>quantity: $quantity2</li>\n");
echo ("<li>Restaurant: $restaurantSession</li>\n");
}
else if (strcmp ($lines[0], "FAIL") == 0) {
// log for manual investigation
}

}

fclose ($fp);

?>

 

 

 

Now, pretty much, how can i use a for each to grab each value thats being sent back for quantity and item name.

 

i want my page to print out:

 

Pep. pizza (item1)  1(quantity1)

cheese pizza (item2)  4(quantity2)

 

 

does not need to be formatted, just echo'd

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/145986-need-a-foreach/
Share on other sites

ok thank you.. that got me somewhere.. but not what i need..  any ideas how i can just pull out item_name_(however many) and quantity?

 

 

im getting EVERYTHING thats posted back.. is there like a php command kind of like the mySQL command "LIKE" that i can just pull out item names haha

Link to comment
https://forums.phpfreaks.com/topic/145986-need-a-foreach/#findComment-766553
Share on other sites

here is what i get when i just pull everything from the url (which is encoded in an array)

 

mc_gross = 56.24

protection_eligibility = Eligible

address_status = confirmed

item_number1 =

tax = 0.00

item_number2 =

payer_id = H6RLXPBW3ZDW6

item_number3 =

address_street = 1 Main St

payment_date = 13:16:18 Feb 19, 2009 PST

payment_status = Completed

charset = windows-1252

address_zip = 95131

mc_shipping = 5.44

mc_handling = 0.00

first_name = Test

mc_fee = 1.93

address_country_code = US

address_name = Test User

custom = 3

payer_status = verified

business = [email protected]

address_country = United States

num_cart_items = 3

mc_handling1 = 0.00

mc_handling2 = 0.00

mc_handling3 = 0.00

address_city = San Jose

payer_email = [email protected]

mc_shipping1 = 5.44

mc_shipping2 = 0.00

mc_shipping3 = 0.00

txn_id = 9M838033K29070309

payment_type = instant

last_name = User

address_state = CA

item_name1 = Medium Sausage Pizza

receiver_email = [email protected]

item_name2 = Medium Ham and Pinnapple Pizza

payment_fee = 1.93

item_name3 = Medium Ham and Cheddar Pizza

quantity1 = 1

quantity2 = 2

receiver_id = THLEVDLL2GVXE

quantity3 = 5

txn_type = cart

mc_gross_1 = 11.79

mc_currency = USD

mc_gross_2 = 12.70

mc_gross_3 = 31.75

residence_country = US

transaction_subject = 3

payment_gross = 56.24

 

 

i want this:

 

item_name1 = Medium Sausage Pizza

item_name2 = Medium Ham and Pinnapple Pizza

item_name3 = Medium Ham and Cheddar Pizza

 

Same with quantity because i am sending this via fax or email and I dont really want EVERYTHING sent.. unless i have too

Link to comment
https://forums.phpfreaks.com/topic/145986-need-a-foreach/#findComment-766558
Share on other sites

try

<?php
$test = 'mc_gross = 56.24
protection_eligibility = Eligible
address_status = confirmed
item_number1 =
tax = 0.00
item_number2 =
payer_id = H6RLXPBW3ZDW6
item_number3 =
address_street = 1 Main St
payment_date = 13:16:18 Feb 19, 2009 PST
payment_status = Completed
charset = windows-1252
address_zip = 95131
mc_shipping = 5.44
mc_handling = 0.00
first_name = Test
mc_fee = 1.93
address_country_code = US
address_name = Test User
custom = 3
payer_status = verified
business = [email protected]
address_country = United States
num_cart_items = 3
mc_handling1 = 0.00
mc_handling2 = 0.00
mc_handling3 = 0.00
address_city = San Jose
payer_email = [email protected]
mc_shipping1 = 5.44
mc_shipping2 = 0.00
mc_shipping3 = 0.00
txn_id = 9M838033K29070309
payment_type = instant
last_name = User
address_state = CA
item_name1 = Medium Sausage Pizza
receiver_email = [email protected]
item_name2 = Medium Ham and Pinnapple Pizza
payment_fee = 1.93
item_name3 = Medium Ham and Cheddar Pizza
quantity1 = 1
quantity2 = 2
receiver_id = THLEVDLL2GVXE
quantity3 = 5
txn_type = cart
mc_gross_1 = 11.79
mc_currency = USD
mc_gross_2 = 12.70
mc_gross_3 = 31.75
residence_country = US
transaction_subject = 3
payment_gross = 56.24';
preg_match_all('/item_name(\d+) = (.*)/', $test, $item);
$item = array_combine($item[1], $item[2]);
preg_match_all('/quantity(\d+) = (.*)/', $test, $quantity);
$quantity = array_combine($quantity[1], $quantity[2]);
foreach ($item as $key => $i){
echo $i, ' - ', $quantity[$key], "<br />\n";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/145986-need-a-foreach/#findComment-766604
Share on other sites

i cant figure out how to get all the results into a variable.. i tried putting the

 

foreach ($_POST as $key => $val) {

  return $key . " => " . $val . "<br />";

}

 

 

into a function and putting $test = "".postResults()."": but that only returns the first value (mc_gross).

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/145986-need-a-foreach/#findComment-766653
Share on other sites

well, its probably easier to put the items into the variable like in sasa's $test variable. I am echoing out each of these "$GET" params using a foreach($arraykey as $key=>$val). Thats how I got that entire list to echo onto my page. I need to place that in a variable just like in sasa's example.

Link to comment
https://forums.phpfreaks.com/topic/145986-need-a-foreach/#findComment-766685
Share on other sites

thanks, it works.. tho i had that return function before and it didn't work haha    maybe cause i didnt first declare it as an empty var

 

even though u were running foreach loop, you were returning inside the loop, so the return was executed first time your loop ran, u just had to put the return outside the foreach.

Link to comment
https://forums.phpfreaks.com/topic/145986-need-a-foreach/#findComment-767470
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.