Commit 8b187eaa authored by Sorrel Harriet's avatar Sorrel Harriet
Browse files

adding updated music-store-app code

parent cd6b3d92
<?php
// define a connection 'handle'
$link = mysqli_connect(
'localhost',
'sorrel',
'password123',
'musicstore'
);
// check connection succeeded
if (mysqli_connect_errno()) {
exit(mysqli_connect_error());
}
?>
<?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";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'/>
<title>Music Store: {{ page }}</title>
</head>
<body>
<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>
<?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
?>
<?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>&pound;".$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>";
}
?>
<?php
$content .= "<h1>Welcome to the Music Store</h1>";
$content .= "<p>Some content here...</p>";
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment