Skip to content

Commit c7079d5

Browse files
committed
Fixed PHP warnings with cart when it has zero items in it.
1 parent af83fe0 commit c7079d5

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

inc/nav.php

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
<div id="mobnav-btn">Menu <i class="fa fa-bars"></i></div>
33
<ul class="sf-menu">
44
<li>
5-
<a href="http://localhost/ecomphp/index.php">Home</a>
5+
<a href="<?php echo getenv('STORE_URL'); ?>/index.php">Home</a>
66
</li>
77
<li>
88
<a href="#">Shop</a>
99
<div class="mobnav-subarrow"><i class="fa fa-plus"></i></div>
1010
<ul>
1111
<?php
12-
$catsql = "SELECT * FROM category";
12+
$catsql = "SELECT * FROM `category`";
1313
$catres = mysqli_query($connection, $catsql);
1414
while ($catr = mysqli_fetch_assoc($catres)) {
1515
?>
@@ -22,9 +22,9 @@
2222
<a href="#">My Account</a>
2323
<div class="mobnav-subarrow"><i class="fa fa-plus"></i></div>
2424
<ul>
25-
<li><a href="http://localhost/ecomphp/my-account.php">My Orders</a></li>
26-
<li><a href="http://localhost/ecomphp/edit-address.php">Update Address</a></li>
27-
<li><a href="http://localhost/ecomphp/logout.php">Logout</a></li>
25+
<li><a href="<?php echo getenv('STORE_URL'); ?>/my-account.php">My Orders</a></li>
26+
<li><a href="<?php echo getenv('STORE_URL'); ?>/edit-address.php">Update Address</a></li>
27+
<li><a href="<?php echo getenv('STORE_URL'); ?>/logout.php">Logout</a></li>
2828
</ul>
2929
</li>
3030
<li>
@@ -35,40 +35,47 @@
3535
<?php $cart = $_SESSION['cart']; ?>
3636
<div class="s-cart">
3737
<div class="sc-ico"><i class="fa fa-shopping-cart"></i><em><?php
38-
echo count($cart); ?></em></div>
39-
38+
if (!is_null($cart)) {
39+
echo count($cart);
40+
} else {
41+
echo "0";
42+
} ?></em></div>
4043
<div class="cart-info">
41-
<small>You have <em class="highlight"><?php
42-
echo count($cart); ?> item(s)</em> in your shopping bag</small>
44+
<small><?php
45+
if (!is_null($cart)) {
46+
echo 'You have <em class="highlight"> ' . count($cart) . ' items</em> in your shopping cart.';
47+
} else {
48+
echo 'Your shopping cart is empty. It\'s pretty lonely over here, why not add something to it?';
49+
} ?>
50+
</small>
4351
<br>
4452
<br>
4553
<?php
46-
//print_r($cart);
47-
$total = 0;
48-
foreach ($cart as $key => $value) {
49-
//echo $key . " : " . $value['quantity'] ."<br>";
50-
$navcartsql = "SELECT * FROM products WHERE id=$key";
51-
$navcartres = mysqli_query($connection, $navcartsql);
52-
$navcartr = mysqli_fetch_assoc($navcartres); ?>
53-
<div class="ci-item">
54-
<img src="admin/<?php echo $navcartr['thumb']; ?>" width="70" alt=""/>
55-
<div class="ci-item-info">
56-
<h5><a href="single.php?id=<?php echo $navcartr['id']; ?>"><?php echo substr($navcartr['name'], 0, 20); ?></a></h5>
57-
<p><?php echo $value['quantity']; ?> x R <?php echo $navcartr['price']; ?>.00</p>
58-
<div class="ci-edit">
59-
<!-- <a href="#" class="edit fa fa-edit"></a> -->
60-
<a href="delcart.php?id=<?php echo $key; ?>" class="edit fa fa-trash"></a>
54+
if (!is_null($total)) {
55+
foreach ($cart as $key => $value) {
56+
$navcartsql = "SELECT * FROM `products` WHERE `id`=$key";
57+
$navcartres = mysqli_query($connection, $navcartsql);
58+
$navcartr = mysqli_fetch_assoc($navcartres); ?>
59+
<div class="ci-item">
60+
<img src="admin/<?php echo $navcartr['thumb']; ?>" width="70" alt=""/>
61+
<div class="ci-item-info">
62+
<h5><a href="single.php?id=<?php echo $navcartr['id']; ?>"><?php echo substr($navcartr['name'], 0, 20); ?></a></h5>
63+
<p><?php echo $value['quantity']; ?> x R <?php echo $navcartr['price']; ?></p>
64+
<div class="ci-edit">
65+
<a href="delcart.php?id=<?php echo $key; ?>" class="edit fa fa-trash"></a>
66+
</div>
6167
</div>
6268
</div>
63-
</div>
64-
<?php
65-
$total = $total + ($navcartr['price']*$value['quantity']);
66-
} ?>
67-
<div class="ci-total">Subtotal: R <?php echo $total; ?>.00</div>
69+
<?php
70+
$total = $total + ($navcartr['price']*$value['quantity']);
71+
} ?>
72+
<div class="ci-total">Subtotal: R <?php echo $total; ?></div>
6873
<div class="cart-btn">
6974
<a href="cart.php">View Bag</a>
7075
<a href="checkout.php">Checkout</a>
7176
</div>
77+
<?php
78+
} ?>
7279
</div>
7380
</div>
7481
<div class="s-search">

0 commit comments

Comments
 (0)