Still need to sort the following:
- Add up a total amount in the cart
Tell it to add a just quantity if photo in same size is already in the cart
It's getting there slowly.
Moderator: General Moderators
Code: Select all
if (isset($_GET['new']))
{
$photo_id = $_POST['photoId'];
$connection = mysql_connect('localhost', 'user', 'password') or die ('Unable to connect!');
mysql_select_db('db') or die ('Unable to select database!');
$result = mysql_query("SELECT * FROM shop_items WHERE id = $photo_id");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$photo_link = $row['photo_link'];
$photo_name = $row['photo_name'];
}
$size_id = $_POST['sizeId'];
$photo_size = $_POST['size'];
$price = $_POST['price'];
$qty = $_POST['qty'];
$item_id = $photo_id . $size_id; //Create an index for each photo and size
//make sure we have an array for the cart
if (!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
}
if (array_key_exists($item_id, $_SESSION['cart']))
{ //If the key already exists
$row = $item_id; //Find the correct row
$_SESSION['cart'][$row]['qty'] = $_SESSION['cart'][$row]['qty'] + $qty; //Increase the quantity
$_SESSION['qty'] = $_SESSION['qty'] + $qty;
}
else
{ //Else add to cart
$_SESSION['cart'][$item_id] = array('photoId' => $photo_id, 'photo' => $photo_link, 'name' => $photo_name, 'sizeId' => $size_id, 'size' => $photo_size, 'price' => $price, 'qty' => $qty);
$_SESSION['qty'] = $_SESSION['qty'] + $qty;
}
unset ($_GET['new']);
mysql_free_result($result);
mysql_close($connection);
header("Location: view_cart.php");
}
?>
Code: Select all
Array
(
[11] => Array
(
[lineTotal] => 3.99
)
[12] => Array
(
[lineTotal] => 5.99
)
[14] => Array
(
[lineTotal] => 19.98
)
)