PHP PayPal Button. Increment inputs during while loop.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tuxiow
Forum Newbie
Posts: 8
Joined: Mon Nov 09, 2009 2:33 pm

PHP PayPal Button. Increment inputs during while loop.

Post by tuxiow »

Hello all, new here and quite a novice with PHP but am learning.

I am hoping someone could help me with a problem I have building a PayPal button for a third party cart.

I need to increment each input name in my form so that each item_name and amount is numbered like this......

Code: Select all

<input type="hidden" name="item_name_01" value="item01"> 
<input type="hidden" name="amount_01" value="amount01"> 
<input type="hidden" name="item_name_02" value="item02"> 
<input type="hidden" name="amount_02" value="amount02">
<input type="hidden" name="item_name_03" value="item03"> 
<input type="hidden" name="amount_03" value="amount03">
and so on, depending how many items are in the cart.

Here is my attempt so far which sort of works. It increments the item_names and amounts but only returns the first
item in the cart for each item_name etc..

My query to get the items.....

Code: Select all

$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);
My form including my attempt to get the item_names to increment with the items......

Code: Select all

<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>
and the output in the source code when the page is run.....

Code: Select all

<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 Roof, 9"x6" Print, 12"x10" mount."> 
   <input type="hidden" name="amount_1" value="10.00"> 
 
   <input type="hidden" name="item_name_2" value="The Roof, 9"x6" Print, 12"x10" mount."> 
   <input type="hidden" name="amount_2" value="10.00"> 
 
   <input type="hidden" name="item_name_3" value="The Roof, 9"x6" Print, 12"x10" mount."> 
   <input type="hidden" name="amount_3" value="10.00"> 
   <input type="hidden" name="currency_code" value="GBP">
</form>
As you can see the input names have incremented okay but the values are the same when they
should be 3 different items.

I hope I have explained this okay. Post if I havn't and I'll try to explain it better.

Hopefully someone can help.

Thanks. Paul
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: PHP PayPal Button. Increment inputs during while loop.

Post by josh »

Code: Select all

 
$i =0;
while( $i < 50 )
{
echo '<this could be a tag ' . $i . '>' . $i . '</this>';
$i++;
}
 
tuxiow
Forum Newbie
Posts: 8
Joined: Mon Nov 09, 2009 2:33 pm

Re: PHP PayPal Button. Increment inputs during while loop.

Post by tuxiow »

Thanks for your reply,

Not sure what you mean here.

How would this be applied in my code?

The input name is coming from my for loop (getting the number of rows in the query) whilst the
input value is coming from my while loop (getting the item info from the other query).

How can I combine them or am I trying to do this the wrong way.

Thanks again, Paul
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: PHP PayPal Button. Increment inputs during while loop.

Post by josh »

Could you var_dump or print_r your input array and show me example html you would like to generate?
tuxiow
Forum Newbie
Posts: 8
Joined: Mon Nov 09, 2009 2:33 pm

Re: PHP PayPal Button. Increment inputs during while loop.

Post by tuxiow »

Hello again Josh,

I have been playing with this again after your suggestion and now have it working but am not sure if it is a good or correct way of doing it.

There are 3 items in the cart!

To start with I had this.....

Code: Select all

<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 
$i = 1;
while($confirmedButton    = @mysql_fetch_array($result8)){
$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
}
?> 
<input type="hidden" name="currency_code" value="GBP">
</form>
which nearly worked but seems to miss out one of the results. (any idea why??)

It outputs this.....

Code: Select all

<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_2" value="The Washers, 9"x6" Print, 12"x10" mount."> 
   <input type="hidden" name="amount_2" value="10.00"> 
   <input type="hidden" name="item_name_3" value="The Roof, 9"x6" Print, 12"x10" mount."> 
   <input type="hidden" name="amount_3" value="10.00"> 
    <input type="hidden" name="currency_code" value="GBP">
  </form>
As you can see item_name_1 and amount_1 are missing??

I have no idea why this is so. What I then tried was this.......

Code: Select all

<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="<?php echo $confirmedButton['title']; ?>, <?php echo $confirmedButton['productName']; ?>"> 
   <input type="hidden" name="amount_1" value="<?php echo $confirmedButton['productPrice']; ?>"> 
<?php 
$i = 1;
while($confirmedButton    = @mysql_fetch_array($result8)){
$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
}
?> 
   <input type="hidden" name="currency_code" value="GBP">
</form>
I have hard coded the inputs for the first record and now the output is this......

Code: Select all

<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 Washers, 9"x6" Print, 12"x10" mount."> 
   <input type="hidden" name="amount_2" value="10.00"> 
   <input type="hidden" name="item_name_3" value="The Roof, 9"x6" Print, 12"x10" mount."> 
   <input type="hidden" name="amount_3" value="10.00"> 
 
   <input type="hidden" name="currency_code" value="GBP">
 </form>
This is the output that I need although I am not totally sure that the code is good or is the best way or even the correct way to do this.

Hope you understand what it is that I have been trying to acheive and I look forward to your input and advice.

Thanks Josh.

Regards, Paul
tuxiow
Forum Newbie
Posts: 8
Joined: Mon Nov 09, 2009 2:33 pm

Re: PHP PayPal Button. Increment inputs during while loop.

Post by tuxiow »

Just bumping this so it gets noticed again.
Post Reply