diff --git a/public/action.php b/public/action.php new file mode 100644 index 0000000..9a77e71 --- /dev/null +++ b/public/action.php @@ -0,0 +1,60 @@ +has('items', ['AND' => ['itemid' => $item, 'price[>]' => 0]])) { + header('Location: ./?page=cart&msg=invaliditem'); + die("Invalid item"); + } + $cart = []; + + if (!empty($_SESSION['cart'])) { + $cart = $_SESSION['cart']; + } + + if (empty($cart[$item])) { + $cart[$item] = $qty; + } else { + $cart[$item] += $qty; + } + + $_SESSION['cart'] = $cart; + + header('Location: ./?page=cart&msg=itemadded'); + die(); + + break; + case "updatecart": + $item = $VARS['item']; + $qty = $VARS['qty']; + + $cart = []; + + if (!empty($_SESSION['cart'])) { + $cart = $_SESSION['cart']; + } + + $cart[$item] = $qty; + + if ($qty <= 0) { + unset($cart[$item]); + } + + $_SESSION['cart'] = $cart; + + header('Location: ./?page=cart&msg=itemupdated'); + + break; +} \ No newline at end of file diff --git a/public/index.php b/public/index.php index 1401259..c5740aa 100644 --- a/public/index.php +++ b/public/index.php @@ -42,6 +42,9 @@ if (isset($_GET['page'])) { $page = "404"; } break; + case "cart": + $page = "cart"; + break; case "home": default: $page = "home"; diff --git a/public/lib/item.php b/public/lib/item.php index d608ad0..0b9080e 100644 --- a/public/lib/item.php +++ b/public/lib/item.php @@ -100,11 +100,40 @@ END; - + +END; + return $html; + } + + static function cart(Item $item, int $qty) { + $id = $item->getId(); + $name = $item->getName(); + $catid = $item->getCategoryId(); + $catname = $item->getCategoryName(); + $price = $item->getPrice(); + $html = << +
+

$name

+ $$price +
+
+
+
+ +
+ +
+
+ + +
+
+ END; return $html; } diff --git a/public/parts/cart.php b/public/parts/cart.php index 13986fb..332af88 100644 --- a/public/parts/cart.php +++ b/public/parts/cart.php @@ -1,8 +1,37 @@ + +
+

Cart

+ +
+ 0) { + foreach ($cart as $i => $qty) { + echo RenderItem::cart(new Item($i), $qty); + } + } else { + ?> +

The cart is empty.

+ +
+
\ No newline at end of file