diff --git a/lab-6/music-store-app/includes/db_connect.php b/lab-6/music-store-app/includes/db_connect.php new file mode 100644 index 0000000000000000000000000000000000000000..3c298f2ad571c8bd84af92df0e74b11c75e1a842 --- /dev/null +++ b/lab-6/music-store-app/includes/db_connect.php @@ -0,0 +1,16 @@ +<?php + +// define a connection 'handle' +$link = mysqli_connect( + 'localhost', + 'sorrel', + 'password123', + 'musicstore' +); + +// check connection succeeded +if (mysqli_connect_errno()) { + exit(mysqli_connect_error()); +} + +?> diff --git a/lab-6/music-store-app/index.php b/lab-6/music-store-app/index.php new file mode 100755 index 0000000000000000000000000000000000000000..ddc6ee34892db4631a687b4f9b4b33b34c3b8d4d --- /dev/null +++ b/lab-6/music-store-app/index.php @@ -0,0 +1,52 @@ +<?php + +// include the HTML for the page header +include "templates/header.html"; + +// include the HTML for the navigation bar +include "templates/nav.html"; + +// open a new MySQL database connection +require "includes/db_connect.php"; + +// check if 'page' parameter is set in query string +if (isset($_GET['page'])) { + $page = $_GET['page']; // if so, set page variable to value of 'page' parameter +} else { + $page = 'home'; // if not, set page variable to home +} + +// define a variable to store content HTML +$content = ""; + +// determine which view to serve based on value of $page +switch ($page) { +case 'home' : + include 'views/home.php'; + break; +case 'albums' : + include 'views/albums.php'; + break; +case 'album' : + include 'views/album.php'; + break; +case 'customers' : + include 'views/customers.php'; + break; +case 'orders' : + include 'views/orders.php'; + break; +default : + include 'views/404.php'; +} + +// close the connection to the database */ +mysqli_close($link); + +// output the HTML +echo $content; + +// +include "templates/footer.html"; + +?> diff --git a/lab-6/music-store-app/templates/footer.html b/lab-6/music-store-app/templates/footer.html new file mode 100644 index 0000000000000000000000000000000000000000..308b1d01b6ca1e7ab1b1fa896e6a8497bbcd1a37 --- /dev/null +++ b/lab-6/music-store-app/templates/footer.html @@ -0,0 +1,2 @@ +</body> +</html> diff --git a/lab-6/music-store-app/templates/header.html b/lab-6/music-store-app/templates/header.html new file mode 100644 index 0000000000000000000000000000000000000000..e08b370b57e850fe50c819bd39b13ea9f5e277e2 --- /dev/null +++ b/lab-6/music-store-app/templates/header.html @@ -0,0 +1,7 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset='utf-8'/> +<title>Music Store: {{ page }}</title> +</head> +<body> diff --git a/lab-6/music-store-app/templates/nav.html b/lab-6/music-store-app/templates/nav.html new file mode 100644 index 0000000000000000000000000000000000000000..ad948e134d2b84f886865e426ff128cb1f1cc21d --- /dev/null +++ b/lab-6/music-store-app/templates/nav.html @@ -0,0 +1,8 @@ +<nav> + <ul> + <li><a href='index.php?page=home'>Home</a></li> + <li><a href='index.php?page=albums'>Albums</a></li> + <li><a href='index.php?page=customers'>Customers</a></li> + <li><a href='index.php?page=orders'>Orders</a></li> + </ul> +</nav> diff --git a/lab-6/music-store-app/views/album.php b/lab-6/music-store-app/views/album.php new file mode 100644 index 0000000000000000000000000000000000000000..aed0264cfd48172379bc404490b5f9db9795e8ad --- /dev/null +++ b/lab-6/music-store-app/views/album.php @@ -0,0 +1,63 @@ +<?php + +// check if upc parameter set in query string +if (!isset($_GET['upc'])) { + // it is not set, so don't run the script + $content .= "<p>I don't know what album you're looking for...</p>"; +} +else { + // continue running the script... + + // set a variable to store Album.upc value + $upc = $_GET['upc']; + + // define the SQL query to run (from lab 3 queries.sql!) + // use column aliases if necessary to make referencing fields in result-set easier + $sql = "SELECT t.name AS track, t.track_number, a.title AS album + FROM Track t + INNER JOIN Album a + ON t.album_upc=a.upc + WHERE a.upc='$upc' + ORDER BY track_number ASC"; + + // query the database + $result = mysqli_query($link, $sql); + + // get number of rows in result-set + $row_cnt = mysqli_num_rows($result); + + // check if there are rows to display... + if ($row_cnt == 0) { + // if not, output a suitable message + $content .= "<p>The album you requested was not found!</p>"; + } else { + // otherwise, update the HTML in $content + + // get a single row from the result-set as an enumerated array + $row = mysqli_fetch_row($result); + // reference the first item in the array by index + $content .= "<h1>".$row[0]."</h1>"; + $content .= "<table cellpadding='4' border='1'> + <thead align='left'> + <tr> + <th>Track name</th> + <th>Track number</th> + </tr> + </thead> + <tbody>"; + // while there are rows, fetch each row as an associative array + while ($row = mysqli_fetch_assoc($result)) { + // append the content with more HTML containing row data + $content .= "<tr> + <td>".$row['track']."</td> + <td>".$row['track_number']."</td> + </tr>"; // reference a field value in the array by its key! + } + // free result set + mysqli_free_result($result); + + $content .= "</tbody></table>"; + } +} // end if/else + +?> diff --git a/lab-6/music-store-app/views/albums.php b/lab-6/music-store-app/views/albums.php new file mode 100644 index 0000000000000000000000000000000000000000..86ddce142038d09dd2cc393504cf2f9454d74864 --- /dev/null +++ b/lab-6/music-store-app/views/albums.php @@ -0,0 +1,57 @@ +<?php + +// define the SQL query to run (from lab 3 queries.sql!) +// use column aliases if necessary to make referencing fields in result-set easier +$sql = "SELECT Album.title, Album.price, Album.upc, Artist.first_name, Artist.last_name, Genre.name AS genre, (SELECT COUNT(*) FROM Track WHERE Album.upc=Track.album_upc) AS num_tracks +FROM Album +INNER JOIN Artist + ON Album.artist_id=Artist.id +INNER JOIN Genre + ON Album.genre_id=Genre.id +ORDER BY title ASC;"; + +// query the database +$result = mysqli_query($link, $sql); + +// update HTML content string with page title +$content .= "<h1>Albums</h1>"; + +// get number of rows in result-set +$row_cnt = mysqli_num_rows($result); + +// check if there are rows to display... +if ($row_cnt == 0) { + // if not, output a suitable message + $content .= "<p>There are no albums in the databased!</p>"; +} else { + // otherwise, update the HTML in $content + + $content .= "<table cellpadding='4' border='1'> + <thead align='left'> + <tr> + <th>Album name</th> + <th>Artist name</th> + <th>Price</th> + <th>Genre</th> + <th>Number of tracks</th> + </tr> + </thead> + <tbody>"; + // while there are rows, fetch each row as an associative array + while ($row = mysqli_fetch_assoc($result)) { + // append the content with more HTML containing row data + $content .= "<tr> + <td><a href='?page=album&upc=".$row['upc']."'>".$row['title']."</a></td> + <td>".$row['first_name']." ".$row['last_name']."</td> + <td>£".$row['price']."</td> + <td>".$row['genre']."</td> + <td>".$row['num_tracks']."</td> + </tr>"; // reference a field value in the array by its key! + } + // free result set + mysqli_free_result($result); + + $content .= "</tbody></table>"; +} + +?> diff --git a/lab-6/music-store-app/views/home.php b/lab-6/music-store-app/views/home.php new file mode 100644 index 0000000000000000000000000000000000000000..1164f129fa509a205eab6e6581b5314181f26890 --- /dev/null +++ b/lab-6/music-store-app/views/home.php @@ -0,0 +1,6 @@ +<?php + +$content .= "<h1>Welcome to the Music Store</h1>"; +$content .= "<p>Some content here...</p>"; + +?>