Skip to content

Commit 543a53f

Browse files
committed
Fixed errors when the $_SESSION['cart'] data is null.
1 parent 71a33af commit 543a53f

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

inc/nav.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
<?php
2+
// Check to see if the cart is in the session data else default to null.
3+
// We do this because the $cart and $count variables are used extensively
4+
// below and will output warnings if we don't.
5+
if (isset($_SESSION['cart'])) {
6+
$cart = $_SESSION['cart'];
7+
$count = count($cart);
8+
} else {
9+
$cart = '';
10+
$count = 0;
11+
}
12+
?>
113
<div class="menu-wrap">
214
<div id="mobnav-btn">Menu <i class="fa fa-bars"></i></div>
315
<ul class="sf-menu">
@@ -32,10 +44,6 @@
3244
</li>
3345
</ul>
3446
<div class="header-xtra">
35-
<?php
36-
$cart = $_SESSION['cart'];
37-
$count = count($cart);
38-
?>
3947
<div class="s-cart">
4048
<div class="sc-ico"><i class="fa fa-shopping-cart"></i><?php
4149
if ($count !== 0) {
@@ -44,7 +52,7 @@
4452
<div class="cart-info">
4553
<small><?php
4654
if ($count !== 0 && $count !== 1) {
47-
echo 'You have <em class="highlight"> ' . count($cart) . ' items</em> in your shopping cart.';
55+
echo 'You have <em class="highlight"> ' . $count . ' items</em> in your shopping cart.';
4856
} else if ($count === 1) {
4957
echo 'You have <em class="highlight"> 1 item</em> in your shopping cart.';
5058
} else {
@@ -54,7 +62,6 @@
5462
<br>
5563
<br>
5664
<?php
57-
$count = count($_SESSION['cart']);
5865
if ($count !== 0) {
5966
foreach ($cart as $key => $value) {
6067
$navcartsql = "SELECT * FROM `products` WHERE `id`=$key";
@@ -77,7 +84,7 @@
7784
} ?>
7885
<div class="ci-total">Subtotal: <?php echo getenv('STORE_CURRENCY') . $cartTotal; ?></div>
7986
<div class="cart-btn">
80-
<a href="<?php echo getenv('STORE_URL'); ?>/cart.php">View Bag</a>
87+
<a href="<?php echo getenv('STORE_URL'); ?>/cart.php">View Cart</a>
8188
<a href="<?php echo getenv('STORE_URL'); ?>/checkout.php">Checkout</a>
8289
</div>
8390
<?php

0 commit comments

Comments
 (0)