Commit ae6c9c82 authored by Sorrel Harriet's avatar Sorrel Harriet
Browse files

updating album.php to remove logical row count error

parent 203a8632
......@@ -33,11 +33,14 @@ else {
} 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'>
// define a row counter
$i = 0;
// while there are rows, fetch each row as an associative array
while ($row = mysqli_fetch_assoc($result)) {
// if first row, output header and start of table
if ($i == 0) {
$content .= "<h1>".$row['album']."</h1>";
$content .= "<table cellpadding='4' border='1'>
<thead align='left'>
<tr>
<th>Track name</th>
......@@ -45,13 +48,15 @@ else {
</tr>
</thead>
<tbody>";
// while there are rows, fetch each row as an associative array
while ($row = mysqli_fetch_assoc($result)) {
$content .= "<p>The album has $row_cnt tracks.</p>";
}
// 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!
// increment row counter
$i++;
}
// free result set
mysqli_free_result($result);
......
<?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 either not found or has no tracks!</p>";
} else {
// otherwise, update the HTML in $content
// while there are rows, fetch each row as an associative array
// define a row counter
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
// if first row, output header and start of table
if ($i == 0) {
$content .= "<h1>".$row['album']."</h1>";
$content .= "<table cellpadding='4' border='1'>
<thead align='left'>
<tr>
<th>Track name</th>
<th>Track number</th>
</tr>
</thead>
<tbody>";
$content .= "<p>The album has $row_cnt tracks.</p>";
}
// 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!
// increment row counter
$i++;
}
// free result set
mysqli_free_result($result);
$content .= "</tbody></table>";
}
} // end if/else
?>
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