PDA

View Full Version : Help with creating Dynamic PayPal Button


tux
11-06-2009, 02:32 PM
Hello All,

I'm having a hard time building a dynamic PayPal button.

The button requires certain variables for its items so I need to increment each variable by one for every item in my cart.

eg. item_name_1, amount_1
item_name_2, amount_2
item_name_3, amount_3

I can increment the numbers ok using for example this....

$result = mysql_query("SELECT COUNT(*) FROM cart WHERE imageID = '10' ");
$row = mysql_fetch_row($result);
$numrows = $row[0];

$item = "item_name_";
do{
for($i = 1; $i <= $numrows; $i++){
echo $item;
echo $i . "<br />";
}
}while($row = @mysql_fetch_row($result));

and I know how to loop through my cart using a do while clause.

My problem is how to get them to work together so my variable names increment by one for each item loop from my cart.

Hope you can understand this.

Regards, Paul

edbr
11-07-2009, 12:38 AM
not sure , each item needs a unique number?
if so could you not use an auto increment value in your table so item name = $row['itemname']."_".$row['id']

tux
11-07-2009, 12:23 PM
Thanks for your reply edbr,

Yes the PayPal third party cart button needs each item to be numbered consecutively.

I cant use the auto increment in the table as each new button created must have its items start from 1. Auto increment would obviously keep getting bigger. Also, more than one user could be making an order at the same time and the item numbers must be consecutive or they will not be recognised.

ie. item_name_1, item_name_2, item_name_3 would be okay but item_name_1, item_name_3, item_name_4 would fail.

Back to the issue, I know how to increment the numbers using this....

$result = mysql_query("SELECT COUNT(*) FROM orders WHERE imageID = '10' ");
$row = mysql_fetch_row($result);
$numrows = $row[0];

$item = "item_name_";
do{
for($i = 1; $i <= $numrows; $i++){
echo $item;
echo $i . "<br />";
}
}while($row = @mysql_fetch_row($result));

which would output....

item_name_1
item_name_2
item_name_3

with 3 items in the orders table.

My query to get the order is this.....

$result8 = @mysql_query("SELECT * FROM orders INNER JOIN products ON orders.productID = products.productID INNER JOIN images ON orders.imageID = images.imageID
INNER JOIN users ON orders.userID = users.userID WHERE orders.orderNo = '$orderNo' ORDER BY orders.orderID DESC");
$confirmedButton = @mysql_fetch_array($result8);

and in the form for my PayPal button I have......

<form target="paypal" name="paypal" id="paypal" method="post" action="https://www.paypal.com/cgi-bin/webscr">

<input type="image" src="Images/Buttons/button_paynow.gif" alt="Pay Now" title="Pay Now">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="sales@mywebsite.co.uk">
<?php
do{
?>
<input type="hidden" name="item_name_1" value="<?php echo $confirmedButton['title']; ?>, <?php echo $confirmedButton['productName']; ?>">
<input type="hidden" name="amount_1" value="<?php echo $confirmedButton['productPrice']; ?>">
<?php }while($confirmedButton = @mysql_fetch_array($result8)); ?>
<input type="hidden" name="currency_code" value="GBP">

</form>

again we have 3 items in the order so the output source code would be....

<form target="paypal" name="paypal" id="paypal" method="post" action="https://www.paypal.com/cgi-bin/webscr">
<input type="image" src="Images/Buttons/button_paynow.gif" alt="Pay Now" title="Pay Now">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="sales@mywebsite.co.uk">
<input type="hidden" name="item_name_1" value="The Dryers, 9"x6" Print, 12"x10" mount.">
<input type="hidden" name="amount_1" value="10.00">
<input type="hidden" name="item_name_1" value="The Washers, 9"x6" Print, 12"x10" mount.">
<input type="hidden" name="amount_1" value="10.00">
<input type="hidden" name="item_name_1" value="The Roof, 9"x6" Print, 12"x10" mount.">
<input type="hidden" name="amount_1" value="10.00">
<input type="hidden" name="currency_code" value="GBP">

</form>

I need to dynamically change the input names 'item_name_1' and 'amount_1' so that they have consecutive numbers. So how can I combine the code to increment the numbers with the code creating the order info.

Hope this is clearer. Thanks.

Paul

tux
11-08-2009, 11:52 AM
Okay Guys,

I have managed to sort this out so here is my PayPal button form that increments the input names by one. The example has 3 items in the order.

<form target="paypal" name="paypal" id="paypal" method="post" action="https://www.paypal.com/cgi-bin/webscr">
<input type="image" src="Images/Buttons/button_paynow.gif" alt="Pay Now" title="Pay Now">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="sales@mywebsite.co.uk">
<?php
$result = mysql_query("SELECT COUNT(*) FROM orders WHERE orderNO = '$confirmedOrderNo' AND userID = '$userID' ");
$row = mysql_fetch_row($result);
$numrows = $row[0];
do{
for($i = 1; $i <= $numrows; $i++){
?>
<input type="hidden" name="item_name_<?php echo $i ?>" value="<?php echo $confirmedButton['title']; ?>, <?php echo $confirmedButton['productName']; ?>">
<input type="hidden" name="amount_<?php echo $i ?>" value="<?php echo $confirmedButton['productPrice']; ?>">
<?php }while($confirmedButton = @mysql_fetch_array($result8));
}while($row = mysql_fetch_row($result));
?>
<input type="hidden" name="currency_code" value="GBP">
</form>

The output in the source code is then this.....

<form target="paypal" name="paypal" id="paypal" method="post" action="https://www.paypal.com/cgi-bin/webscr">
<input type="image" src="Images/Buttons/button_paynow.gif" alt="Pay Now" title="Pay Now">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="sales@mywebsite.co.uk">
<input type="hidden" name="item_name_1" value="The Dryers, 9"x6" Print, 12"x10" mount.">
<input type="hidden" name="amount_1" value="10.00">
<input type="hidden" name="item_name_2" value="The Dryers, 9"x6" Print, 12"x10" mount.">
<input type="hidden" name="amount_2" value="10.00">
<input type="hidden" name="item_name_3" value="The Dryers, 9"x6" Print, 12"x10" mount.">
<input type="hidden" name="amount_3" value="10.00">
<input type="hidden" name="currency_code" value="GBP">
</form>

Hope this helps someone else.

Regards Paul

tux
11-09-2009, 08:04 PM
On looking at this again I have just noticed that the products have not returned correctly so I could still do with some help on this.

As you will notice in the above post, the input names have incremented okay but the script has just repeated the first product in the query instead of the 3 different ones.

Any ideas with this guys as it is now starting to bug me big time.

Thanks.

Regards Paul.