Hello all,
I'm in the process of creating my first shopping cart. I seem to be able to add a product ok as when I test it I get Array ( [6] => 4 ) displayed. Though when I go to view the cart there are no products listed, and the test only displays Array. I'm guessing that session is ending.
heres my code to view the cart.
<?
if (isset ($_POST['submit'])) {
foreach ($_POST['qty'] as $key => $value) {
if (($value == 0) AND (is_numeric ($value))) {
unset ($_SESSION['cart'][$key]);
} elseif ( is_numeric ($value) AND ($value > 0) ) {
$_SESSION['cart'][$key] = $value;
}
}
}
$empty = TRUE;
if (isset ($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $key => $value) {
if (isset($value)) {
}
}
}
if (!$empty) {
$in = implode(',',$_SESSION['cart']);
$query = 'SELECT * FROM prod_list IN ($in) ORDER BY prod_name ASC';
$result = mysql_query ($query);
echo '<table border="0" align="center">
<tr>
<td align="center"><span class="text">Product Name</span></td>
<td align="center"><span class="text">Price</span></td>
<td align="center"><span class="text">Qty</span></td>
<td align="center"><span class="text">Total Price</span></td>
</tr>
<form action="view_cart.php" method="post">
';
$total = 0;
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
$subtotal = $_SESSION['cart'][$row['prod_id']] * $row['prod_price'];
$total += $subtotal;
echo "
<tr>
<td align=\"center\"><span class=\"alt_text\">{$row[prod_name]}</span></td>
<td align=\"center\"><span class=\"alt_text\">{$row[prod_price]}</span></td>
<td align=\"center\"><span class=\"alt_text\"><input type=\"text\" size=\"3\" name=\"qty[{$row[prod_id]}]\" value=\"{$_SESSION['cart'][$row['prod_id']]}\" /></span></td>
<td align=\"center\"><span class=\"alt_text\">$" . number_format ($subtotal, 2) . "</span></td>
</tr>\n";
}
echo ' <tr>
<td colspan="4" align="right"><span class="text">Total:</span></td>
<td align="center"><span class="alt_text">$' . number_format ($total, 2) . '</span></td>
</tr>
</table><div align="center"><input type="submit" name="submit" value="Update Cart" />
</form><br /><br /><a href="checkout.php" class="link">Checkout</a></div>';
} else {
echo '<p class="text">Your cart is empty.</p>';
}
?>
Thanks for your help,
D