Jump to content

foreach help please. add items to paypal.


spires

Recommended Posts

Hi,

I am attempting to send paypal mulitple items from my shopping cart.
www.nickyrubin.com/shop.php

I have had it working where you can buy one item from the cart, but not multiple items.
I'm looping through the hidden feilds and change the values e.g title, id, price, for each item.  but, when i have two items in my cart and click checkout, it only displays one item 10 times
(for loop is set to 10, i need to set this to the amount of items that you have in the cart?).

hope this makes sence.
I'm sure i have to use a foreach to pull items out of the array. But i dont know how, any help please

Sorry, i'm not very good at explaining.
if you goto www.nickyrubin.com/shop.php, add some items to the cart then checkout
you will see what its all about.

Thanks for all your help.

[code]
<?php

function showCart() {

$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] =  $contents[$item] + 1;
}

echo '<CENTER>';
echo '<TABLE width="125" class="TLRB_border" cellpadding="1" cellspacing="0" bgcolor="#FFF2DB">';
echo
'<TR bgcolor="#FFB876" class="Title">
<TD colspan="3">
Shopping Cart
</TD>
</TR>
<TR>
<TD width="10">&nbsp;</TD>
<TD class="bold" >Track</TD>
<TD class="bold" width="7">Qty</TD>
</TR>';

foreach ($contents as $id=>$qty) {


$sql = "SELECT * FROM books WHERE id ='$id' ";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$count = mysql_num_rows($result);


 

echo '
<TR>
<TD align="left" width="10"><br><a href="shop.php?action=delete&id='.$row['id'].'" class="link">X&nbsp;</a></TD>
<TD align="left"><br>'.$row['title'].' </TD>
<TD width="7"><br><div align="right">'.$qty.'</div></TD>
</TR>';

$total += $row['price'] * $qty;


}


echo '

<TR>
<TD width="5">&nbsp;</TD>
<TD colspan="2" align="right"><HR width="100"><span>Grand total: <strong>&pound;'.$total.'</strong></span></TD>&nbsp;
</TR>
<TR>
<TD width="5">&nbsp;</TD>
<TD>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">';

$title = $row['title'];
$id = $row['id'];
$price = $row['price'];

foreach ($contents as $id=>$qty) {

for ($i = 0; $i < 10; $i++) {

echo '
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="'.$i.'">
<input type="hidden" name="business" value="nicky@nickyrubin.com">
<input type="hidden" name="item_name_'.$i.'" value="'.$title.'">
<input type="hidden" name="item_number_'.$i.'" value="'.$id.'">
<input type="hidden" name="quantity_'.$i.'" value="'.$qty.'">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="amount_'.$i.'" value="'.$price.'">';
}

}
echo '<input type="image" src="../JPGs/checkout.jpg" name="submit" alt="Make payments with PayPal - its fast, free and secure!">
</form>
</TD>
</TR>';


echo '</TABLE>';
echo '</CENTER>';
} else {
echo '<CENTER>
<TABLE width="125" class="TLRB_border" cellpadding="1" cellspacing="0" bgcolor="#FFF2DB">
<TR bgcolor="#FFB876" class="Title">
<TD>
Shopping Cart
</TD>
</TR>
<TR>
<TD>

<br><div align="center"><span class="loginBox_text">You shopping cart<br> is empty.</span></div>

</TD>
</TR>
</TABLE>
</CENTER>';
}


}


?>
[/code]
Link to comment
Share on other sites

without knowing the requirements of PayPal, I can't tell you exactly how this should be set up, but I can see that there are some problems with the organization of your code blocks. 

everything from
[code]$title = $row['title'];
$id = $row['id'];
$price = $row['price'];

foreach ($contents as $id=>$qty) {

for ($i = 0; $i < 10; $i++) {
[/code]

on should go in the first foreach block, probably after
[code]
$total += $row['price'] * $qty;
[/code]
and the foreach ($contents as $id=>$qty) should be omitted and the code within placed there


I can't understand why the for ($i = 0; $i < 10; $i++)  loop is there.  It doesn't seem to serve any purpose. 
You already have a hidden input for the quantity, so there really isn't any reason to change the i<10 to i<$qty

Also, some of the hidden fields do not have unique names, e.g.
[code]
<input type="hidden" name="upload" value="'.$i.'">
[/code]
and sending multiple inputs with the same name can create problems.

Finally, if there is to be one button to submit all items for payment, there should only be one form block, and the beginning and end should be before and after all the other code.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.