Last active
April 30, 2020 23:45
-
-
Save AhmedKorim/862d7d18b06cd90222d9aeea8001e76c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
DEFINE('DB_USER', 'root'); | |
DEFINE('DB_PASSWORD', 'root'); | |
DEFINE('DB_HOST', 'localhost'); | |
DEFINE('DB_NAME', 'minor_shopping_cart'); | |
function db_connect() { | |
$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, "3306"); | |
return $connection; | |
} | |
function insert_product($order_id, $product_id, $qty, $con) { | |
$sql = "INSERT INTO order_products(product_id, order_id , qty) values (" . $product_id . "," . $order_id . "," . $qty . ")"; | |
mysqli_query($con, $sql); | |
} | |
$connection = db_connect(); | |
if ($_SERVER['REQUEST_METHOD'] === 'POST') { | |
// post request | |
$count_coke = $_POST['coke'] ?? ''; | |
$count_sprite = $_POST['sprite'] ?? ''; | |
$count_sandwiches = $_POST['sandwiches'] ?? ''; | |
$count_cake = $_POST['cake'] ?? ''; | |
$count_pizza = $_POST['pizza'] ?? ''; | |
$count_coke_id = $_POST['item-id-coke'] ?? ''; | |
$count_sprite_id = $_POST['item-id-sprite'] ?? ''; | |
$count_sandwiches_id = $_POST['item-id-sandwiches'] ?? ''; | |
$count_cake_id = $_POST['item-id-cake'] ?? ''; | |
$count_pizza_id = $_POST['item-id-pizza'] ?? ''; | |
// create order | |
// create order | |
$amount = 0; | |
$amount += 6 * $count_coke; | |
$amount += 6.5 * $count_sprite; | |
$amount += 12 * $count_sandwiches; | |
$amount += 13 * $count_cake; | |
$amount += 21 * $count_pizza; | |
$amount = $amount - $amount * .1; | |
$sql = "INSERT INTO orders (amount) values (" . $amount . ")"; | |
$result = mysqli_query($connection, $sql); | |
if ($result) { | |
$last_id = mysqli_insert_id($connection); | |
insert_product($last_id, $count_coke_id, $count_coke, $connection); | |
insert_product($last_id, $count_sprite_id, $count_sprite, $connection); | |
insert_product($last_id, $count_sandwiches_id, $count_sandwiches, $connection); | |
insert_product($last_id, $count_cake_id, $count_cake, $connection); | |
insert_product($last_id, $count_pizza_id, $count_pizza, $connection); | |
echo "<h1>Order " . $last_id . "<br/>" . "Total Cost " . $amount . " </h1>" . "<br/> You have order " . $count_coke . " coke " . $count_sprite . " sprite " . | |
$count_sandwiches . " sandwiches " | |
. $count_cake . " cake " . $count_pizza . " pizza "; | |
} else { | |
echo "Error 500"; | |
} | |
// check the user on the data base | |
} else { | |
$sql = "SELECT * FROM product"; | |
$res = mysqli_query($connection, $sql); | |
$place_order = ""; | |
$place_order .= "<!DOCTYPE html> | |
<html lang=\"en\"> | |
<head> | |
<meta charset=\"UTF-8\"> | |
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"> | |
<title>Project Assessment</title> | |
<link rel=\"stylesheet\" href=\"../assets/style.css\"> | |
</head> | |
<body> | |
<form action='./index.php' method='post'> | |
<table> | |
<tbody> | |
<tr class=\"header\"> | |
<th>Logo</th> | |
<th colspan=\"2\">SPACE Catering Company</th> | |
<th colspan=\"2\">username</th> | |
</tr> | |
<tr> | |
<td colspan=\"5\" class=\"menu\"> | |
<a href=\"javascript:void(0)\">Place order</a> | |
<a href=\"../list_orders\">Check order</a> | |
</td> | |
</tr> | |
<tr> | |
<td colspan=\"5\"> | |
<div class=\"container\"> | |
<h2>Place Order</h2> | |
<p class=\"date\">Date: today</p> | |
<table border=\"1\" id=\"inner-table\"> | |
<thead> | |
<tr> | |
<th>Item#</th> | |
<th>Desc</th> | |
<th>Picture</th> | |
<th>Qty</th> | |
<th>Price</th> | |
<th>Amount</th> | |
</tr></thead><tbody>"; | |
while ($row = mysqli_fetch_assoc($res)) { | |
$place_order .= "<tr>"; | |
$place_order .= "<td >" . $row['id'] . "</td>"; | |
$place_order .= "<td>" . $row['description'] . "</td>"; | |
$place_order .= "<td>" . $row['image'] . "</td>"; | |
$place_order .= "<td><input oninput=\"countTotals()\" type='number' id='" . strtolower($row['id']) | |
. "' name='" . strtolower($row['description']) . "'/><input type='hidden' class=\"item-amount\" value='" . $row['id'] . "' name='item-id-" . | |
strtolower | |
($row['description']) . "'/></td>"; | |
$place_order .= "<td id='price-" . $row['id'] . "'>" . $row['price'] . "</td>"; | |
$place_order .= "<td><input id='item-amount-" . strtolower($row['id']) . "' name='" . strtolower($row['description']) . " disabled'/></td>"; | |
$place_order .= "</tr>"; | |
} | |
$place_order .= " <tr> | |
<td colspan=\"3\"></td> | |
<td colspan=\"2\">Sub-Total</td> | |
<td id=\"sub-total\"></td> | |
</tr> | |
<tr> | |
<td colspan=\"3\"></td> | |
<td colspan=\"2\">Discount</td> | |
<td id=\"discount\"></td> | |
</tr> | |
<tr> | |
<td colspan=\"3\"></td> | |
<td colspan=\"2\">Delivery Charges</td> | |
<td id=\"delivery-charges\"></td> | |
</tr> | |
<tr> | |
<td colspan=\"3\"></td> | |
<td colspan=\"2\">Grand Total</td> | |
<td id=\"grand-total\"></td> | |
</tr></tbody></table> | |
</div> | |
</td> | |
</tr> | |
<tr> | |
<td colspan=\"5\"> | |
<button type='submitæ' id=\"submit-button\">Submit</button> | |
</td> | |
</tr> | |
<tr> | |
<td colspan=\"5\">Copyright line</td> | |
</tr> | |
</tbody> | |
</table> | |
</form> | |
<script src=\"../assets/main.js\"></script> | |
</body> | |
</html>"; | |
echo $place_order; | |
} | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment