update code
if(isset($_POST["type"]) && $_POST["type"]=='add')
{
$product_id = (int) $_POST["product_id"];
$product_qty = (int) $_POST["product_qty"]; //product code
$product_size_id = $_POST["product_size"]; //product size
$prosize=mysqli_query($connection,"SELECT * FROM size where SizeID = $product_size_id");
$prosizename=mysqli_fetch_array($prosize,MYSQLI_ASSOC);
$product_size=$prosizename['Name'];
if(isset($product_size_id)){
$product_size=$prosizename['Name'];
}else{
$product_size="default";
}
$return_url = base64_decode($_POST["return_url"]); //return url
//MySqli query - get details of item from db using product code
$results = mysqli_query($connection,"SELECT * FROM products WHERE id='$product_id' LIMIT 1");
$obj = mysqli_fetch_array($results,MYSQLI_ASSOC);
if ($results) { //we have the product info
//prepare array for the session variable
$new_product = array(array('name'=>$obj['name'], 'id'=>$product_id, 'size'=>$product_size, 'qty'=>$product_qty, 'price'=>$obj['amount']));
if(isset($_SESSION["products"])) //if we have the session
{
$found = false; //set found item to false
foreach ($_SESSION["products"] as $cart_itm) //loop through session array
{
if($cart_itm["id"] == $product_id){ //the item exist in array
$product[] = array('name'=>$cart_itm["name"],
'id'=>$cart_itm["id"],
'size'=>$product_size,
'qty'=>$product_qty+$cart_itm['qty'],
'price'=>$cart_itm["price"]);
$found = true;
}else{
//item doesn't exist in the list, just retrive old info and prepare array for session var
$product[] = array('name'=>$cart_itm["name"],
'id'=>$cart_itm["id"],
'size'=>$cart_itm["size"],
'qty'=>$cart_itm["qty"],
'price'=>$cart_itm["price"]);
}
}
if($found == false) //we didn't find item in array
{
//add new user item in array
$_SESSION["products"] = array_merge($product, $new_product);
}
else{
//found user item in array list, and increased the quantity
$_SESSION["products"] = $product;
}
}else{
//create a new session var if does not exist
$_SESSION["products"] = $new_product;
}
}
//redirect back to original page
header('Location:'.$return_url);
}
product code
<?php session_start(); ?>
<?php require_once("../includes/connection.php");?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
<link href="css/style1.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="products-wrapper">
<h1>Products</h1>
<div class="products">
<?php
//current URL of the Page. cart_update.php redirects back to this URL
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);?>
<?php
$results = mysqli_query($connection,"SELECT * FROM products ORDER BY id ASC");
$all='';
if ($results) {
//fetch results set as object and output HTML
while($obj = mysqli_fetch_array($results,MYSQLI_ASSOC)){
$id=$obj['id'];
$qry_size=mysqli_query($connection,"SELECT * FROM productsizes WHERE ProductID = $id");
while($sizeopt=mysqli_fetch_array($qry_size,MYSQLI_ASSOC)){
$psizeID=$sizeopt['SizeID'];
$sizes=mysqli_query($connection,"SELECT * FROM size where SizeID = $psizeID");
$sizename=mysqli_fetch_array($sizes,MYSQLI_ASSOC);
$namesize=$sizename['Name'];
$all.= "<option value=\"$psizeID\">$namesize</option>";
}
$row_cnt = mysqli_num_rows($qry_size);
$query = mysqli_query($connection,"SELECT * FROM `images` WHERE productID = $id LIMIT 1 ");
$img = mysqli_fetch_array($query,MYSQLI_ASSOC);
$src = $img['file_name'];
echo '<div class="product">';
echo '<form method="post" action="product_cart_update.php">';
echo "<div class=\"product-thumb\"><img src=\"$src\" width=\"150px\" height=\"100px\"></div>";
echo '<div class="product-content"><h3>'.$obj['name'].'</h3>';
echo '<div class="product-info">';
echo 'Price '.$obj['amount'].' | ';
echo 'Qty <input type="text" name="product_qty" value="1" size="3" />';
if($row_cnt > 0) {
echo "<br>Size <select name=\"product_size\">";
echo $all;
echo "</select>";
}
echo '<button class="add_to_cart">Add To Cart</button>';
echo '</div></div>';
echo '<input type="hidden" name="product_id" value="'.$id.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
}
}
?>
</div>
<div class="shopping-cart">
<h2>Your Shopping Cart</h2>
<?php
if(isset($_SESSION["products"]))
{
$total = 0;
echo '<ol>';
foreach ($_SESSION["products"] as $cart_itm)
{
echo '<li class="cart-itm">';
echo '<span class="remove-itm"><a href="product_cart_update.php?removep='.$cart_itm['id'].'&return_url='.$current_url.'">×</a></span>';
echo '<h3>'.$cart_itm["name"].'</h3>';
echo '<div class="p-qty">Size : '.$cart_itm["size"].'</div>';
echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
echo '<div class="p-price">Price : '.$currency.$cart_itm["price"].'</div>';
echo '</li>';
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
}
echo '</ol>';
echo '<span class="check-out-txt"><strong>Total : '.$currency.$total.'</strong></span>';
echo '<span class="empty-cart"><a href="product_cart_update.php?emptycart=1&return_url='.$current_url.'">Empty Cart</a></span>';
}else{
echo 'Your Cart is empty';
}
?>
</div>
</div>
</body>
</html