From abe57a53042c94184df19400209c12bd708be55f Mon Sep 17 00:00:00 2001 From: totoW <anthony.weiss1910@gmail.com> Date: Fri, 12 Jan 2024 13:10:15 +0100 Subject: [PATCH] stock managment --- html/data_treatment/AJAXendcheckout.php | 9 +++++++++ startbootstrap-agency-gh-pages/index.php | 11 ++++++++-- .../purchased_item.php | 8 ++++++++ .../shoppingPage.php | 20 +++++++++++++++++-- .../thank_you_page.php | 8 ++++++++ 5 files changed, 52 insertions(+), 4 deletions(-) diff --git a/html/data_treatment/AJAXendcheckout.php b/html/data_treatment/AJAXendcheckout.php index e13b366..4c14365 100644 --- a/html/data_treatment/AJAXendcheckout.php +++ b/html/data_treatment/AJAXendcheckout.php @@ -52,6 +52,15 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $text = ""; foreach ($decoded_items as $item) { // Decode Unicode escape sequences within the itemName + $itemName = $item['itemName']; + $quantityOrdered = $item['quantity']; + + // Adjust the SQL query based on your table structure + $updateQuantitySQL = "UPDATE articles SET quantity = quantity - ? WHERE itemName = ?"; + $stmt = $conn->prepare($updateQuantitySQL); + $stmt->bind_param("is", $quantityOrdered, $itemName); + $stmt->execute(); + $stmt->close(); $item['itemName'] = json_decode('"' . $item['itemName'] . '"'); $text .= "- " . $item['quantity'] . " " . $item['itemName'] . " which costs "; if ($item['quantity'] >= 16) { diff --git a/startbootstrap-agency-gh-pages/index.php b/startbootstrap-agency-gh-pages/index.php index 831b0fc..aea9ebe 100644 --- a/startbootstrap-agency-gh-pages/index.php +++ b/startbootstrap-agency-gh-pages/index.php @@ -233,7 +233,9 @@ error: function (error) { </section> <?php - $sql = "select * from articles"; + + $sql = "SELECT * FROM articles WHERE quantity > 0"; + $result = $conn->query($sql); $articles = array(); while ($row = $result->fetch_assoc()) { @@ -313,6 +315,8 @@ error: function (error) { <img class="img-fluid d-block mx-auto" src=<?php echo "'../image/laptops/".$articles[$i]["imagename"]."'"; ?> alt="..." /> <p><?php echo $articles[$i]["description"]; ?></p> <p><?php echo "Price: ".$articles[$i]["price"]." € <br>Quantity available: ".$articles[$i]["quantity"];?></p> + <input type="hidden" id=hiddenquantity value=<?php echo $articles[$i]["quantity"];?>> + <?php if(isset($_SESSION["username"])){?> <div class="quantity"> <button onclick="decrementQuantity(this)">-</button> <input type="text" value="1" <?php echo 'id="quantity'.$i.'"'; ?>> @@ -325,12 +329,15 @@ error: function (error) { onclick="addToCart(this)"> Add to shopping cart </button> - + <?php } ?> <script> function incrementQuantity(button) { var input = button.parentNode.querySelector('input'); + var quantity = parseInt(button.parentNode.parentNode.querySelector('input[type="hidden"]').value,10); var value = parseInt(input.value, 10); + if(value+1<= quantity){ input.value = value + 1; + } } function decrementQuantity(button) { diff --git a/startbootstrap-agency-gh-pages/purchased_item.php b/startbootstrap-agency-gh-pages/purchased_item.php index 2eb7d5d..8064454 100644 --- a/startbootstrap-agency-gh-pages/purchased_item.php +++ b/startbootstrap-agency-gh-pages/purchased_item.php @@ -1,3 +1,11 @@ +<?php +session_start(); +include("../html/data_treatment/update_activity.php"); +if(!isset($_SESSION["username"])){ + header("location: ../html/login.php"); + exit; +} +?> <button type="button" onclick="redirectToPage()">Return</button> <br><br><br> <script> diff --git a/startbootstrap-agency-gh-pages/shoppingPage.php b/startbootstrap-agency-gh-pages/shoppingPage.php index 2c80018..ec13468 100644 --- a/startbootstrap-agency-gh-pages/shoppingPage.php +++ b/startbootstrap-agency-gh-pages/shoppingPage.php @@ -83,11 +83,21 @@ while ($row = $result->fetch_assoc()) { </div> </div> </section> - <?php foreach($_SESSION["panier"] as $item){ ?> + <?php foreach($_SESSION["panier"] as $item){ + $sql = "SELECT * FROM articles WHERE quantity > 0 AND itemName = '" . $item["itemName"] . "'"; + + + $result = $conn->query($sql); + $articles = array(); + while ($row = $result->fetch_assoc()) { + $articles[] = $row; + } + ?> <div class="product"> <img <?php echo 'src="../image/laptops/'.$item['imagename'].'"'?> style="width:10%"> <h2><?php echo $item["itemName"];?></h2> <input type="hidden" value="<?php echo $item["price"];?>" id=unitprice-<?php echo $item["itemName"];?>> + <input type="hidden" id=hiddenquantity value=<?php echo $articles[0]["quantity"];?>> <p id="price-<?php echo $item["itemName"]; ?>" data-price="<?php echo $item["price"]; ?>"> <?php if($item['quantity']>=16){ echo "Price: ".$item["price"]*$item["quantity"]*(1-0.16)." € (16% discount)"; @@ -127,10 +137,16 @@ while ($row = $result->fetch_assoc()) { function incrementQuantity(button, itemName) { var input = button.parentNode.querySelector('input'); var value = parseInt(input.value, 10); - input.value = value + 1; + var maxquantity = parseInt(button.parentNode.parentNode.querySelector('input[type="hidden"]:nth-of-type(2)').value,10); + console.log(maxquantity); + if(value+1<= maxquantity){ + input.value = value + 1; + } updateQuantity(itemName, input.value); var price = button.parentNode.parentNode.querySelector('p'); var unitprice = button.parentNode.parentNode.querySelector('input[type="hidden"]'); + + if(input.value>=16){ price.innerText = "Price: "+parseInt(unitprice.value,10)*input.value*(1-0.16)+" € (16% discount)"; } else if(input.value>=8){ diff --git a/startbootstrap-agency-gh-pages/thank_you_page.php b/startbootstrap-agency-gh-pages/thank_you_page.php index 4ab1201..8390de5 100644 --- a/startbootstrap-agency-gh-pages/thank_you_page.php +++ b/startbootstrap-agency-gh-pages/thank_you_page.php @@ -1,3 +1,11 @@ +<?php +session_start(); +include("../html/data_treatment/update_activity.php"); +if(!isset($_SESSION["username"])){ + header("location: ../html/login.php"); + exit; +} +?> <!DOCTYPE html> <html lang="en"> -- GitLab