Jump to content

Adding a purchase button to my cart


mikebyrne

Recommended Posts

I want to add a purchase button to my cart. I've a rough idea of what I need to put into the fucntion below but im really not sure how to structure it

 

I know I need to insert

 

SQL2= UPDATE $table SET stockamount = stockamount - 1 WHERE ProductNo = $id

 

A purchase button: '<div><button type="submit">Purchase</button></div>';

 

A Form

 

<form action="cart.php?action=purchase" method="post">

<input type="submit" name="purchase" value="Purchase" />

</form>

 

Any help here would be great

 

 

<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
	return '<p>You have no items in your shopping cart</p>';
} else {
	// Parse the cart session variable
	$items = explode(',',$cart);
	$s = (count($items) > 1) ? 's':'';
	return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
}
}

function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
	$items = explode(',',$cart);
	$contents = array();
	foreach ($items as $item) {
		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
	}
	$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
	$output[] = '<table>';
	foreach ($contents as $id=>$qty) {
		$sql = 'SELECT * FROM product WHERE ProductNo = '.$id;
		$result = $db->query($sql);
		$row = $result->fetch();
		extract($row);
		$output[] = '<tr>';
		$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
		$output[] = '<td>'.$ProductName.' by '.$Productinfo.'</td>';
		$output[] = '<td>€'.$Price.'</td>';
		$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
		$output[] = '<td>€'.($Price * $qty).'</td>';
		$total += $Price * $qty;
		$output[] = '</tr>';
	}
	$output[] = '</table>';
	$output[] = '<p>Grand total: <strong>€'.$total.'</strong></p>';
	$output[] = '<div><button type="submit">Update cart</button></div>';
	$output[] = '</form>';
} else {
	$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/100362-adding-a-purchase-button-to-my-cart/
Share on other sites

What's your actual question, i'm none too clear?

 

What payment method are you eventually using. Other than that you seem to do everything (if duplicate some bits), you got price, qty, name, info, is there a product ref? It generally depends upon what your products are and how you later allow to update the cart, and how that is then transferred into an order, then sent off to the credit site, and how you store their specific info, then once (if they remember to) they return what needs to be done? (p.s. sorry about waffling, just got up!)

something like this??

 

function reduceItem($id, $qty)
{
$query = "UPDATE tablename SET quantity = quantity - '$qty' WHERE tableid = '$id'";
if ($result = mysql_query($query))
{
	$rows = mysql_affected_rows($result);//should be equal to 1 or 0 as an id is being used
}
else
{
	$rows = 0; // unable to carry out query set affected rows to 0
}
return $rows;
}

$amended = reduceItem(14, 1); //reduce item 14 by 1
echo $amended; //1 if the reduction has happened, 0 if no rows affected or error with the query

Ok thanks for your help.

 

One last thing, how would I apply the code to the "purchase" button I'm not to familair with calling functions?

 

A purchase button: '<div><button type="submit">Purchase</button></div>';

 

<form action="cart.php?action=purchase" method="post">

<input type="submit" name="purchase" value="Purchase" />

</form>

 

you would not call the function direct, you would post the values back to the page and have the function at the top of the page, first of all checking to see if the posted value match what is needed

 

if (isset($_POST['purchase']))
{
$amended = reduceItem($_POST['id'], $_POST['qty']); 
if ($amended == 1)
{
	echo "Table updated";
}
else
{
	echo "For some reason table not updated";
}
unset $_POST['id']; // to prevent refresh
unset $_POST['qty'];
}

Archived

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

×
×
  • Create New...

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.