Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (isset($_POST ['pid'])) {
- $pid = $_POST ['pid'];
- $wasFound = false;
- $i = 0;
- if (!isset ($_SESSION["cart_array"]) || count($_SESSION ["cart_array"])<1){
- $_SESSION["cart_array"] = array (0=> array("item_id"=> $pid, "quantity" => 1));
- } else {
- foreach ($_SESSION["cart_array"] as $each_item) {
- $i++;
- while(list($key,$value)=each($each_item)){
- if ($key== "item_id" && $value == $pid) {
- array_splice ($_SESSION["cart_array"], $i-1,1, array(array("item_id"=>$pid,"quantity"=> $each_item['quantity']+1)));
- $wasFound = true;
- }
- }
- }
- if($wasFound==false) {
- array_push ($_SESSION["cart_array"], array("item_id"=> $pid, "quantity" => 1));
- }
- }
- header("Location: cart.php");
- exit();
- }
- ?>
- if (isset($_POST['pid'])) {
- // add (+1) item to cart
- $pid = (int)$_POST['pid']; // cast as integer
- // valid pids are > 0
- if($pid > 0){
- if(!isset($_SESSION['cart_array'][$pid])){
- // item is not in the cart, add it with quantity = 1
- $_SESSION['cart_array'][$pid] = array("item_id" => $pid, "quantity" => 1); // I left the array in the cart the same, but it could also be simplified so that it is only the quantity, since the item_id is now the cart array index
- } else {
- // item is in the cart, increment quantity
- $_SESSION['cart_array'][$pid]['quantity']++;
- }
- }
- header("location: cart.php");
- exit();
- }
- if ($result = print_r(array_keys('cart_array',$pid))) {
- $comma_seperated = implode("," $result);
- // then use use $comma_seperated in query where needed later in annother code block?
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement