Добрый день
Жалуется на $_SESSION['total_items'] = total_item($_SESSION['cart']); в
add_cart
Fatal error: Uncaught Error: Call to undefined function total_item() in C:\xampp\htdocs\shop\index.php:34 Stack trace: #0 {main} thrown in C:\xampp\htdocs\shop\index.php on line 34
при чем в случае
case ('update_cart'): все работает нормально.
Это главный index.php
<?php
include('db_functions.php');
include('cart_functions.php');
if(!isset($_SESSION))
{
session_start();
}
if (!isset($_SESSION['cart'])){
$_SESSION['cart'] = array();
$_SESSION['total_items'] = 0;
$_SESSION['total_price'] = '0.00';
}
$view = empty($_GET['view']) ? 'index' : $_GET['view'];
global $products;
switch ($view){
case('index'):
$products = get_products();
break;
case ('cat'):
$id = $_GET['id'];
$products = get_cat_products($id);
break;
case ('product'):
$id = $_GET['id'];
$products = get_product($id);
break;
case ('cart'):
break;
case ('add_to_cart'):
$id = $_GET['id'];
$add_item = add_to_cart($id);
$_SESSION['total_items'] = total_item($_SESSION['cart']);
header('Location: index.php?view=product&id='.$id);
break;
case ('update_cart'):
update_cart();
$_SESSION['total_items'] = total_item($_SESSION['cart']);
header('Location: index.php?view=cart');
break;
}
include($_SERVER['DOCUMENT_ROOT'] . '/shop/views/layouls/index.php');
?>
Файл cart_functions.php
<?php
function add_to_cart($id){
if (isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id]++;
return true;
}
else {
$_SESSION['cart'][$id]=1;
return true;
}
return false;
}
function update_cart() {
foreach ($_SESSION['cart'] as $id => $qty){
if($_POST[$id]=='0'){
unset($_SESSION['cart'][$id]);
session_unset() ;
}
else {
$_SESSION['cart'][$id]=$_POST[$id];
}
}
function total_item($cart){
$num_items = 0;
if(is_array($cart)){
foreach ($cart as $id => $qty){
$num_items = $num_items + $qty;}
}
return $num_items;
}
function total_item1($cart){
$num_items = 0;
if(is_array($cart)){
foreach ($cart as $id => $qty){
$num_items = $num_items + $qty;}
}
return $num_items;
}
}