Jump to content

need help on shoping cart check out!


lukelee

Recommended Posts

  Hi, guys, I am making a simple shopping cart, now i got problem on check out function. i want to save the product information into database after click "checkout".

  because people may have different products in their shopping cart, so i made an array to display the items. but when I POST those items to next step which is check out, it only posts the latest item's information.

  can anyone help? thanks, here is a part of my code:

 

function checkout() {
	$output[] = '<form action="checkout_process.php" method="post" id="cart">';
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[] = '<table>';
	foreach ($contents as $id=>$qty) {
		$sql = 'SELECT * FROM product WHERE product_id = '.$id;
		$result = $db->query($sql);
		$row = $result->fetch();
		extract($row);
		$output[] = '<tr><td><input type="hidden" name="model" value="'.$model.'"/></td>';
		$output[] = '<td><input type="hidden" name="quantity'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
		$output[] = '<td><input type="hidden" name="price" value="'.$price.'"/></td>';
		$output[] = '<td><input type="hidden" name="price" value="'.($price * $qty).'"/></td>';
		$total += $price * $qty;
		$output[] = '</tr>';
	}

	$output[] = '</table>';
	$output[] = '<div><button type="submit">check out</button></div>';
	$output[] = '</form>';
} else {
	$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}

Link to comment
https://forums.phpfreaks.com/topic/142779-need-help-on-shoping-cart-check-out/
Share on other sites

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.