Commit c5f052c4 authored by Sorrel Harriet's avatar Sorrel Harriet

making a single artist view navigable from record view

parent e6c86ecb
......@@ -2,7 +2,6 @@
<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=artist" title="artists">Artist</a></li>
<li><a href="?page=orders" title="orders">Orders</a></li>
</ul>
</nav>
<?php
// create variable for content HTML
$content = "<h1>Bob Marley Records</h1>";
// fetch record titles by artists with last name Marley
$sql = "SELECT title, year, price FROM record
INNER JOIN artist ON artist.id=record.artist_id
WHERE artist.last_name='Marley'
ORDER BY year ASC";
$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['title']."</td>";
$content .= "<td>".$row['year']."</td>";
$content .= "<td>&pound;".$row['price']."</td>";
$content .= "</tr>";
}
$content .= "</tbody></table>";
// free result set
mysqli_free_result($result);
// 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
......
......@@ -4,11 +4,11 @@
$content = "<h1>Records</h1>";
// fetch records as a result set
$sql = "SELECT r.title, a.first_name, a.last_name, r.genre, r.price
$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 a.id";
ORDER BY r.title, r.price DESC";
$result = mysqli_query($link, $sql);
// check query returned a result
......@@ -20,7 +20,7 @@ if ($result === false) {
while ($row = mysqli_fetch_assoc($result)) {
$content .= "<tr>";
$content .= "<td>".$row['title']."</td>";
$content .= "<td>".$row['first_name']." ".$row['last_name']."</td>";
$content .= "<td><a href='?page=artist&id=".$row['id']."'>".$row['first_name']." ".$row['last_name']."</a></td>";
$content .= "<td>".$row['genre']."</td>";
$content .= "<td>".$row['price']."</td>";
$content .= "</tr>";
......
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