Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
insert values from indexed array into html form button (PHP)
Old 01-10-2006, 03:52 PM insert values from indexed array into html form button (PHP)
Novice Talker

Posts: 8
Trades: 0
I'm designing an online shop - http://growinggifts.co.uk/shop.php . It has a functioning shopping basket coded with PHP, using a mysql db. However I'd like to create an "add to Paypal cart button", which passes the entire basket contents (item names, prices, quantity) to paypals cart, with one click. The shoppingcart.php file (shown further down) returns an indexed array of all the contents in the basket using the function "get_contents". It is then displayed in basket.php using a table. I'd like to have this "add to paypal cart" in basket.php, below the table of basket contents.
Each item to be added to paypals shopping cart requires 4 lines of form code within the "add to cart" button.
eg.
<input type='hidden' name='item_name_1' value='Medium Ceramic Pot' />
<input type='hidden' name='amount_1' value='18.00' />
<input type='hidden' name='currency_code' value='GBP' />
<input type='hidden' name='quantity_2' value = '2' />
The item_name_? and amount_? must be ascending 1, 2, 3, 4 etc.. The quantity_? and its value is generated from the current basket contents.

Below is all the relevant code. Can anyone show me the code to do this?

First of all ...this is basically what I'm trying to achieve...

<form target="paypal" action="https://www.paypal.com/uk/cgi-bin/webscr" method="post">
<input type='hidden' name='cmd' value='_cart' />
<input type='hidden' name='upload' value='1' />
<input type='hidden' name='business' value='email' />

//need to automatically generate these (example) lines, getting the values from the current basket contents
//item_name_? and amount_? must be ascending 1, 2, 3, 4 etc..
//quantity_? and its value is generated from the current basket contents
//************************************************** *****************
<input type='hidden' name='item_name_1' value='Medium Ceramic Pot' />
<input type='hidden' name='amount_1' value='18.00' />
<input type='hidden' name='currency_code' value='GBP' />
<input type='hidden' name='quantity_2' value = '2' />
//************************************************** *****************
<input type='hidden' name='item_name_2' value='Small Ceramic Pot' />
<input type='hidden' name='amount_2' value='14.00' />
<input type='hidden' name='currency_code' value='GBP' />
<input type='hidden' name='quantity_1' value = '1' />
//************************************************** *****************
<input type='hidden' name='item_name_3' value='Small Hanging Basket' />
<input type='hidden' name='amount_3' value='15.00' />
<input type='hidden' name='currency_code' value='GBP' />
<input type='hidden' name='quantity_1' value = '1' />
//************************************************** *****************
etc...

<input type='submit' name='submit' value='Pay via Paypal' /></form>




