diff options
-rw-r--r-- | lib/db.php (renamed from public/db.php) | 0 | ||||
-rw-r--r-- | lib/login.php | 27 | ||||
-rw-r--r-- | public/cart.php | 56 | ||||
-rw-r--r-- | public/index.php | 2 | ||||
-rw-r--r-- | public/product.php | 3 | ||||
-rw-r--r-- | public/products.php | 2 |
6 files changed, 75 insertions, 15 deletions
diff --git a/public/db.php b/lib/db.php index 6d158b7..6d158b7 100644 --- a/public/db.php +++ b/lib/db.php diff --git a/lib/login.php b/lib/login.php index 3467994..39dc612 100644 --- a/lib/login.php +++ b/lib/login.php @@ -1,13 +1,34 @@ <?php +$username = $_COOKIE['username']; +$password = $_COOKIE['password']; + +function login($username, $password) { + if (!$username) return false; + if (!$password) return false; + return true; +} + function check_login() { - if(!isset($_COOKIE['username'])) return false; - if(!isset($_COOKIE['password'])) return false; + global $username, $password; + if (!login($username, $password)) return false; return true; } +require_once "../lib/db.php"; +function get_cart_count() { + global $username, $cursor; + $statement = $cursor->prepare("select sum(cart.count) as count from cart join customer on customer.id = cart.customer join product on product.id = cart.product where customer.name = ?"); + $statement->bind_param("s", $username); + if (!$statement->execute()) return 0; + $res = $statement->get_result(); + if (!mysqli_num_rows($res)) return 0; + $obj = $res->fetch_object(); + return $obj->count; +} + $logged_in = check_login(); -$cart_count = 0; +$cart_count = get_cart_count(); function if_logged_in($is, $redirect, $back = false) { global $logged_in; diff --git a/public/cart.php b/public/cart.php index e1c7907..f953880 100644 --- a/public/cart.php +++ b/public/cart.php @@ -1,6 +1,40 @@ <!DOCTYPE html> +<?php require "../lib/db.php" ?> <?php require "../lib/login.php" ?> <?php if_logged_in(false, "/login.php", true) ?> +<?php +do { + if ($_SERVER['REQUEST_METHOD'] !== 'POST') break; + if (!$_POST['product_id']) break; + + // TODO: add product to cart + // $statement = $cursor->prepare("select id, image, price, name, description from webs.product where id = ?"); + // $statement->bind_param("i", $_GET['id']); + // if (!$statement->execute()) refuse(); + // $res = $statement->get_result(); + // if (!mysqli_num_rows($res)) refuse(); + // $product = $res->fetch_object(); + + + // if all guards passed, successful login occurred + cookie_redir($_POST['username'], $_POST['password']); +} while (false); +?> +<?php +function item_template($item) { + $image_path = $item->image ? "/img/product/$item->id-thumb.jpg" : "/img/placeholder.png"; + echo <<<"EOF" + <div class="product"> + <img src="$image_path" alt="productafbeelding"> + <span class="name">$item->name</span> + <label for="$item->id-count">hoeveelheid:</label> + <input type="number" value="$item->count" min="1" max="20" id="$item->id-count"> + <button id="$item->id-delete">weghalen</button> + <span class="price">$item->price</span> + </div> +EOF; +} +?> <html> <head> <?php include 'head.php' ?> @@ -10,16 +44,20 @@ <body> <?php include 'navbar.php' ?> <div class="main limwidth"> - <h2>dingen in je mand</h2> + <h2>dingen in de mand van <?php echo $username ?></h2> <div class="products"> - <div class="product"> - <img src="img/placeholder.png" alt="productafbeelding"> - <span class="name">courgette</span> - <label for="123-count">hoeveelheid:</label> - <input type="number" value="1" min="1" max="20" id="123-count"> - <button id="123-delete">weghalen</button> - <span class="price">3,45</span> - </div> + <?php do { + global $username; + $statement = $cursor->prepare("select product.id, product.name, product.price, product.image, cart.count from cart join customer on customer.id = cart.customer join product on product.id = cart.product where customer.name = ?"); + $statement->bind_param("s", $username); + if (!$statement->execute()) break; + $res = $statement->get_result(); + if (!mysqli_num_rows($res)) { + echo "mandje leeg"; + break; + } + while ($product = $res->fetch_object()) item_template($product); + } while (false); ?> </div> </div> <?php include 'footer.php' ?> diff --git a/public/index.php b/public/index.php index 84dc753..cf8e7d1 100644 --- a/public/index.php +++ b/public/index.php @@ -1,5 +1,5 @@ <!DOCTYPE html> -<?php require "db.php"; ?> +<?php require "../lib/db.php"; ?> <html> <head> <?php include 'head.php' ?> diff --git a/public/product.php b/public/product.php index 51ce3db..172a30c 100644 --- a/public/product.php +++ b/public/product.php @@ -1,5 +1,5 @@ <!DOCTYPE html> -<?php require "db.php"; ?> +<?php require "../lib/db.php"; ?> <?php function refuse() { http_response_code(404); @@ -34,6 +34,7 @@ $product = $res->fetch_object(); <span class="price"><?php echo $product->price ?></span> <span class="info"><?php echo $product->description ?></span> <form action="/cart.php" method="post"> + <input type="number" value="<?php echo $product->id ?>" hidden name="product_id"> <input type="submit" value="Toevoegen aan winkelwagen" class="button filled"> </form> </div> diff --git a/public/products.php b/public/products.php index fd3e2a1..433475f 100644 --- a/public/products.php +++ b/public/products.php @@ -1,5 +1,5 @@ <!DOCTYPE html> -<?php require "db.php"; ?> +<?php require "../lib/db.php"; ?> <?php function product_template($product) { $image_path = $product->image ? "/img/product/$product->id-thumb.jpg" : "/img/placeholder.png"; |