I'm new to php so I've struggled through each step of this assignment. I've hit a wall with this final step. I've built a simple shopping cart using only PHP (per the instructions). The index.php page contains all the items as well as a checkout button that redirects to the checkout.php page. If the user clicks this "checkout" button before items have been added to their cart, I need to display "Your cart is empty" message on the checkout page. I'm assuming I need to do the check on the checkout.php file. I've attempted it more than a dozen times without luck. Help is appreciated.
I'm including my attempted code on the checkout.php file
<?php
require_once('session_util.php');
if(isset($_SESSION[SHOPPING_CART]) && empty($_SESSION[SHOPPING_CART])) {
echo 'Your shopping cart is empty.';
}
?>
<!doctype html>
<html lang="en">
<head>
<title>Form 1</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<main>
<h2>Checkout</h2>
<table id="cart">
<?php
// This is where session starts
$itemArray = $_SESSION[SHOPPING_CART]->getItemArray();
foreach ($itemArray as $item) {
$name = $item->getName();
$price = number_format($item->getPrice(), 2);
echo "<tr>
<th>$name</th>
<th>$$price</th>
</tr>";
}
// you will get these from the shopping cart, not a specifc item
$subTotal = number_format($_SESSION[SHOPPING_CART]->subTotal(), 2);
$tax = number_format($_SESSION[SHOPPING_CART]->addTax(), 2);
$total = number_format($_SESSION[SHOPPING_CART]->getTotal(), 2);
// Display calculated subtotal, tax, and total.
?>
<?php
echo "
<table id='total'>
<tr><td>SubTotal   $$subTotal</td></tr>
<tr><td>Tax           $$tax</td></tr>
<tr><td>Total         $$total</tr></td>
</table>
";
?>
</table>
<form id="keep_shopping" method="post" action="index.php">
<input id="submit" type="submit" class="keep_shopping" name="keep_shopping" value="Keep Shopping">
</form>
<form id="empty_cart" method="post" action="clear_cart.php">
<input id="submit" type="submit" class="empty_cart" name="empty_cart" value="Empty Cart">
</form>
</main>
</body>
</html>
Edits and Infromation as asked in comment: enter code here
session_start() is located in a separate file - session_util.php - and this file is required at the top of all other assignment files.
Also, when I do a var_dump on $_SESSION[SHOPPING_CART]
; I get the following:
object(ShoppingCart)#11 (1) { ["itemArray":"ShoppingCart":private]=> array(0) { } }