Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Ifrah Shahid
lab-exercises
Commits
ae6c9c82
Commit
ae6c9c82
authored
Nov 19, 2016
by
Sorrel Harriet
Browse files
updating album.php to remove logical row count error
parent
203a8632
Changes
2
Hide whitespace changes
Inline
Side-by-side
lab-6/music-store-app/views/album.php
View file @
ae6c9c82
...
...
@@ -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
);
...
...
lab-7/music-store-app/views/album.php
0 → 100644
View file @
ae6c9c82
<?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
?>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment