При открытии страницы товаров из одной категории, в корзине уже лежат все товары
Код вывода и добавления товаров
<?php
session_start();
?>
<?php
$status="";
$idcat = $_GET['idcat'];
$dbUser = 'root';
$dbName = 'lshop';
$dbPass = ' ';
$mysqli = new mysqli('localhost', $dbUser, $dbPass, $dbName);
$query = "set names utf8";
$mysqli->query($query);
$query="SELECT * FROM product WHERE id_category = '$idcat'";
$results = $mysqli->query($query);
while($x=$results->fetch_assoc()){
$tovars[] = array(
'id' => $x['id'],
'quantity'=>1,
'img' => $x['img'],
'name' => $x['name'],
'price' => $x['price'],
);
}
foreach($tovars as $x){
echo '
<div class="tovar">
<form method="post" action="">
<a href="tt.php?id='.$x['id'].'"><img src='.$x['img'].'></a>
<div class="tovar-list">
<h3>'.$x["name"].'</h3>
<span class="price">'.$x["price"].'</span>
<button type="submit" class="buy">Buy now</button>
<a class="button" href="cart.php?id=$x">В корзину</a>
</div>
</form>
</div>';
}
?>
<?php
if(!empty($_SESSION["shopping_cart"])) {
$cart_count = count(array_keys($_SESSION["shopping_cart"]));
?>
<div class="cart_div">
<a href="cart.php"><img src="../images/cart-icon.png" /> Корзина<span>
<?php echo $cart_count; ?></span></a>
</div>
<?php
}
?>
<?php
if(empty($_SESSION["shopping_cart"])) {
$_SESSION["shopping_cart"] = $tovars;
$status = "<div class='box'>Product is added to your cart!</div>";
}else{
$array_keys = array_keys($_SESSION["shopping_cart"]);
if(in_array($x["id"],$array_keys)) {
$status = "<div class='box' style='color:white;'>
Product is already added to your cart!</div>";
} else {
$_SESSION["shopping_cart"] = array_merge(
$_SESSION["shopping_cart"],
$tovars
);
$status = "<div class='box'>Product is added to your cart!</div>";
}
}
?>