Schlo_50 Posted March 20, 2008 Share Posted March 20, 2008 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 More sharing options...
RMcLeod Posted March 20, 2008 Share Posted March 20, 2008 Okay there is no need for the nested for loop. Take $numberofItems out of the foreach loop and set it before the loop starts. Remove the for loop altogether but leave the print statemnts intact. Now add $numberOfItems++; as the last line of the loop. Link to comment https://forums.phpfreaks.com/topic/97065-loops/#findComment-496700 Share on other sites More sharing options...
Schlo_50 Posted March 20, 2008 Author Share Posted March 20, 2008 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 More sharing options...
Schlo_50 Posted March 20, 2008 Author Share Posted March 20, 2008 Solved. Thanks for the input RMc! Link to comment https://forums.phpfreaks.com/topic/97065-loops/#findComment-496753 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.