PDA

View Full Version : insert values from indexed array into html form button (PHP)



boognish
10 Jan 2006, 02:27 PM
I'm designing an online shop - http://growinggifts.co.uk/shop.php . I already have an existing shopping cart with its own basket. I'd like to give the customer various options of payment, one of which is paypal. I'd like to pass all the item information - product names, prices and quantities to a paypal shopping basket.
My problem is - The contents of the basket are returned using an indexed array. I know how display this kind of array with tables, but I don't know how to pass all these seperate values into the appropriate places in form buttons. I just want one single "pay via paypal" button in basket.php, which adds all items in the basket at once to paypals shopping cart. The way to do this is to generate form code of all the product values from the current basket within this form button.
So essentially my question is; what is the code to get the baskets values from the indexed array into the appropiate automatically generated form code within the "pay via paypal" button?

Below is all the relevant code.

First of all ...this is basically what I'm trying to achieve...
Each item to be added to paypals shopping cart requires 4 lines of form code within the "add to cart" button.

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


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>

?>

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.



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

// Adapted from John Coggeshall's tutorial at:
// http://www.zend.com/zend/spotlight/php-shopping-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
10 Jan 2006, 07:27 PM
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;
?>