Each time i add to cart, i get Your cart is empty.
Code: Select all
<?php
session_start();
if (isset($_GET['add'])){
$_SESSION['cart_'.(int)$_GET['add']]+='1';
}
if (isset($_GET['remove'])){
$_SESSION['cart_'.(int)$_GET['remove']]--;
header ('Location: m_addtocart.php');
}
if (isset($_GET['delete'])){
$_SESSION['cart_'.(int)$_GET['delete']]='0';
header ('Location: m_addtocart.php');
}
?>
<?php
function cart2(){
foreach($_SESSION as $name => $value){
if($value > 0){
if(substr($name,0,5) == 'cart_'){
$id = substr($name,5,(strlen($name)-5));
$q = 'SELECT id, prodname, price FROM products WHERE id = $id';
$r = @mysqli_query($dbc, $q);
if ($r){
while($get_row = mysql_fetch_array($r, MYSQLI_ASSOC)){
$sub = $get_row['price']*$value;
echo $get_row['prodname']. 'x'. $value . '@' . number_format($get_row['price'], 2). '='.number_format($sub, 2).' <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'">[Delete]</a></br>';
}
}
$total = $total + $sub;
}
}
if ($total == 0){
echo "Your Cart is empty!";
} else {
echo '<p>Total: N'.number_format($total, 2).'</p>';
echo "Checkout Button";
}
} }
?>