Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Now that we have specified the item we want to use and grabbed the details for that item, we're ready to display those details on our page using the array keys.
Update Link to details.php
We need to update our functions.php file to replace the "#" with a link to details.php. We'll need to also include our item id in the query string. Example: "details.php?id=101"
The function we need to update is get_item_html
function get_item_html($id,$item) { $output = "<li><a href='details.php?id=" . $id . "'><img src='" . $item["img"] . "' alt='" . $item["title"] . "' />" . "<p>View Details</p>" . "</a></li>"; return $output; }
Let's create a new div with
the class="media-details".
0:04
First, we'll put the title
within the h1 tags.
0:14
Next, we'll create a table
with the item details.
0:28
We'll start by creating a row for
category.
0:37
Table header is category.
0:44
Now, we'll pull our echo
0:53
$item["category"].
0:58
Now, let's copy and
past this row three times.
1:03
We'll change this for Genre.
1:10
Format.
1:15
And Year.
1:21
These are the shared attributes for
all the items in the catalog.
1:26
Each of our categories also
has some specific attributes.
1:30
We'll use a conditional
to check our category and
1:34
display the attributes
associated with each category.
1:36
Once again, we'll use our string to lower
function to make our categories match.
1:39
If the item category ==" books",
we'll want to show the authors,
1:55
publishers and ISBN.
1:59
Let's close off our PHP and
we'll open it around our end tag.
2:03
Let's copy our category row and
paste it three times.
2:10
Now we'll switch out Authors,
2:22
Publisher.
2:30
ISBN.
2:36
Let's preview the page now.
2:41
You'll see that we have
most of our details, but
2:42
we're getting a notice array to string
conversion, instead of our authors.
2:45
Authors are another array
nested within our item.
2:51
What we want to do is print out a string
of each of the authors separated
2:54
by a comma.
2:57
We could do this with a for each
loop like we've done before.
2:58
However, we don't want to add anything
to the beginning or end of our string,
3:01
just in between the elements.
3:05
Luckily for us, PHP has another function
that will do just that, implode.
3:08
Implode takes two parameters and
joins the array pieces into a string.
3:13
The first parameter is the string you
want to use to separate the elements and
3:18
the second parameter is the array itself.
3:22
The string separator can be anything or
group of things such as HTML.
3:25
This can be really useful if you want
to make a list of items in HTML.
3:30
Let's take a look at this in our code.
3:34
In this case,
we want to echo out implode, a comma and
3:37
a space between each of
the authors in our array.
3:43
Now when we refresh the browser,
3:50
we see our list of authors
separated by a comma and a space.
3:53
We need to add a conditional for
our last two categories.
3:58
Let's clean up this if line.
4:02
And copy it and paste it right
before our closing statement.
4:09
We'll add a closing curly braces and
then an else.
4:17
If our category item equals movies.
4:22
We're gonna copy one line of Publisher and
4:28
change it to Director.
4:34
And then we're going to copy our implode.
4:40
And paste it two times for
4:48
Writers And Stars.
4:51
Finally, we'll copy and paste this
else if statement one more time.
5:01
And change it to music.
5:11
We can copy Director and
paste this row and change it to Artist.
5:15
Now we're ready to preview our page again.
5:28
Nothing has changed for our book.
5:31
But if we change our ID from 101 to 201,
5:33
we see Forrest Gump with our director,
writers and stars.
5:39
If we change to 301, we see our music of
5:46
Beethoven with our artist,
Ludwig van Beethoven.
5:49
You need to sign up for Treehouse in order to download course files.
Sign up