Jump to content
Larry Ullman's Book Forums

Shopping Cart Issue: View_Cart.php Ch 19


Recommended Posts

This is an adaptation of the view_cart.php script in ch 19.  It had worked previously until I changed servers/web hosting.  It may be coincidental, however, when changing cart quantity to zero, it will either throw an error (undefined total variable) or add additional products to cart which were previously deleted in the same session.  All other functions work propertly, including "add to cart" and "checkout" when quantities are not changed to zero.  Any suggestions would be greatly appreciated. 

<div class="generalarticle">
	
	<?php // add_cart.php
$cart = $_SESSION['cart'];

// Check if the form has been submitted (to update the cart):
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

	// Change any quantities:
	foreach ($_POST['qty'] as $k => $v) {

		// Must be integers!
		$pid = (int) $k;
		$qty = (int) $v;
		
		if ( $qty == 0 ) { // Delete.
			unset ($_SESSION['cart'][$pid]);
		} elseif ( $qty > 0 ) { // Change quantity.
			$_SESSION['cart'][$pid]['quantity'] = $qty;
		}
		
	} // End of FOREACH.
	
} // End of SUBMITTED IF.

// Display the cart if it's not empty...
if (!empty($_SESSION['cart'])) {

	// Retrieve all of the information for the prints in the cart:
	require ('../mysqli_connect.php'); // Connect to the database.
	$q = "SELECT * FROM products WHERE productID IN (";
	foreach ($_SESSION['cart'] as $pid => $value) {
		$q .= $pid . ',';
	}
	$q = substr($q, 0, -1) . ') ORDER BY productName ASC';
	$r = mysqli_query ($dbc, $q);
	
	// Create a form and a table:
	echo '<form action="https://www.mysite.net/view-cart/" method="post">
	<fieldset class="checkout">
	<legend class="checkout">My Shopping Cart</legend>
	<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">
	<tr class="checkout" >
		<td class="checkout" align="left" width="30%"><b>Item Name</b></td>
		<td class="checkout" align="right" width="10%"><b>Price</b></td>
		<td class="checkout" align="right" width="10%"><b>Shipping</b></td>
		<td class="checkout" align="center" width="10%"><b>Qty</b></td>
		<td class="checkout" align="right" width="10%"><b>Total Price</b></td>
	</tr>
	';

	// Print each item...
	$total = 0; // Total cost of the order.
	while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {
	
		// Calculate the total and sub-totals.
		$subtotal = (($_SESSION['cart'][$row['productID']]['quantity'] * $_SESSION['cart'][$row['productID']]['price']) + ($_SESSION['cart'][$row['productID']]['quantity'] * $_SESSION['cart'][$row['productID']]['shipping']));
		$total += $subtotal;
		
		// Print the row:
		echo "\t<tr class=\"checkout\">
		<td class=\"checkout\" align=\"left\">{$row['productName']}</td>
		<td class=\"checkout\"  align=\"right\">\${$_SESSION['cart'][$row['productID']]['price']}</td>
		<td class=\"checkout\"  align=\"right\">\${$_SESSION['cart'][$row['productID']]['shipping']}</td>
		<td class=\"checkout\"  align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['productID']}]\" value=\"{$_SESSION['cart'][$row['productID']]['quantity']}\" /></td>
		<td class=\"checkout\"  align=\"right\">$" . number_format ($subtotal, 2) . "</td>
		</tr>\n";
	} // End of the WHILE loop.
	mysqli_close($dbc); // Close the database connection.

	// Print the total, close the table, and the form:
	echo '<tr>
		<td class="checkout" colspan="4" align="right"><b>Total:</b></td>
		<td class="checkout"  align="right">$' . number_format ($total, 2) . '</td>
	</tr>
	</table>
	<div align="center"><input type="submit" name="submit" value="Update My Cart" /></div>
	</form>
	
	<p align="center">Enter a quantity of 0 to remove an item.
	<br /><br /><a href="https://www.mysite.net/checkout/">Checkout</a> or <a href="https://www.mysite.net/real-estate-advertisement-packages/">Continue Shopping</a>.</p></fieldset>';

} else {
	echo '<p>Your cart is currently empty.</p>';
}
$_SESSION['total'] = $total; // carry total to checkout
$_SESSION['cart'] = $cart; // carry total to checkout
?>
</div>
<div class="sidebar"></div>
<div class="spacer"></div>
</div></div>
Link to comment
Share on other sites

 Share

×
×
  • Create New...