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