There are goods in the basket. How to implement the removal of goods from the basket? Through JQuery and ajax or I did it. And how is the increase in the quantity of goods in the basket?

 if(is_numeric($_GET['id'])) {unset($_SESSION['cart'][$_GET['id']]);} 

Closed due to the fact that the issue is too general for the participants andreymal , br3t , user207618, sanmai , ilyaplot 3 Aug '17 at 9:14 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Depends on the specific basket of a particular site - andreymal

2 answers 2

Usually done via jQuery and ajax. Basket data can be stored in $ _SESSION - but this data will disappear if the user leaves the site. For large stores, where the order is a few items, this is unacceptable, and the basket must be stored in the database.

How to implement the removal of goods from the basket?

For $ _SESSION - do as you did, for the database - delete the row in the basket table.

And how is the increase in the quantity of goods in the basket?

For $ _SESSION $_SESSION['cart'][ид товара] = число единиц товара в заказе; - adding is just an increase of this value. For the database you have a field with the number of units of the order - and increase it.

    Depending on how you have organized the data structures in which the basket is stored. For example, you store a basket in cookies in json type:

     [{"id":идентификатор_товара,"count":количество},{...}] 

    then a php handler that accepts a get-parameter product identifier will be like (add and delete in one, I think you will figure it out):

     <?php // add to cart $item_id = $_GET['id']; if(isset($_COOKIE['cart'])) $cart = json_decode($_COOKIE['cart']); else $cart = array(); $is_in_cart = false; foreach($cart as $key=>$value) { if($value->id == $item_id) { $cart[$key]->count++; $is_in_cart = true; }//if($value->id == $item_id) }//foreach($cart as $key=>$value) if(!$is_in_cart) { $cart[] = array('id'=>$item_id,'count'=>1); }//if(!$is_in_cart) setcookie("cart", json_encode($cart), time() + (86400 * 30), "/"); print_r(json_decode($_COOKIE['cart'])); exit; //delete from cart $item_id = $_GET['id']; foreach($cart as $key=>$value) { if($value->id == $item_id) { $count = $cart[$key]->count; if($count == 1) unset($cart[$key]); else $cart[$key]->count--; }//if($value->id == $item_id) }//foreach($cart as $key=>$value) print_r(json_decode($_COOKIE['cart'])); exit;