Не работают функции корзины на сайте.
Не могу добавить товар в корзину, когда я добавил корзину в нем уже находились два моих товара.
Не могу понять в чем ошибка.
Код:
?php
session_start();
?>
<?php
$status=" ";
$idcat = $_GET['idcat'];
$dbUser = 'user';
$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'],
'img' => $x['img'],
'name' => $x['name'],
'price' => $x['price'],
'quantity'=>1,
);
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($id,$array_keys)) {
$status = "<div class='box' style='color:red;'>
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>";
}
}
}
foreach ($tovars as $x){
echo '
<div class="tovar">
<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>
</div>
</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" /> Cart<span>
<?php echo $cart_count; ?></span></a>
</div>
<?php
}
?>
</main>
</body>
</html>
--------------------------------------------------------------------------------------
Код корзины:
<?php
session_start();
$status="";
if (isset($_POST['action']) && $_POST['action']=="remove"){
if(!empty($_SESSION["shopping_cart"])) {
foreach($_SESSION["shopping_cart"] as $ky => $value) {
if($_POST["idcat"] == $key){
unset($_SESSION["shopping_cart"][$key]);
$status = "<div class='box' style='color:red;'>
Product is removed from your cart!</div>";
}
if(empty($_SESSION["shopping_cart"]))
unset($_SESSION["shopping_cart"]);
}
}
}
if (isset($_POST['action']) && $_POST['action']=="change"){
foreach($_SESSION["shopping_cart"] as &$value){
if($value['id_category'] === $_POST["idcat"]){
$value['quantity'] = $_POST["quantity"];
break; // Stop the loop after we've found the product
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="../Styles/style.css">
</head>
<body>
<?php include 'include/header.php'; ?>
<div class="cart">
<?php
if(isset($_SESSION["shopping_cart"])){
$total_price = 0;
?>
<table class="table">
<tbody>
<tr>
<td></td>
<td>ITEM NAME</td>
<td>QUANTITY</td>
<td>UNIT PRICE</td>
<td>ITEMS TOTAL</td>
</tr>
<?php
foreach ($_SESSION["shopping_cart"] as $product){
?>
<tr>
<td>
<img src='<?php echo $product["img"]; ?>' width="50" height="40" />
</td>
<td><?php echo $product["name"]; ?><br />
<form method='post' action=''>
<input type='hidden' name='code' value="<?php echo $product["code"]; ?>" />
<input type='hidden' name='action' value="remove" />
<button type='submit' class='remove'>Remove Item</button>
</form>
</td>
<td>
<form method='post' action=''>
<input type='hidden' name='id_category' value="<?php echo $product["idcat"]; ?>" />
<input type='hidden' name='action' value="change" />
<select name='quantity' class='quantity' onChange="this.form.submit()">
<option <?php if($product["quantity"]==1) echo "selected";?>
value="1">1</option>
<option <?php if($product["quantity"]==2) echo "selected";?>
value="2">2</option>
<option <?php if($product["quantity"]==3) echo "selected";?>
value="3">3</option>
<option <?php if($product["quantity"]==4) echo "selected";?>
value="4">4</option>
<option <?php if($product["quantity"]==5) echo "selected";?>
value="5">5</option>
</select>
</form>
</td>
<td><?php echo "$".$product["price"]; ?></td>
<td><?php echo "$".$product["price"]*$ptoduct["quantity"]; ?></td>
</tr>
<?php
$price += ($product["price"]*$product["quantity"]);
}
?>
<tr>
<td colspan="5" align="right">
<strong>TOTAL: <?php echo "$".$total_price; ?></strong>
</td>
</tr>
</tbody>
</table>
<?php
}else{
echo "<h3>Your cart is empty!</h3>";
}
?>
</div>
<div style="clear:both;"></div>
<div class="message_box" style="margin:10px 0px;">
<?php echo $status; ?>
</div>
</body>
</html>