I am trying to build a shopping cart, I want to store the contents of the cart in an array and store that in a session. I understand sessions but just started using arrays somehow I'm having trouble combining the two. I was hoping someone can at least tell me if I'm on the right track here. This is what I have so far:
<?
session_start();
$cart = array();
$_SESSION['shoppingcart'] = $cart;
if (!isset($_SESSION['subtotal'])) {
$_SESSION['subtotal'] = 0;
$_SESSION['count'] = 0;
}
$cart[$_SESSION['count']]['PID'] = $_GET['item'];
$cart[$_SESSION['count']]['quantity'] = $_GET['quantity'];
$_SESSION['count']++;
if ($_SESSION['status']=="good") {
if (count($cart)==NULL) {
echo "Your shopping cart is empty";
} else {
?>
<table cellpadding=0 cellspacing=0 border=0>
<tr><td>Contents of your shopping cart</td></tr>
<tr><td> </td></tr>
<tr><td>Quantity</td><td>Item</td><td>Price</td></tr>
<?
include 'functions.php';
$table = "catalog";
for ($i=0; $i<count($cart);$i++) {
$pid = $cart[$i]['PID'];
$sqlquery = "SELECT * FROM $table WHERE ID = $pid";
$result = dbconnect($sqlquery);
echo "<tr><td>" . $cart[$i]['quantity'] . "</td><td>" . mysql_result($result,0,"name") . "</td><td>" . mysql_result($result,0,"price") . "</td></tr>";
$_SESSION['subtotal'] += mysql_result($result,0,"price");
mysql_close();
}
?>
<tr><td> </td></tr>
<tr><td>Subtotal</td><td><?=$_SESSION['subtotal'];?></td></tr>
<tr><td> </td></tr>
<tr><td><a href="main.php">Shop some more</a></td></tr>
<tr><td><a href="checkout.php">Check out</a></td></tr>
</table>
<?
}
} else {
include 'logon.php';
}
?>
It works fine when I add one item, then when I try to add another, I get all kinds of mysql errors. Thanks for any help anyone can provide. It is very much appreciated