From e031215516573b14545e81665b991176a467bce5 Mon Sep 17 00:00:00 2001 From: Sudipto Mallick Date: Sat, 3 Feb 2024 16:29:27 +0000 Subject: Implement PHP assignment #3, improve the rest --- mysql-php/code/a1.php | 24 ++++----- mysql-php/code/a2.php | 13 +++-- mysql-php/code/a3.php | 132 +++++++++++++++++++++++++++-------------------- mysql-php/output/3/1.png | Bin 0 -> 45417 bytes mysql-php/output/3/2.png | Bin 0 -> 34124 bytes 5 files changed, 95 insertions(+), 74 deletions(-) create mode 100644 mysql-php/output/3/1.png create mode 100644 mysql-php/output/3/2.png diff --git a/mysql-php/code/a1.php b/mysql-php/code/a1.php index 4569ca8..614f044 100644 --- a/mysql-php/code/a1.php +++ b/mysql-php/code/a1.php @@ -1,7 +1,7 @@ $desc) { - if (!isset($_POST[$name]) || empty($_POST[$name])) + if (empty($_POST[$name])) display_failure('The ' . $desc . ' field can not be empty'); $vars[$name] = $_POST[$name]; } @@ -186,7 +186,7 @@ function show_record($record) { Path ' . htmlspecialchars($path) . ' was not found.

'; diff --git a/mysql-php/code/a2.php b/mysql-php/code/a2.php index 10a7a33..9d9a1a5 100644 --- a/mysql-php/code/a2.php +++ b/mysql-php/code/a2.php @@ -1,7 +1,7 @@ set_charset('utf8mb4'); + $dbh->query('CREATE DATABASE IF NOT EXISTS EATERY_DB'); + $dbh->select_db('EATERY_DB'); + $dbh->query('CREATE TABLE IF NOT EXISTS `FOOD_DETAILS` (`FOOD_ID` INT PRIMARY KEY, `FOOD_ITEM` VARCHAR(255), `PRICE_PER_ITEM` DOUBLE, UNIQUE (`FOOD_ID`))'); + $dbh->query('INSERT INTO `FOOD_DETAILS` VALUES + (1, "Egg Biriyani (w/ 2 eggs)", 200), + (2, "Chicken Biriyani", 300), + (3, "Mutton Biriyani", 350), + (4, "Veg Pulao", 200), + (5, "Shahi Paneer", 220), + (6, "Schezwan Paneer", 240), + (7, "Steam Rice", 90) + ON DUPLICATE KEY UPDATE `FOOD_ITEM` = VALUES(`FOOD_ITEM`)'); + $dbh->query('CREATE TABLE IF NOT EXISTS `CUSTOMER_DETAILS` (`BILL_ID` INT PRIMARY KEY AUTO_INCREMENT, `CUSTOMER_NAME` VARCHAR(255), `TOTAL_AMOUNT_PAID` DOUBLE, `DATE_OF_PAYMENT` DATE)'); return $dbh; } @@ -32,22 +33,14 @@ h2 { font-size: 1.8rem; font-weight: 500; } form { margin: 2em auto; width: 20em; } form > * { padding: 0.5em; } table, tr, th, td { border-collapse: collapse; border: 1px solid black; } +table.noborder, table.noborder * { border: none; } +table.invoice td:nth-child(2), table.invoice td:nth-child(3) { text-align: end; } +p.right { text-align: right; } th, td { padding: 5px; } -
-

Enter E-mail address to search for the record of a student

- - -
- @@ -57,39 +50,68 @@ function display_failure($reason) { die(); } -function search_and_show($dbh, $email) { - $stmt = mysqli_prepare($dbh, 'SELECT * FROM STUDENT WHERE EMAIL = ?'); - $result = mysqli_query($dbh, ); - html_prologue('Students\' details'); +function show_menu($dbh) { + html_prologue('Eatery Menu'); ?> -

Students' details

-

record(s) found.

- - - - - - - - - - "> +

Welcome to Delish Eatery

+
Roll No.NameE-mailCityDate of birth
- ', array_map('htmlspecialchars', [ - $row['ROLL'], $row['NAME'], $row['EMAIL'], $row['CITY'], - $row['DATE_OF_BIRTH'] - ])); ?> -
+ + + query('SELECT * FROM `FOOD_DETAILS`'); + while ($row = $result->fetch_assoc()) { + echo ''; + echo '', ''; } ?> +
ServingsFood itemPrice
', $row['FOOD_ITEM'], '₹', number_format(+$row['PRICE_PER_ITEM'], 2), '
- + query('SELECT * FROM `FOOD_DETAILS` WHERE `FOOD_ID` IN (' . implode(', ', $orders) . ')')->fetch_all(MYSQLI_ASSOC); + $total_price = 0.0; + foreach ($items as $i => $item) { + $items[$i]['price'] = $servings[$item['FOOD_ID']] * $item['PRICE_PER_ITEM']; + $total_price += $items[$i]['price']; + } + $tax = 0.15 * $total_price; + $net_price = $total_price + $tax; + $stmt = $dbh->prepare('INSERT INTO `CUSTOMER_DETAILS` (`CUSTOMER_NAME`, `TOTAL_AMOUNT_PAID`, `DATE_OF_PAYMENT`) VALUES (?, ?, ?)'); + $stmt->bind_param('sds', $_POST['customer_name'], $net_price, @date('Y-m-d')); + $stmt->execute(); + $bill_id = $dbh->insert_id; + html_prologue('Customer invoice'); + ?> +

Customer Invoice

+

Bill No.: D.E./

+

Name:

+ + +'; +} ?> + + + +
Food itemServingsPrice
', implode('', [$item['FOOD_ITEM'], $servings[$item['FOOD_ID']], number_format($item['price'], 2)]), '
Total:
GST (15%):
Net price:
+ close(); +} catch (mysqli_sql_exception $e) { + display_failure($e->getMessage()); } + diff --git a/mysql-php/output/3/1.png b/mysql-php/output/3/1.png new file mode 100644 index 0000000..55e8f05 Binary files /dev/null and b/mysql-php/output/3/1.png differ diff --git a/mysql-php/output/3/2.png b/mysql-php/output/3/2.png new file mode 100644 index 0000000..f8ee1b7 Binary files /dev/null and b/mysql-php/output/3/2.png differ -- cgit 1.4.1-2-gfad0