Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We will use a multidimensional associative array to store the information for our Media Library Catalog. In this video, we’ll add a second dimension to our current array.
DOWNLOAD
Catalog data.txt
-
0:00
We left our catalog page looking like this with the bulleted list of four items.
-
0:05
In the code for
-
0:05
the catalog.php file, we had our catalog stored in a simple indexed array.
-
0:10
Instead of letting PHP assign the key,
-
0:12
we have specified our own item ID for the key.
-
0:16
Each element in the main catalog array represents one item.
-
0:20
Right now each element has a piece of text with a title of the item as its value.
-
0:25
Let's change this first element making the value another array
-
0:30
instead of just a piece of text.
-
0:32
Let's go back into our catalog.
-
0:35
Before we add anymore information to our array here,
-
0:38
let's separate out this code into a new include file.
-
0:41
We'll name this file data.php.
-
0:46
Let's copy our array from catalog.php,
-
0:51
and paste it into data.php.
-
0:55
Add our opening php tag, and the closing php tag.
-
1:03
Now we're ready to add a new dimension to our array.
-
1:05
I'm going to show you another shortcut for adding an array.
-
1:09
Instead of using the keyword array with parentheses,
-
1:12
we can simply add open and closed square brackets.
-
1:16
We'll use this as we add another array to the first element
-
1:19
in our main catalog array.
-
1:22
I'll refer to this as our interior array,
-
1:24
because it's inside our main catalog array.
-
1:27
This interior array will contain information about this one item.
-
1:32
One element for each piece of information.
-
1:35
The first element will contain the title.
-
1:38
We'll specify the key for each element here using a piece of text to describe
-
1:43
what information the element contains.
-
1:46
Title and then the = sign, followed by the > sign.
-
1:53
Then we also have the value in quotation marks, because it too, is a piece of text.
-
1:58
We need to add another element to this interior array, for
-
2:01
all the information about this item.
-
2:03
We separate items in array with a comma.
-
2:07
Then we can create a new element for each of the shared attributes of this item.
-
2:11
The next element we'll add is the path to the image.
-
2:15
Call it Image=>, our path
-
2:21
is img/media/design_patterns.jpg.
-
2:31
Then we'll add the genre.
-
2:36
Tech.
-
2:39
The format.
-
2:43
Paperback.
-
2:47
And the year.
-
2:52
Since this value is a number, it does not need quotation marks.
-
2:57
Finally, let's specify the category.
-
3:02
This will match up with one of the navigation headings, books,
-
3:05
movies, or music.
-
3:07
This one's Books.
-
3:10
Each category will have some extra details that are specific to the item category.
-
3:14
Design Patterns is a book, so now we need to add the extra book elements.
-
3:19
To start with, we'll add ISBN.
-
3:25
Then we'll add publisher.
-
3:31
And finally, authors.
-
3:33
Although many books only have one author, some books,
-
3:36
including this one, have multiple authors.
-
3:39
Instead of making authors a string like our elements,
-
3:42
we'll make authors another array.
-
3:46
And just like that, we have a third dimension to our associated array.
-
3:50
Let's add our authors.
-
3:59
And let's finish off our arrays.
-
4:01
We've changed the first element in our main catalog array from a simple piece of
-
4:05
text to a full associative array, this makes our item much more useful.
-
4:11
We now have attributes for each item grouped together.
-
4:14
This will allow us to display details of each item.
-
4:17
To get our catalog ready to use,
-
4:19
we'll need to add the details of each item in the array.
-
4:22
For movies and music, we'll have elements specific to those items.
-
4:27
You probably don't want to watch me type out all the code for all the other items
-
4:30
in the catalog, and you probably don't want to type them yourself.
-
4:34
So I've attached a text file to this video with the code you'll need.
-
4:38
I also added a few more items so we have four items in each category.
-
4:42
Download that text file and copy the code from that file and
-
4:46
paste it into our data.php file.
-
4:52
Also, when ending a php file with php code,
-
4:55
you do not need to use the php closing tag.
-
4:59
Leaving off the closing tag is a widely accepted coding practice
-
5:03
used by projects such as WordPress and Drupal.
-
5:06
In fact, the Zend Framework specifically forbids using it.
-
5:10
The closing tag is not required by PHP, and by omitting it at the end of a file,
-
5:16
you guarantee that no trailing whitespace can be added.
-
5:20
These might cause unexpected results.
-
5:22
Once you've pasted your code the full array should look like this.
You need to sign up for Treehouse in order to download course files.
Sign up