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
Our catalog page needs to display different categories depending on what the user selects. In this video, we’ll start talking about some special server variables and conditionals necessary to switch categories.
Update to html
index.php unordered list for our random items should have a class of "items".
<ul class="items">
-
0:00
Right now all our media categories link to the same catalog.php page.
-
0:05
We want to be able to specify which category the catalog should display.
-
0:11
We can accomplish that by adding a string at the end of the URL.
-
0:14
After the file name, type a question mark, and a variable with any name.
-
0:18
Let's call it cat, short for category.
-
0:21
We use an equal sign, and then specify a value of books.
-
0:26
Variables added to the URL in this manner get sent to the server,
-
0:30
along with the request for the webpage.
-
0:32
They are available in a special php variable called _GET,
-
0:36
all uppercase, and our element cat in square brackets.
-
0:42
The element cat is the same name we gave our variable in the URL.
-
0:46
We can use this value in a conditional to check which category should be displayed.
-
0:52
There are four options for this page.
-
0:54
Our three categories, books, movies, and music, and the full catalog.
-
0:59
We want to check if the get variable cat
-
1:02
is set to one of these three specific categories.
-
1:05
And if not, show the full catalog.
-
1:08
Now it is possible to type anything after cat equals in our address bar, so
-
1:13
we want to make sure that we are only looking for one of the 3 categories.
-
1:18
We're going to edit our catalog.php file.
-
1:21
The page title with the value Full Catalog will be our default.
-
1:26
A default is used if none of the conditions are met.
-
1:28
In other words, if there's no variable set for the cat variable in the query string,
-
1:34
we'll display the full catalog, all the books, movies, and music in the library.
-
1:39
So let's write a conditional right after our page title that checks the value of
-
1:43
the get variable cat.
-
1:47
If, get, cat,
-
1:52
double equals, books.
-
1:58
We want to set our pageTitle equal "books".
-
2:05
To check for our other two categories we can add an else statement with another if.
-
2:09
Else, if, get cat
-
2:13
double equals movies
-
2:25
We'll set our $pageTtitle = "Movies".
-
2:33
Finally, else if
-
2:37
($_GET["cat == music.
-
2:45
We'll set our page title = music.
-
2:53
Close out our conditional.
-
2:55
To make this a little easier to see when we click the links,
-
2:57
we're going to set the H1 tag to use our page title as well.
-
3:00
Open php echo $pageTitle.
-
3:06
Close php and save.
-
3:09
Now, we need to change your navigation to use these values.
-
3:12
Open header.php and add our query string after catalog
-
3:18
?cat=books and for
-
3:23
movies we'll do ?cat=movies.
-
3:28
For music, ?cat=music.
-
3:35
Again, we can see that having an include file makes it so
-
3:38
much easier to make changes to our header.
-
3:40
Now when we go back into the browser.
-
3:43
And we refresh our page.
-
3:45
Our navigation will pass the correct variables.
-
3:49
Books, movies, music.
-
3:52
And the title is displayed on the page based on what category we're in.
-
3:57
One last thing we should do in this conditional.
-
3:59
We're currently checking the value of the get variable cat here.
-
4:02
But, if the variable doesn't exist at all,
-
4:05
then this conditional would generate a notice.
-
4:07
Let me show you.
-
4:09
Let's remove the cat equals music.
-
4:14
Yuck.
-
4:15
Not something we wanna see.
-
4:16
Let's fix that.
-
4:18
Let's go back into work spaces, and our catalog file.
-
4:22
Before this conditional, we need to do another check.
-
4:26
We're going to use php's isset function.
-
4:29
If (isset ($_GET["cat"]
-
4:42
Then move on to execute.
-
4:43
Is conditional.
-
4:49
Now when we save this file and go back into our browser,
-
4:53
we refresh the page and we see our full catalog, books, movies, and music.
-
5:01
We still need to actually display the full catalog or
-
5:03
the categories which we'll do a little later on.
-
5:06
We have everything in place to choose the category we want to show.
You need to sign up for Treehouse in order to download course files.
Sign up