From 38dc044fa72bdfa144ac0ab54521069004253790 Mon Sep 17 00:00:00 2001
From: Sorrel Harriet <s.harriet@gold.ac.uk>
Date: Fri, 4 Nov 2016 16:19:06 +0000
Subject: [PATCH] adding demo code for lecture

---
 lab-5/vending-machine/index.php      | 65 ++++++++++++++++++++++++++++
 lab-5/vending-machine/sql/schema.sql | 28 ++++++++++++
 2 files changed, 93 insertions(+)
 create mode 100755 lab-5/vending-machine/index.php
 create mode 100755 lab-5/vending-machine/sql/schema.sql

diff --git a/lab-5/vending-machine/index.php b/lab-5/vending-machine/index.php
new file mode 100755
index 0000000..843df65
--- /dev/null
+++ b/lab-5/vending-machine/index.php
@@ -0,0 +1,65 @@
+<?php
+// open a new MySQL database connection
+
+// define a connection 'handle'
+$link = mysqli_connect(
+    'localhost',
+    'sharr003',
+    'password123',
+    'sharr003_vendingmachine'
+);
+
+// check connection succeeded
+if (mysqli_connect_errno()) {
+    echo "Failed to connect to MySQL: " . mysqli_connect_error();
+}
+
+// create variable for page content HTML
+$content = "<h1>Hot Drinks Menu</h1>";
+
+// define the SQL query to run
+$sql = "SELECT name, code, price FROM DrinkOption ORDER BY code";
+
+// fetch records as a result-set
+$result = mysqli_query($link, $sql);
+
+// check query returned a result
+if ($result === false) {
+    echo mysqli_error($link);
+} else {
+	$content .= "<table>
+					<tbody>";
+    // fetch associative array
+    while ($row = mysqli_fetch_assoc($result)) {
+        $content .= "<tr>
+						<td>".$row['name']."</td>
+						<td>".$row['code']."</td>
+						<td> &pound;".$row['price']."</td>
+					</tr>";
+    }
+	$content .= "</tbody></table>";
+    // free result set
+    mysqli_free_result($result);
+}
+
+?>
+
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+    <head>
+        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+        <title>My First PHP Application</title>
+    </head>
+    <body>
+		<ul>
+			<li><a href="index.php">Home</a></li>
+			<li><a href="page2.php">Page 2</a></li>
+		</ul>
+        
+		<?php
+		// Output value of $content variable
+		echo $content;
+		?>
+		
+    </body>
+</html>
diff --git a/lab-5/vending-machine/sql/schema.sql b/lab-5/vending-machine/sql/schema.sql
new file mode 100755
index 0000000..6c3dc41
--- /dev/null
+++ b/lab-5/vending-machine/sql/schema.sql
@@ -0,0 +1,28 @@
+DROP TABLE IF EXISTS Selection, DrinkOption;
+
+CREATE TABLE DrinkOption (
+	code CHAR(2) UNIQUE NOT NULL,
+	drink_name VARCHAR(25) NOT NULL, 
+	price DECIMAL(4,2) DEFAULT 0.80,
+	milk BOOLEAN NOT NULL,
+	PRIMARY KEY (code)
+);
+	
+CREATE TABLE Selection (
+	id INT AUTO_INCREMENT,
+	option_code CHAR(2) NOT NULL,
+	sugars ENUM('0','1','2') NOT NULL,
+	PRIMARY KEY (id),
+	FOREIGN KEY (option_code)
+		REFERENCES DrinkOption (code)
+);
+
+INSERT INTO DrinkOption VALUES
+('A','Black tea',DEFAULT,0),
+('B','White tea',DEFAULT,1),
+('C','Black coffee',DEFAULT,0),
+('D','White coffee',DEFAULT,1);
+
+INSERT INTO Selection VALUES
+(NULL, 'A', '1'),
+(NULL, 'D', '0');
-- 
GitLab