basket.php
************************************************** *****************
<?
// Cart Cookie Creator
if(!isset($HTTP_COOKIE_VARS['cart_id'])) {
$cart_id = md5(uniqid(rand()));
setcookie("cart_id", $cart_id, time() + 14400);
} else {
$cart_id = $HTTP_COOKIE_VARS['cart_id'];
}
// Make the Cart Object
require_once("shoppingcart.php");
$cart = new cart($cart_id);
// Select Action
switch($_GET['a']){
case "add":
if(isset($_POST['prodId']) and isset($_POST['quantity'])) $cart->add_item($_POST['prodId'],$_POST['quantity']);
break;

case "update":
$cart->modify_quantity($_POST["prodId"],$_POST["quantity"]);
break;

case "delete":
$cart->delete_item($_POST['prodId']);
break;

case "clear":
$cart->clear_cart();
break;
}
// Begin Cart Display
$cartContents = $cart->get_contents();
$cart_tbl = "
<!-- Begin Cart Display -->
<table summary='Your shopping cart' id='cart'>
<tr>
<th>Item</th>
<th>Product Name</th>
<th>Quantity</th>
<th>Price</th>
<th>&nbsp;</th>
</tr>";
if(count($cartContents) <= 0){ //nothing in cart
$cart_tbl .= "
<tr>
<td class='items' colspan='5'>Your cart is empty</td>
</tr>";

}else{ //stuff in the cart so show it
for($i=0; $i < count($cartContents); $i++){
$cart_tbl .= "
<tr>
<td class='items'>" . $cartContents[$i][0] . "</td>
<td class='items'>" . $cartContents[$i][1] . "</td>
<td class='items'>
<form action=\"basket.php?a=update\" method=\"POST\">
<input type='hidden' name='prodId' value='". $cartContents[$i][0] ."' />
<input type='text' name='quantity' size='3' value='" . $cartContents[$i][2] . "' />
<input type='submit' value='update' />
</form>
</td>
<td class='items'>£". $cartContents[$i][3] ."</td>
<td class='items'>
<form action=\"basket.php?a=delete\" method=\"POST\">
<input type='hidden' name='prodId' value='". $cartContents[$i][0] ."' />
</>
<input type='submit' value='remove item' />
</form>
</td>
</tr>";}}
$cart_tbl .= "
<tr>
<td colspan='3' class='empty'>&nbsp;</td><td class='total'>". $cart->cart_total() ."</td>
<td class='items'><form action=\"basket.php?a=clear\" method=\"post\"><input type=\"submit\" value=\"clear cart\" /></form></td>
</tr>";
$cart_tbl .= "
</tr>
</table>
<!-- End Cart Display -->\n\n\n";


//add all basket contents to paypal cart (the bit I'm stuck on)

<form target="paypal" action="https://www.paypal.com/uk/cgi-bin/webscr" method="post">
<input type='hidden' name='cmd' value='_cart' />
<input type='hidden' name='upload' value='1' />
<input type='hidden' name='business' value='email' />

//need to automatically generate these (example) lines, getting the values from the current basket contents
//item_name_? and amount_? must be ascending 1, 2, 3, 4 etc..
//quantity_? and its value is generated from the current basket contents
//************************************************** *****************
<input type='hidden' name='item_name_1' value='Medium Ceramic Pot' />
<input type='hidden' name='amount_1' value='18.00' />
<input type='hidden' name='currency_code' value='GBP' />
<input type='hidden' name='quantity_2' value = '2' />
//************************************************** *****************
<input type='hidden' name='item_name_2' value='Small Ceramic Pot' />
<input type='hidden' name='amount_2' value='14.00' />
<input type='hidden' name='currency_code' value='GBP' />
<input type='hidden' name='quantity_1' value = '1' />
//************************************************** *****************
<input type='hidden' name='item_name_3' value='Small Hanging Basket' />
<input type='hidden' name='amount_3' value='15.00' />
<input type='hidden' name='currency_code' value='GBP' />
<input type='hidden' name='quantity_1' value = '1' />
//************************************************** *****************
etc...

<input type='submit' name='submit' value='Pay via Paypal' /></form>

?>


relevant functions to this problem in shoppingcart.php
************************************************** *****************

// Adapted from John Coggeshall's tutorial at:
// http://www.zend.com/zend/spotlight/p...ping-cart1.php

function get_contents() {

//get stuff in the cart and return in indexed array
$q1 = "SELECT * FROM ". $this->cart_table. " WHERE session='".$this->cart_id."'";
$incart_r = mysql_query($q1,$this->dblink) or die(mysql_error());

$contents = array();

while($incart = mysql_fetch_array($incart_r)){
//get the item's price first
$q2 = "SELECT price, prodName FROM ". $this->inv_table. " WHERE prodId='".$incart["product"]."'";
$prodResult = mysql_query($q2) or die(mysql_error());
$name = mysql_result($prodResult,0,"prodName");
$price = mysql_result($prodResult,0,"price");


//build array of info
$item = array($incart['product'], $name, $incart['quantity'],$price);
array_push($contents,$item);
}

return $contents;
}


function quant_items() {
$contents = $this->get_contents();
$q = 0;
for($i=0; $i < count($contents); $i++){
$q = $q + $contents[$i][2];
}
return $q;
}
boognish is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-10-2006, 05:03 PM
TrentonD's Avatar
Ultra Talker

Posts: 253
Location: Calgary, Alberta, Canada
Trades: 0
Hi there, unfortunatly I don't have the right amount of experience in the right area (I think..) for what you are trying to achieve.

If no one here replies or helps, try checking out www.paypaldev.org, I have used them in the past, and they helped a lot with paypal related scripts.

Good Luck!
__________________
Signature Coming Soon! :)
Please login or register to view this content. Registration is FREE
TrentonD is offline
Reply With Quote
View Public Profile Visit TrentonD's homepage!
 
Old 01-10-2006, 05:31 PM
Novice Talker

Posts: 8
Trades: 0
Thanks for the reply! That was the first place I tried actually. I posted a couple of weeks ago. http://www.paypaldev.org/topic.asp?TOPIC_ID=11969. One person replied, but the answer only got me so far. Since then no one seems to have picked up on it, so I thought I'd try here. If anyone can enlighten me on a solution I'd be most grateful!
boognish is offline
Reply With Quote
View Public Profile
 
Old 01-10-2006, 08:20 PM
Novice Talker

Posts: 8
Trades: 0
I figured it out! If anyone wants this for reference -
Heres the solution;

for($i=0; $i < count($cartContents); $i++){
$val = $i + 1;
$paypalitems .= "
<input type='hidden' name='item_name_$val' value='". $cartContents[$i][1] ."' />
<input type='hidden' name='amount_$val' value='". $cartContents[$i][3] ."' />
<input type='hidden' name='currency_code' value='GBP' />
<input type='hidden' name='quantity_$val' value = '" . $cartContents[$i][2] . "' />
";
}

<?
echo $paypalitems;
?>
boognish is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to insert values from indexed array into html form button (PHP)
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.22342 seconds with 12 queries