Item Details and Redirection5:35 with Alena Holligan
We have our catalog and category pages with thumbnails of the items. We are going to add one more page to display the details for each item. We will grab the details of the specific item we choose. If we can't find the item specified, we redirect to the Catalog page.
Our library is really coming together, 0:00 we have all our data loaded and sorting on our catalog and category pages. 0:02 Currently all we see are the thumbnails of the items, 0:06 but we have a lot more data that we know about each item. 0:08 The last thing we need to do is to be able to display the details of each item, 0:12 let's add one more page. 0:17 Create a new page named details.php. 0:19 We want to make sure that this is right next to our other pages in the root of 0:24 our site. 0:28 Copy the entire top code block from php all they way down to including the header. 0:30 And paste it into our details page. 0:41 Now we'll make the changes specific to this page. 0:43 To pull the details for 0:46 a specific item, we're going to utilize the get variable again, to pass the ID. 0:47 Let's change the get variable in the first if statement. 0:52 Where it's looking to see if it is set get cat. 0:56 We're gonna change cat to id. 0:59 We're going to change what we're doing with the variable, so 1:03 let's remove this entire nested if statement. 1:05 If an ID was passed, we want to check if that ID is in our catalog. 1:09 So let's create a new ID variable. 1:13 We'll assign it the value of GET id. 1:18 Then we want to add another if is set to check for that ID within our catalog. 1:26 If isset catalog. 1:30 ID. 1:38 If we find an item, let's hit the value of that item to an item variable. 1:41 If we don't find an item or we aren't passing a variable, 1:50 then our item variable will not be set. 1:56 And we want to redirect them to a full catalog. 2:00 So down here we'll do another if isset item. 2:03 But we're going to change and use our negation operator. 2:11 So, if not isset item we're going to redirect. 2:17 The command for this is header. 2:22 This has nothing to do with our own header file that displays the logo and menu. 2:25 This is part of the HTTP, the hyper text transfer protocol that defines how 2:29 the server and the browser communicate before any HTML gets passed. 2:34 The header field we want to define is location. 2:38 This tells the browser to look for a different page, catalog.php. 2:45 We need to make sure we don't send anything else to the browser before 2:52 this redirect. 2:55 Even a blank line at the beginning of the file will cause the redirect to fail. 2:56 This is because a blank line, or a echo statement, will cause a header response 3:00 to be sent back to the requesting computer, nullifying the redirect header. 3:04 We also want to add an exit command. 3:09 This makes sure nothing else is processed while the redirect is happening. 3:13 The last thing we want to do in this top section of our file, 3:17 is to move the page title in the section below our redirect. 3:21 Then we know we can use the item values. 3:28 We'll change page title to item title. 3:30 We can leave section null since we're not using it in our navigation. 3:38 Now let's start adding the content for this page. 3:41 Start adding our divs. 3:46 Div class="section page". 3:48 Div class="wrapper". 3:57 Within the wrapper the first that we want to add is our image. 4:07 We're going to write the HTML for the image first. 4:10 Then add the PHP we need. 4:12 Image source and alt. 4:13 Now we'll fill in the PHP needed to grab the details of the item, php echo 4:22 $item["img"]. 4:27 Php echo item title. 4:38 Included in the CSS you've been given is a style for this image. 4:46 We need to surround it with the div tag with a class of media picture. 4:49 Media-picture. 4:59 We also want to include a span tag, to give it a nice border. 5:07 When we take a look at this page in the browser, let's pull up this browser 5:17 page details.phpid=101. 5:23 We see a nice image but what we really want are the details. 5:29 Now let's go back and add those. 5:32
You need to sign up for Treehouse in order to download course files.Sign up