Jump to content

Loops


Schlo_50

Recommended Posts

I have a shopping cart (3rd party) and im trying to integrate Paypal with it. At the moment I can't seem to include a loop for the incrementation of form names that Paypal require. (The first item included in the cart should be defined with parameters ending in "_1", such as "item_name_1", "amount_1", etc. Similarly the second item should be denoted with variables like "item_name_2", "amount_2", etc.)

 

When I view source after running my code I get:

 

<input type="hidden" name="item_name_1" value="Mustek ScanExpress">
<input type="hidden" name="amount_1" value="30.00">

<input type="hidden" name="item_name_1" value="Reconditioned 17" CRT Monitor">
<input type="hidden" name="amount_1" value="32.00">

 

and not:

 

<input type="hidden" name="item_name_1" value="Mustek ScanExpress">
<input type="hidden" name="amount_1" value="30.00">

<input type="hidden" name="item_name_2" value="Reconditioned 17" CRT Monitor">
<input type="hidden" name="amount_2" value="32.00">

 

This is my PHP code, which runs a loop to find the item names and prices in an array. Inside that loops I have another loop which tries to increment the form names Paypal requires.

 

  foreach($bcartfile as $bKey => $Val){
$Data[$bKey] = explode("|", $Val);
$item = $Data[$bKey][0];
$pid = $Data[$bKey][1];
$price = $Data[$bKey][2];
   $bstr = $cartfile[$bKey];
   $bstr = explode('|', $str);
   $btotal += $bstr[2];
$fulltotal = $total+$pprice;
$numberOfItems = 1;
for ($i = 1; $i<=$numberOfItems; $i++) {

print "<input type=\"hidden\" name=\"item_name_$i\" value=\"$item\">";
print "<input type=\"hidden\" name=\"amount_$i\" value=\"$price\">";
}

 

Can anyone help?

Link to comment
https://forums.phpfreaks.com/topic/97065-loops/
Share on other sites

Like this?

 

$numberOfItems = 2;
  print "<p><font size=\"3\"><b>Confirm Order</b></font></p><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
   foreach($bcartfile as $bKey => $Val){
$Data[$bKey] = explode("|", $Val);
$item = $Data[$bKey][0];
$pid = $Data[$bKey][1];
$price = $Data[$bKey][2];

    $bstr = $cartfile[$bKey];
    $bstr = explode('|', $str);
    $btotal += $bstr[2];
$fulltotal = $total+$pprice;





print "<input type=\"hidden\" name=\"item_name_$i\" value=\"$item\">";
print "<input type=\"hidden\" name=\"amount_$i\" value=\"$price\">";



print "<tr>";
print "<td width=\"65%\" valign=\"top\">$item</td>";
print "<td width=\"35%\" valign=\"top\"><p align=\"right\">£$price</p></td>";
print "</tr>";
$numberOfItems++; 
   }

Link to comment
https://forums.phpfreaks.com/topic/97065-loops/#findComment-496747
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.