Respuesta :
A program that creates a virtual online grocery website
The PHP file
<html>
<head>
</head>
<body>
<?php
$servername = "localhost";
$username = "///";
$password = "///";
$dbname = "assignment1";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection error: " . $conn->connect_error);
}
$product_name = "";
$unit_price = "";
$unit_quantity = "";
$in_stock = "";
$itemId = "";
$showNoItem = "display: none";
$showItem = "";
if (isset($_GET['data'])) {
$itemId = $_GET['data'];
$sql = "SELECT product_id , product_name , unit_price, unit_quantity, in_stock FROM products where product_id=".$itemId;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$showNoItem = "display: none";
$showItem = "";
while($row = $result->fetch_assoc()) {
$product_name = $row["product_name"];
$unit_price = $row["unit_price"];
$unit_quantity = $row["unit_quantity"];
$in_stock = $row["in_stock"];
break;
}
} else {
$showNoItem = "";
$showItem = "display: none";
}
} else {
$showNoItem = "";
$showItem = "display: none";
}
?>
<div id="noItem" style="<?php echo $showNoItem?>">Select items from categories on the left.</div>
<div id="itemDiv" style="<?php echo $showItem?>">
<div class="item-title">
<span class="item-name"><?php echo $product_name?> </span>
(<span class="item-quatity"><?php echo $unit_quantity?></span>)
</div>
<div class="itemDetail">
<div class="item-desp">
<div class="in-stock-div">In Stock: <span class="item-in-stock"><?php echo $in_stock?> </span> </div>
<div class="price-tag-div">Price: <span class="item-price-red">${data-answer}lt;?php echo $unit_price ?></span></div>
<p></p>
<form action="cart.php" method="get" target="cart" class="order-row" onsubmit="return validate_quantity">
<input type="number" class="item-quatity-input spin0" min="1" value="1" name="display" id="display" onkeyup="addCartButtonCtrl()" >
<input type="hidden" name="productId" value="<?php echo $itemId?>">
<input type="hidden" name="productInfo" value='<?php echo "$product_name($unit_quantity)"?>'>
<input type="hidden" name="productPrice" value="<?php echo $unit_price ?>" >
<div class="add-cart-div">
<input id="cart-button" class="btn btn-primary" type="submit" value="Add to Cart" title="Add to cart." onclick="updateShoppingCart()">
</div>
</form>
</div>
</div>
</div>
<?php
$conn->close();
?>
</body>
</html>
Adding the CartButton
addCartButtonCtrl(){
$action = $_GET['action'];
switch ($action) {
case 'Add':
$product_id = $_GET['productId'];
$product_name = $_GET['productInfo'];
$unit_price = $_GET['ProductPrice'];
if(!isset($_SESSION['cart'])) {
$_SESSION['cart']=array();
}
$index = getItemIndex($product_id);
if ($index < 0) {
$item_array=array('product_id' => $product_id,
'product_name' => $product_name,
'unit_price' => $unit_price,
}
Read more about programming here:
https://brainly.com/question/23275071
#SPJ1