Jump to content

Problem with code....


s_ainley87

Recommended Posts

Hello Everyone,

I have just made a view shopping cart script for my site but when i add something to the cart then try to view the cart it appears empty i think it might be a problem in the code that i cant see, could someone take a look please?
<body>
<?php
error_reporting(E_ALL);
// This page displays the contents of the shopping cart.
// This page also lets the user update the contents of the cart.
// Check if the form has been submitted (to update the cart).
if (isset($_POST['submitted'])) { // Check if the form has been submitted.

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

// Check if the shopping cart is empty.
$empty = TRUE;
if (isset ($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $key => $value) {
	if (isset($value)) {
		$empty = FALSE;	
		break; // Leave the loop.
	}
} // End of FOREACH.
} // End of ISSET IF.

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

require_once ('include/mysql_connect.php'); // Connect to the database.

// Retrieve all of the information for the prints in the cart.
$query = "SELECT product_id, category_name, product_name FROM category, product WHERE category.category_id = product.category_id AND product.product_id IN (";
foreach ($_SESSION['cart'] as $pid => $value) {
	$query .= $pid . ',';
}
$query = substr ($query, 0, -1) . ') ORDER BY product.product_id ASC';
$result = mysql_query ($query,$dbc);

// Create a table and a form.
echo '<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">
<tr>
	<td align="left" width="30%"><b>Category</b></td>
	<td align="left" width="30%"><b>Print Name</b></td>
	<td align="right" width="10%"><b>Price</b></td>
	<td align="center" width="10%"><b>Qauntity</b></td>
	<td align="right" width="10%"><b>Total Price</b></td>
</tr>
<form action="view_cart.php" method="post">
';

// Print each item.
$total = 0; // Total cost of the order.
while ($row = mysql_fetch_array ($result, MYSQLI_ASSOC)) {

	// Calculate the total and sub-totals.
	$subtotal = $_SESSION['cart'][$row['product_id']]['quantity'] * $_SESSION['cart'][$row['product_id']]['price'];
	$total += $subtotal;

	// Print the row.
	echo "	<tr>
	<td align=\"left\">{$row['name']}</td>
	<td align=\"left\">{$row['product_name']}</td>
	<td align=\"right\">\${$_SESSION['cart'][$row['product_id']]['price']}</td>
	<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['product_id']}]\" value=\"{$_SESSION['cart'][$row['product_id']]['quantity']}\" /></td>
	<td align=\"right\">$" . number_format ($subtotal, 2) . "</td>
</tr>\n";
} // End of the WHILE loop.

mysql_close($dbc); // Close the database connection.

// Print the footer, close the table, and the form.
echo '	<tr>
	<td colspan="4" align="right"><b>Total:<b></td>
	<td align="right">$' . number_format ($total, 2) . '</td>
</tr>
</table><div align="center"><input type="submit" name="submit" value="Update My Cart" />	
<input type="hidden" name="submitted" value="TRUE" />
</form><br /><br /><a href="checkout.php"><font size="+2">Checkout</font></a></div>';

} else {
echo '<p>Your cart is currently empty.</p>';
}

?>

Link to comment
https://forums.phpfreaks.com/topic/98195-problem-with-code/
Share on other sites

just realised that the add cart code might be helpful too

 

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>View basket</title>
</head>

<body>
<?php
//this page dispalys the contents of the shopping basket
//this page also lets the user update the contents of their basket

// Check if the form has been submitted (to update the cart).
if (isset($_POST['submitted'])) { // Check if the form has been submitted.

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

// Check if the shopping cart is empty.
$empty = TRUE;
if (isset ($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $key => $value) {
	if (isset($value)) {
		$empty = FALSE;	
		break; // Leave the loop.
	}
} // End of FOREACH.
} // End of ISSET IF.

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

require_once ('include/mysql_connect.php'); // Connect to the database.

// Retrieve all of the information for the prints in the cart.
$query = "SELECT category_name, product_id, product_name FROM category, product WHERE category.category_id = product.category_id AND product.product_id IN (";
foreach ($_SESSION['cart'] as $pid => $value) {
	$query .= $pid . ',';
}
$query = substr ($query, 0, -1) . ') ORDER BY product.product_id ASC';
$result = mysqli_query ($dbc, $query);

// Create a table and a form.
echo '<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">
<tr>
	<td align="left" width="30%"><b>Category</b></td>
	<td align="left" width="30%"><b>Product Name</b></td>
	<td align="right" width="10%"><b>Price</b></td>
	<td align="center" width="10%"><b>Qty</b></td>
	<td align="right" width="10%"><b>Total Price</b></td>
</tr>
<form action="view_cart.php" method="post">
';

// Print each item.
$total = 0; // Total cost of the order.
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)) {

	// Calculate the total and sub-totals.
	$subtotal = $_SESSION['cart'][$row['product_id']]['quantity'] * $_SESSION['cart'][$row['product_id']]['price'];
	$total += $subtotal;

	// Print the row.
	echo "	<tr>
	<td align=\"left\">{$row['category_name']}</td>
	<td align=\"left\">{$row['product_name']}</td>
	<td align=\"right\">\${$_SESSION['cart'][$row['product_id']]['product_price']}</td>
	<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['product_id']}]\" value=\"{$_SESSION['cart'][$row['product_id']]['quantity']}\" /></td>
	<td align=\"right\">$" . number_format ($subtotal, 2) . "</td>
</tr>\n";
} // End of the WHILE loop.

mysqli_close($dbc); // Close the database connection.

// Print the footer, close the table, and the form.
echo '	<tr>
	<td colspan="4" align="right"><b>Total:<b></td>
	<td align="right">$' . number_format ($total, 2) . '</td>
</tr>
</table><div align="center"><input type="submit" name="submit" value="Update My Cart" />	
<input type="hidden" name="submitted" value="TRUE" />
</form><br /><br /><a href="checkout.php"><font size="+2">Checkout</font></a></div>';

} else {
echo '<p>Your cart is currently empty.</p>';
}
print_r($_SESSION);
echo $total;
echo $subtotal;

?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/98195-problem-with-code/#findComment-502466
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.