//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']); require_once 'class/config.db.php'; require_once 'class/db.php'; $db=new db(); $results = $db->query("SELECT * FROM cart ORDER BY id ASC"); if ($results) {
//fetch results set as object and output HTML while($obj = $db->fetch_object($results)) { echo '
//limit quantity for single product if($product_qty > 10){ die('
This demo does not allowed more than 10 quantity! Back To Products.
'); }
//MySqli query - get details of item from db using product code $results = $mysqli->query("SELECT product_name,price FROM cart WHERE product_code='$product_code' LIMIT 1"); $obj = $results->fetch_object();
if ($results) { //we have the product info
//prepare array for the session variable $new_product = array(array('name'=>$obj->product_name, 'code'=>$product_code, 'qty'=>$product_qty, 'price'=>$obj->price));
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["code"] == $product_code){ //the item exist in array
$product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$product_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"], 'code'=>$cart_itm["code"], '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); }
//remove item from shopping cart if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["products"])) { $product_code = $_GET["removep"]; //get the product code to remove $return_url = base64_decode($_GET["return_url"]); //get return url
foreach ($_SESSION["products"] as $cart_itm) //loop through session array var { if($cart_itm["code"]!=$product_code){ //item does,t exist in the list $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]); }
//create a new product list for cart $_SESSION["products"] = $product; }
//redirect back to original page header('Location:'.$return_url); } ?>
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn