Мне выдает ошибку и какой-то код. Если добавить в код
$cart = new Cart;
ошибка пропадает, но тогда каждый раз будет создаваться новая корзина, и н получится добавит больше одного товара
В коде, который выводит скрипт много много символов, поэтому я добавил его скриншотом
index.php
<?php
include $_SERVER["DOCUMENT_ROOT"] . "/config.php";
include $_SERVER["DOCUMENT_ROOT"] . "/classes/cart.php";
$query = 'SELECT * FROM products';
$stmt = $link->prepare($query);
$stmt->execute();
$result = $stmt->get_result();
$id = filter_input(INPUT_POST, 'product_id', FILTER_VALIDATE_INT);
$price = filter_input(INPUT_POST, "product_price", FILTER_VALIDATE_INT);
$cart =& $_SESSION["cart"];
if(!is_object($cart)){
$cart = new Cart;
}
if(isset($_POST["Submit"])){
$cart->add(new CartItem($id, $price));
}
var_dump($cart);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" /> -->
<link rel="stylesheet" href="css/style.css">
<title>Shop</title>
</head>
<table border="1">
<tr>
<th>ID товара</th><th>Цена</th><th>Кол-во</th><th>Сумма</th>
<?php foreach($cart->get() as $product){?>
<tr>
<td><?php echo $product->id;?></td>
<td><?php echo $product->price;?></td>
<td><?php echo $product->count;?></td>
<td><?php echo $product->total_price?></td>
</tr>
<?php };?>
</tr>
<tr>
<td colspan="3">Всего товаров:</td>
<td><?php echo $cart->count();?></td>
</tr>
<tr>
<td colspan="3">На сумму:</td>
<td><?php echo $cart->total();?></td>
</tr>
</table>
<body>
<div class="cart">
</div>
<header>
<div class="logo">
<h1>Uniqlo</h1>
</div>
<ul class="first-menu">
<li><a href="#">Home</a></li>
<li><a href="#">Sale</a></li>
<li><a href="#">Catalog</a>
<div class="sub-menu">
<ul>
<li class="sub-menu-index">
<h3>Mac</h3>
</li>
<li class="sub-menu-item"><a href="#">Macbook</a></li>
<li class="sub-menu-item"><a href="#">Imac</a></li>
<li class="sub-menu-item"><a href="#">Macpro</a></li>
<li class="sub-menu-item"><a href="#">Macmini</a></li>
</ul>
<ul>
<li class="sub-menu-index">
<h3>Iphone</h3>
</li>
<li class="sub-menu-item"><a href="#">Iphone13</a></li>
<li class="sub-menu-item"><a href="#">Iphone12</a></li>
<li class="sub-menu-item"><a href="#">IphoneSE</a></li>
</ul>
<ul>
<li class="sub-menu-index">
<h3>Ipad</h3>
</li>
<li class="sub-menu-item"><a href="#">IpadPro
</a></li>
<li class="sub-menu-item"><a href="#">IpadAir</a></li>
<li class="sub-menu-item"><a href="#">IpadMini</a></li>
</ul>
<ul>
<li class="sub-menu-index">
<h3>AirPods</h3>
</li>
<li class="sub-menu-item"><a href="#">AirPods Max</a></li>
<li class="sub-menu-item"><a href="#">AirPods Pro</a></li>
<li class="sub-menu-item"><a href="#">AirPods 3</a></li>
<li class="sub-menu-item"><a href="#">AirPods 2</a></li>
</ul>
</div>
</li>
<li><a href="#">Contacts</a></li>
</ul>
<ul class="second-menu">
<li class="search-button"><img src="img/search.svg" width="20px" alt="search">
</li>
<form class="search-window" action="" merhod="GET">
<input type="text"></form>
<li><img src="img/heart.svg" width="20px" alt="wishlist"></li>
<li><img src="img/cart2.svg" height="23px" alt="cart"></li>
</ul>
</header>
<!-- Слайдер -->
<div class="slider">
<div class="item">
<img src="img/3.jpg" alt="Первый слайд">
<div class="slideText">Заголовок слайда 1</div>
</div>
<div class="item">
<img src="img/2.jpg" alt="Второй слайд">
<div class="slideText">Заголовок слайда 2</div>
</div>
<div class="item">
<img src="img/1.jpg" alt="Третий слайд">
<div class="slideText">Заголовок слайда 3</div>
</div>
<a class="prev" onclick="minusSlide()">❮</a>
<a class="next" onclick="plusSlide()">❯</a>
<div class="slider-dots">
<span class="slider-dots_item" onclick="currentSlide(1)"></span>
<span class="slider-dots_item" onclick="currentSlide(2)"></span>
<span class="slider-dots_item" onclick="currentSlide(3)"></span>
</div>
</div>
<!-- Конец слайдера -->
<!-- Раздел Sale -->
<div class="container">
<h3 class="product-list-title">Sale</h3>
<div class="product-list">
<?php
$query = 'SELECT * FROM products';
$stmt = $link->prepare($query);
$stmt->execute();
$result = $stmt->get_result();
foreach($result as $item){
?>
<div class="product">
<div class="wrap">
<img class="product-image" src="<?php echo $item['image'];?>" alt="img">
<div class="overlay">
<form id="form" name="form" method="POST" action=""><br>
<label><br>
<input type="hidden" name="product_price" value="<?php echo $item['price']; ?>" /><br>
<input type="hidden" name="product_id" value="<?php echo $item['id']; ?> "/><br>
<input type="submit" name="Submit" value="В корзину" /><br>
</label><br>
</form>
</div>
</div>
<h4><a href="#"><?php echo $item['title'];?></a></h4>
<span class="price"><?php echo $item['price'];?>$</span>
<span></span>
</div>
<?php
}
?>
</div>
</div>
<div class="wrap-footer">
<footer>
<span>tel</span>
<span>Privacy Policy | Your California Privacy Rights</span>
<span>© UNIQLO CO. LTD. All rights reserved.</span>
</footer>
</div>
<script src="js/script.js"></script>
</body>
</html>
cart.php
<?php
class CartItem{
public $id;
public $price = 0;
public $count = 1;
public $total_price = 0;
public function __construct($id=NULL, $price=0, $count=1){
if(isset($id)){
$this->id = $id;
$this->price = $price;
$this->count = !$count?1:$count;
$this->totalPrice();
}
}
function totalPrice(){
$this->total_price = $this->price * $this->count;
return $this->total_price;
}
}
class Cart{
protected $items = array();
function add(CartItem $cartItem){
if(isset($this->items[$cartItem->id])){
$cartItem->count += $this->items[$cartItem->id]->count;
}
$this->edit($cartItem);
}
function edit(CartItem $cartItem) {
$cartItem->totalPrice();
$this->items[$cartItem->id] = $cartItem;
}
function delete($id){
unset($this->item[$id]);
}
function emptyCart(){
$this->items=array();
}
function count(){
$count = 0;
foreach($this->get() as $cartItem) $count += $cartItem->count;
return $count;
}
function total(){
$total = 0;
foreach($this->get() as $cartItem) $total += $cartItem->total_price;
return $total;
}
function get($id=null){
if(isset($id)) return $this->items[$id];
else return $this->items;
}
}