php shopping cart session id remove -
if have session cart items more 1 not work.. have 1 work. happen this
i put of codes in session. check happen case...
here php code link ?action=remove&id=<value>
function checkcartforitem($additem, $cartitems) { if (is_array($cartitems)){ foreach($cartitems $key => $item) { if($item['id'] === $additem) return $key; } } return false; } if (!empty($_get['qty'])) { $qty = $_get['qty']; } //store in array $item = array( //item name 'id' => $_post['id'] ); $additem = $_get['id']; //check if(!empty($_get["action"])) { switch($_get["action"]) { case "add": if(!empty($_get["qty"])) { $productbycode = $db_handle->runquery("select * product id='" . $_get["id"] . "'"); $itemarray = array($productbycode[0]["id"]=>array('name'=>$productbycode[0]["product_name"], 'id'=>$productbycode[0]["id"], 'quantity'=>$qty, 'price'=>$productbycode[0]["new_price"])); $itemexists = checkcartforitem($additem, $_session['cart_item']); if ($itemexists !== false){ $_session['cart_item'][$itemexists]['quantity'] = $qty ; } else { if(!empty($_session["cart_item"])) { if(in_array($productbycode[0]["id"],$_session["cart_item"])) { foreach($_session["cart_item"] $k => $v) { if($productbycode[0]["id"] == $k) $_session["cart_item"][$k]["quantity"] = $qty; } } else { $_session["cart_item"] = array_merge($_session["cart_item"],$itemarray); } } else { $_session["cart_item"] = $itemarray; } } } break; case "remove": if(!empty($_session["cart_item"])) { foreach($_session["cart_item"] $k => $v) { if($_get["id"] == $k) unset($_session["cart_item"][$k]); if(empty($_session["cart_item"])) unset($_session["cart_item"]); } } break; case "empty": unset($_session["cart_item"]); break; } }
array (print_r($_session["cart_item"]);
)
array ( [0] => array ( [name] => sadfsafsadf [id] => 11 [quantity] => 1 [price] => safsafsa ) [1] => array ( [name] => tp-link 4 port wireless dual band n600 [id] => 13 [quantity] => 1 [price] => 15980 ) )
you made mistake on line. if($_get["id"] == [$k])
case "remove": if(!empty($_session["cart_item"])) { foreach($_session["cart_item"] $k => $v) { if($_get["id"] == $_session["cart_item"][$k]['id']) unset($_session["cart_item"][$k]); if(empty($_session["cart_item"])) unset($_session["cart_item"]); } } break;
Comments
Post a Comment