You need to create an item counter in the shopping cart. When I add an item to the cart, it is duplicated. In $_SESSION ['products'] appears [0], which also contains a number. I found a way to get rid of it: unset($_SESSION['products'][0]); but I'm not sure that's right. What should I do?
this file is order.php :
<?php
class Order{
static function addInOrder($id){
$id= intval($id);
$productsInOrder=array();
if(isset($_SESSION['products'])){
$productsInOrder=$_SESSION['products'];
}
if(array_key_exists($id, $productsInOrder)){
$productsInOrder[$id]++;
}
else{
$productsInOrder[$id]=1;
}
$_SESSION['products']=$productsInOrder;
}
static function orderCount(){
if(isset($_SESSION['products'])){
unset($_SESSION['products'][0]);
$count=0;
foreach ($_SESSION['products']as $id=>$value){
$count+=$value;
}
return $count;
}
else{
return 0;
}
}
}
?>
orderController.php :
<?php
class OrderController{
public function actionSend($id){
Order::addInOrder($id);
$referer=$_SERVER['HTTP_REFERER'];
header("location: $referer");
}
}
?>