Commit b4e1f461 authored by Sorrel Harriet's avatar Sorrel Harriet

adding intial work on week-6 incarnation/lecture 5 demo

parent b4d7d3a4
*********************************
* RECORD STORE APPLICATION *
*********************************
Description
-----------
This is a demo record store application. You can use it to help you complete lab 5. It is You can also read this README file to find out the sorts of things that should be included in a README file!
Author & Contact
----------------
Sorrel Harriet s.harriet@gold.ac.uk
Installation Instructions
-------------------------
+ Check you have a LAMP stack installed with PHP>5 and MySQL>5
+ Upload the application to your web root folder.
+ Run the record-store.sql file on your database.
+ Run the dummy_data.sql file to insert some data.
Configuration Instructions
--------------------------
Modify the includes/db_connect.php script with your MySQL database credentials.
\ No newline at end of file
<?php
/* Open a new connection to the MySQL server */
/* connect to the database */
$link = mysqli_connect(
'localhost',
'sharr003',
'password123',
'sharr003_recordstore'
);
/* check connection succeeded */
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<?php
// connect to the database
require('includes/db_connect.php');
// include the header HTML
include('templates/header.html');
// include the navigation HTML
include('templates/navigation.html');
// get the page id from the URL
// if no parameter detected...
if (!isset($_GET['page'])) {
$id = 'home'; // display home page
} else {
$id = $_GET['page']; // else requested page
}
// use switch to determine which view to serve based on $id
switch ($id) {
case 'home' :
include 'views/home.php';
break;
case 'record' :
include 'views/record.php';
break;
case 'artist' :
include 'views/artist.php';
break;
case 'orders' :
include 'views/orders.php';
break;
case 'order' :
include 'views/order.php';
break;
default :
include 'views/404.php';
}
// close the connection to the database
mysqli_close($link);
// include the footer HTML
include('templates/footer.html');
?>
/* Note that, because foreign key values are being
inserted manually, tables must be recreated before running
this code in order to reset AUTO_INCREMENT */
/* Statement to insert some records in the artist table */
INSERT INTO artist (id, first_name, last_name)
VALUES
(NULL, 'Bob', 'Marley'),
(NULL, 'Peter', 'Tosh'),
(NULL, 'Burning', 'Spear'),
(NULL, 'Alton', 'Ellis'),
(NULL, 'Gregory', 'Issacs'),
(NULL, 'Desmond', 'Dekker');
INSERT INTO record (ean, title, artist_id, genre, year, price)
VALUES
('00562056', 'Soul Rebel', 1, 'Reggae', 1970, 25.99 ),
('50264967', 'Catch A Fire', 1, 'Reggae', 1973, 25.99 ),
('00748396', 'Natty Dread', 1, 'Reggae', 1974, 20.99 ),
('00495739', 'Babylon By Bus', 1, 'Reggae', 1978, 24.99 ),
('00738432', 'Legalize It', 2, 'Reggae', 1976, 22.99 ),
('50847583', 'Bush Doctor', 2, 'Reggae', 1978, 20.99 ),
('30748743', 'Marcus Garvey', 3, 'Reggae', 1975, 24.99 ),
('50856384', 'Night Nurse', 5, 'Reggae', 1982, 17.99 ),
('50264972', 'Mr Issacs', 5, 'Reggae', 1982, 9.99 ),
('00649573', 'Black and Dekker', 6, 'Reggae', 1980, 19.99 ),
('00625485', 'Sunday Coming', 4, 'Reggae', 1970, 15.99 );
INSERT INTO customer (id, first_name, last_name, email_address, address_1, address_2, postcode)
VALUES
(NULL, 'John', 'Smith', 'john@smith.com', '1 Fake Street', 'London', 'SE3 5RD'),
(NULL, 'Sukie', 'Bapswent', 's.baps@gmail.com', '64 The Terrace', 'Whitby', 'YO65 3TR'),
(NULL, 'John', 'Thumb', 'jthumb@gmail.com', '25 Fantasy Grove', 'Brighton', 'BR2 6LV');
INSERT INTO transaction (id, customer_id, delivery_method, dt_date)
VALUES
(NULL, 1, 2, '2015-07-01 14:34:58'),
(NULL, 1, 2, '2015-04-01 11:22:35'),
(NULL, 3, 1, '2015-04-01 19:47:03'),
(NULL, 2, 1, '2015-05-11 22:01:19');
INSERT INTO orderline (id, transaction_id, record_ean, quantity)
VALUES
(NULL, 1, '00562056', 1),
(NULL, 1, '00495739', 1),
(NULL, 2, '00649573', 2),
(NULL, 2, '00495739', 1),
(NULL, 3, '00738432', 2),
(NULL, 3, '00562056', 1),
(NULL, 3, '00625485', 1),
(NULL, 4, '00562056', 2);
/* Simple query
Fetch first_name and last_name columns from artist table */
SELECT first_name, last_name FROM artist;
/* Query with filters
Fetches titles from record table where year is 1973 and genre is Reggae */
SELECT title FROM record
WHERE year = 1973
AND genre = "Reggae";
\ No newline at end of file
/* Make sure tables don't exist before creation */
DROP TABLE IF EXISTS orderline, transaction, customer, record, artist;
/* Define table for storing artists */
CREATE TABLE artist (
id INT AUTO_INCREMENT,
first_name VARCHAR(50),
last_name VARCHAR(50),
PRIMARY KEY(id)
) ENGINE=InnoDB;
/* Define table for storing records (products) */
CREATE TABLE record (
ean VARCHAR(8),
title VARCHAR(50) NOT NULL,
artist_id INT,
genre VARCHAR(50),
year YEAR(4),
price DECIMAL(10, 2) unsigned NOT NULL,
PRIMARY KEY (ean),
FOREIGN KEY (artist_id)
REFERENCES artist (id)
ON DELETE CASCADE
) ENGINE=InnoDB;
/* Define table for storing customers */
CREATE TABLE customer (
id INT AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email_address VARCHAR(50) NOT NULL,
address_1 VARCHAR(50) NOT NULL,
address_2 VARCHAR(50),
postcode VARCHAR(10) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB;
/* Define table for storing orders */
CREATE TABLE transaction (
id INT AUTO_INCREMENT,
customer_id INT NOT NULL,
delivery_method INT,
dt_date DATETIME,
PRIMARY KEY (id),
FOREIGN KEY (customer_id)
REFERENCES customer(id)
) ENGINE=InnoDB;
/* Define table for storing orderlines */
CREATE TABLE orderline (
id INT AUTO_INCREMENT,
transaction_id INT,
record_ean VARCHAR(8),
quantity INT NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (transaction_id)
REFERENCES transaction(id),
FOREIGN KEY (record_ean)
REFERENCES record(ean)
ON UPDATE CASCADE
ON DELETE CASCADE
) ENGINE=InnoDB;
<h3>Find a record!</h3>
<p>Enter an artist name or record title.</p>
<form action="results.php" method="GET">
<fieldset>
<label for="artist_name">Artist name</label>
<input type="text" name="artist_name" />
<label for="record_title">Title</label>
<input type="text" name="record_title" />
</fieldset>
<button type="submit">Submit</button>
</form>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Record Store</title>
</head>
<body>
\ No newline at end of file
<nav>
<ul>
<li><a href="?page=home" title="home">Home</a></li>
<li><a href="?page=record" title="records">Records</a></li>
<li><a href="?page=orders" title="orders">Orders</a></li>
</ul>
</nav>
<h3>Find a record!</h3>
<p>Enter an artist name or record title.</p>
<form action="results.php" method="GET">
<fieldset>
<label for="artist_name">Artist name</label>
<input type="text" name="artist_name" />
<label for="record_title">Title</label>
<input type="text" name="record_title" />
</fieldset>
<button type="submit">Submit</button>
</form>
<?php
// create variable for content HTML
$content = "<h1>Page not found</h1>";
$content .= "<p>Sorry, the page you requested could not be found.</p>";
// output the content HTML
echo $content;
?>
<?php
// check if id parameter was not set in query string
if (!isset($_GET['id'])) {
// define $content with suitable message
$content = "<h1>I don't know which artist you're looking for...</h1>";
} else { // id was set, so carry on...
// define $artist_id variable and assign value of id parameter
$artist_id = $_GET['id'];
// fetch record titles for artist with id matching $artist_id
$sql = "SELECT r.title, r.year, r.price, a.first_name, a.last_name
FROM record r
INNER JOIN artist a
ON r.artist_id=a.id
WHERE a.id=".$artist_id."
ORDER BY year ASC";
$result = mysqli_query($link, $sql);
// check query returned a result
if ($result === false) {
echo mysqli_error($link);
} else {
// define a row counter
$i = 0;
// fetch associative array
while ($row = mysqli_fetch_assoc($result)) {
// do this if we are on first row
if ($i == 0) {
// initialise $content string, assigning it a page header
$content = "<h1>".$row['first_name']." ".$row['last_name']." Records</h1>";
// append $content string with table definition
$content .= "<table border='1'><tbody>";
}
// append table rows to $content string
$content .= "<tr>";
$content .= "<td>".$row['title']."</td>";
$content .= "<td>".$row['year']."</td>";
$content .= "<td>&pound;".$row['price']."</td>";
$content .= "</tr>";
// increment the row counter
$i++;
}
// append $content string with closing table tags
$content .= "</tbody></table>";
// free result set
mysqli_free_result($result);
}
}
// output the content HTML
echo $content;
?>
<?php
// create variable for content HTML
$content = "<h1>Artists</h1>";
// fetch records as a result set
$sql = "SELECT first_name, last_name FROM artist";
$result = mysqli_query($link, $sql);
// check query returned a result
if ($result === false) {
echo mysqli_error($link);
} else {
$content .= "<table border='1'><tbody>";
// fetch associative array
while ($row = mysqli_fetch_assoc($result)) {
$content .= "<tr>";
$content .= "<td>".$row['first_name']."</td>";
$content .= "<td>".$row['last_name']."</td>";
$content .= "</tr>";
}
$content .= "</tbody></table>";
// free result set
mysqli_free_result($result);
}
// output the content HTML
echo $content;
?>
<?php
// create variable for content HTML
$content = "<h1>Welcome to Goldsmith's Record Store</h1>";
$content .= "<p>Follow the links above to browse the store.</p>";
// output the content HTML
echo $content;
?>
<?php
/* **************************************************************
* TASK 2: Create a view which: *
* - outputs the order details for an order based *
* on the value of the order_id parameter *
* - for each item in the order, the following *
* details should be displayed: *
* | EAN | TITLE | QUANTITY | PRICE | SUBTOTAL | *
* - the order total should also be displayed. *
****************************************************************/
// check the order_id parameter has been set in the URL
if (isset($_GET['order_id']))
{
$order_id = $_GET['order_id'];
} else {
$order_id = -1; // if not, set to an implausible value
}
// fetch order details associated with current order id
$sql = "INSERT YOUR SQL QUERY HERE!";
$result = mysqli_query($link, $sql);
// check query returned a result
if ($result === false) {
echo mysqli_error($link);
} else {
// Find the number of rows returned
$num_rows = mysqli_num_rows($result);
// Check it's not 0
if ($num_rows == 0) {
$content = "<h1>Order not found</h1>";
} else {
// create variable for content HTML
$content = "<h1>Order ".$order_id."</h1>";
$content .= "<table border='1'>";
$content .= "<thead><tr>
<th>EAN</th>
<th>Title</th>
<th>Quantity</th>
<th>Price</th>
<th>Total</th>
</tr></thead>";
$content .= "<tbody>";
// initialise total order price to 0
$total = 0.00;
// fetch associative array
while ($row = mysqli_fetch_assoc($result)) {
$subtotal = 0.00; // <-- CALCULATE SUBTOTAL!
$total = 0.00; // <-- KEEP RUNNING ORDER TOTAL!
$content .= "<tr>";
$content .= "<td>".$row['ean']."</td>";
$content .= "<td>".$row['title']."</td>";
$content .= "<td>".$row['quantity']."</td>";
$content .= "<td>&pound;".$row['price']."</td>";
$content .= "<td>&pound;".$subtotal."</td>";
$content .= "</tr>";
}
$content .= "<tr><td colspan=4><b>TOTAL</b><td><b>&pound;".$total."</b></td></tr>";
$content .= "</tbody></table>";
// free result set
mysqli_free_result($result);
}
}
// output the content HTML
echo $content;
?>
<?php
// check the order_id parameter has been set in the URL
if (isset($_GET['order_id']))
{
$order_id = $_GET['order_id'];
} else {
$order_id = -1; // if not, set to an implausible value
}
// fetch order details associated with current order id
$sql = "SELECT r.ean, r.title, ol.quantity, ol.transaction_id, r.price
FROM record r
INNER JOIN orderline ol
ON ol.record_ean=r.ean
WHERE ol.transaction_id=".$order_id;
$result = mysqli_query($link, $sql);
// check query returned a result
if ($result === false) {
echo mysqli_error($link);
} else {
// Find the number of rows returned
$num_rows = mysqli_num_rows($result);
// Check it's not 0
if ($num_rows == 0) {
$content = "<h1>Order not found</h1>";
} else {
// create variable for content HTML
$content = "<h1>Order ".$order_id."</h1>";
$content .= "<table border='1'>";
$content .= "<thead><tr>
<th>EAN</th>
<th>Title</th>
<th>Quantity</th>
<th>Price</th>
<th>Total</th>
</tr></thead>";
$content .= "<tbody>";
// initialise total order price to 0
$total = 0.00;
// fetch associative array
while ($row = mysqli_fetch_assoc($result)) {
$subtotal = $row['quantity'] * $row['price'];
$total = $total + $subtotal;
$content .= "<tr>";
$content .= "<td>".$row['ean']."</td>";
$content .= "<td>".$row['title']."</td>";
$content .= "<td>".$row['quantity']."</td>";
$content .= "<td>&pound;".$row['price']."</td>";
$content .= "<td>&pound;".$subtotal."</td>";
$content .= "</tr>";
}
$content .= "<tr><td colspan=4><b>TOTAL</b><td><b>&pound;".$total."</b></td></tr>";
$content .= "</tbody></table>";
// free result set
mysqli_free_result($result);
}
}
// output the content HTML
echo $content;
?>
<?php
/* **************************************************************
* TASK 1: Create a view which: *
* - outputs a list of all transactions (orders) *
* - the orders should be grouped by customer ID *
* - each transaction links to an `order' view, setting a *
* parameter `order_id' in the URL query string *
****************************************************************/
// initialise string variable for content HTML
$content = "<h1>Orders</h1>";
// fetch all transactions (orders) and group by customer id
$sql = "INSERT YOUR SQL STATEMENT HERE!";
$result = mysqli_query($link, $sql);
// check query returned a result
if ($result === false) {
echo mysqli_error($link);
} else {
$num_rows = mysqli_num_rows($result);
if ($num_rows > 0)
{
$content .= "<table border='1'>";
$content .= "<thead><tr><th>Order ID</th><th>Customer ID</th></tr></thead>";
$content .= "<tbody>";
// fetch each row in result set as an associative array
while ($row = mysqli_fetch_assoc($result)) {
$content .= "<tr>";
$content .= "</tr>";
}
$content .= "</tbody></table>";
} else {
$content .= "<p>There are no orders to display.</p>";
}
// free result set
mysqli_free_result($result);
}
// output the content HTML
echo $content;
?>
<?php
// initialise string variable for content HTML
$content = "<h1>Orders</h1>";
// fetch all transactions (orders) and group by customer id
$sql = "SELECT id, customer_id FROM transaction
ORDER BY customer_id";
$result = mysqli_query($link, $sql);
// check query returned a result
if ($result === false)
{
echo mysqli_error($link);
} else {
$num_rows = mysqli_num_rows($result);
if ($num_rows > 0)
{
$content .= "<table border='1'>";
$content .= "<thead><tr><th>Order ID</th><th>Customer ID</th></tr></thead>";
$content .= "<tbody>";
// fetch each row in result set as an associative array
while ($row = mysqli_fetch_assoc($result)) {
$content .= "<tr>";
$content .= "<td><a href=\"?page=order&order_id=".$row['id']."\">".$row['id']."</a></td>";
$content .= "<td>".$row['customer_id']."</td>";
$content .= "</tr>";
}
$content .= "</tbody></table>";
} else {
$content .= "<p>There are no orders to display.</p>";
}
// free result set
mysqli_free_result($result);
}
// output the content HTML
echo $content;
?>
<?php
// create variable for content HTML
$content = "<h1>Records</h1>";
// fetch records as a result set
$sql = "SELECT r.title, a.first_name, a.last_name, r.genre, r.price, a.id
FROM record r
INNER JOIN artist a
ON r.artist_id=a.id
ORDER BY r.title, r.price DESC";
$result = mysqli_query($link, $sql);
// check query returned a result
if ($result === false) {
echo mysqli_error($link);
} else {
$content .= "<table border='1'><tbody>";