Jump to content

Archived

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

spires

foreach help please. add items to paypal.

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]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Hi, thanks for your reply.

I think i got a better idea now.

Thanks

Share this post


Link to post
Share on other sites

×

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.