php - how to increment session id variable after adding item into cart -
i working a cart item user can add multiple item cart , delete item cart using session after adding 1 item increment id 1 one on each add new item cart code working fine problem in deleting when trying delete item item tray delete 1 item here code
<?php session_start(); if(!empty($_get["action"])) { switch($_get["action"]) { case "add": if(!isset($_session['cart'])) { $item_id_session = 1; $_session['cart'] = array(); } else { $count = count($_session['cart']); $item_id_session = $count++; } $item = array(); $item['item_id_session']= $item_id_session ; $item['title']=$_request['title']; $item['length_m'] =$_request['length_m']; $item['length_md']=$_request['length_md']; $item['length_cm'] =$_request['length_cm']; $item['length_cmd']=$_request['length_cmd']; $item['width_m']=$_request['width_m']; $item['width_md']=$_request['width_md']; $item['width_cm']=$_request['width_cm']; $item['width_cmd']=$_request['width_cmd']; $item['height_m']=$_request['height_m']; $item['height_md']=$_request['height_md']; $item['height_cm']=$_request['height_cm']; $item['height_cmd']=$_request['height_cmd']; $item['weight']=$_request['weight']; $item['weightd']=$_request['weightd']; $item['quantity']=$_request['quantity']; $item['image']=$_files["image"]["name"]; $target_dir = "profile_images/"; $item['image']= "profile_images/" . $_files["image"]["name"]; $imagefiletype = pathinfo($item['image'],pathinfo_extension); $check = getimagesize($_files["image"]["tmp_name"]); if($check !== false) { echo "file image - " . $check["mime"] . "."; //$uploadok = 1; move_uploaded_file($_files["image"]["tmp_name"], $item['image']); echo "the file ". basename( $_files["image"]["name"]). " has been uploaded."; } $_session['cart'][] = $item; break; case "remove": if(!empty($_session["cart"])) { foreach($_session["cart"] $k => $v) { if($_get['item_id_session'] == $k) unset($_session["cart"][$k]); unset($_session['cart'][$item_id_session]); if(empty($_session["cart"])) unset($_session["cart"]); } } break; } } ?>
here delete button code
<a href="step3.php?action=remove&id=<?php echo $item["item_id_session"];?>"class="product-title"> <span class="glyphicon glyphicon-trash pull-right"></span> </a>
try this.while adding items cart need use $item_id_session
key.
case "add": if (isset($_session['cart']) && count($_session['cart']) > 0) { $count = count($_session['cart']); $item_id_session = $count+ 1; } else { $item_id_session = 1; $_session['cart'] = array(); } $item = array(); $item['item_id_session']= $item_id_session ; $item['title']=$_request['title']; $item['length_m'] =$_request['length_m']; $item['length_md']=$_request['length_md']; $item['length_cm'] =$_request['length_cm']; $item['length_cmd']=$_request['length_cmd']; $item['width_m']=$_request['width_m']; $item['width_md']=$_request['width_md']; $item['width_cm']=$_request['width_cm']; $item['width_cmd']=$_request['width_cmd']; $item['height_m']=$_request['height_m']; $item['height_md']=$_request['height_md']; $item['height_cm']=$_request['height_cm']; $item['height_cmd']=$_request['height_cmd']; $item['weight']=$_request['weight']; $item['weightd']=$_request['weightd']; $item['quantity']=$_request['quantity']; $item['image']=$_files["image"]["name"]; $target_dir = "profile_images/"; $item['image']= "profile_images/" . $_files["image"]["name"]; $imagefiletype = pathinfo($item['image'],pathinfo_extension); $check = getimagesize($_files["image"]["tmp_name"]); if($check !== false) { echo "file image - " . $check["mime"] . "."; //$uploadok = 1; move_uploaded_file($_files["image"]["tmp_name"], $item['image']); echo "the file ". basename( $_files["image"]["name"]). " has been uploaded."; } $_session['cart'][$item_id_session] = $item; break; case "remove": if(!empty($_session["cart"])) { foreach($_session["cart"] $k => $v) { if($_get['item_id_session'] == $k) unset($_session["cart"][$k]); } } break;
Comments
Post a Comment