Working With Results6:05 with Hampton Paulk
After successfully querying our database and taking a look at our PDO Statement results object, we still need to work with our results to get the properly formatted data to our users. Let's take a look at how to bend these results to our will.
After successfully querying our database and 0:00 taking a look at our PDO statement results object, we're still gonna need to 0:02 work with our results to get the properly formatted data to our users. 0:06 Let's take a look at how to bend these results to our will. 0:10 Let's build out and work with our results set, by making our 0:14 index page of films link off to a single film and 0:18 then we can just query the database for just one film by using say the film ID. 0:23 So we have a 1,000 results, let's go ahead and 0:29 start by cleaning up our index file and listing out our films and 0:32 populating the actual list with what we have here from our database. 0:37 So switch back over to our index.php file. 0:41 We don't need to do this inside of our try catch. 0:44 So, I'm actually gonna cut that out. 0:48 So, once we have our results set and 0:50 we've run the query we can actually just come down here and do what we need to do. 0:53 I don't wanna die anymore. 0:57 I actually don't need our pretags any longer. 0:59 Let's get rid of all those, and instead of dumping this out, 1:04 I actually wanna take my result set and store it to a variable. 1:08 The variable here that makes sense to me is filmsplural. 1:12 Okay so result set, fetchAll will store to a variable called films and 1:17 films will be that 1,000 item array from our result set or from our films database. 1:22 So going down, 1:31 we're gonna use some standard PHP at the very bottom of this file. 1:32 Here we have our ordered list. 1:36 So let's go ahead and open up some four each after we open up our PHP tags. 1:38 So we'll go ahead and open those now and get started. 1:42 PHP, all right I'm gonna close those guys out. 1:46 And then here we're going to do a four each, so four each. 1:49 And then films, as film. 1:55 [BLANK_AUDIO] 1:59 All right, we're going to open up and close our curly braces and 2:02 then here we're actually going to have one of these items. 2:06 So I'm going to cut one from here and paste it in. 2:09 All right, so in order to use this with our 2:13 looping PHP code, we're gonna need to modify this just a little bit. 2:20 So we'll start by adding our echo statement, and 2:26 then we'll surround this entire thing in single quotes for now. 2:30 Okay, and all that will do is just echo the string, 2:36 as it is right now, a thousand times. 2:39 Let's double check that that works as we expect. 2:41 All right, you have film 1 a lot of times. 2:45 We don't need that. 2:48 Let's go back and make this echo statement do what we want. 2:49 So, we're gonna use this same class, lens, except we wanna modify this spot 2:53 right here where it says Film One with our actual film title. 2:57 And we'll say use string concatenation with the dot symbol, and then do $film. 3:03 Kay, We gotta put some single quotes here to stop the actual string. 3:13 There we go. 3:18 Film, so our column name will be title. 3:19 So we'll open up some double quotes here, and use title, hit Save. 3:22 Kay now let's go back and 3:28 refresh and see if that gives us our film titles, and it does. 3:29 So here is the one through 1,000 of all of our films. 3:33 But it doesn't go anywhere. 3:39 So, we actually need to link this off somewhere. 3:40 So let's go back to our code and see how we're gonna do this. 3:42 So on the same line here as our actual list items. 3:47 We're gonna open up that anchor tag just after our little image, 3:53 our little video icon. 3:57 So we'll do an A, and then href equals, and 3:59 we'll use double quotes here, and then we'll say films.php. 4:03 Now that file doesn't exist yet, but it will. 4:10 And then we're going to pass through a git variable, an argument here, and 4:13 that's gonna be called ID. 4:17 We're gonna set ID equal to, and 4:18 we wanna set it equal to something that's going to be unique for the database, 4:21 and in our case, if we go back to the sakila manual, so we'll do sakila. 4:27 Let's see, it's right here in our history. 4:33 And then we're gonna look at our film table. 4:35 So in our film table, you'll see that the film ID is asserted primary key used 4:38 to uniquely identify each film in table. 4:43 It's exactly what we're looking for. 4:46 So we will use film_id and 4:48 since the column names correspond to the key of the array. 4:51 We should be able to use film_id directly in our code. 4:55 We'll do that by doing more string concatenation, so 5:00 gonna break this string with single quotes, 5:04 add more of our concatenation dots here, and then do $film. 5:06 Okay? And then open up for an array index. 5:12 And in here we're going to use, let's see, film_id. 5:15 Let's Save that, refresh our page, and see if it gives us what we expect. 5:22 Well they are all links, that's good. 5:28 And so see now, if you look at the link in the status bar at the bottom, 5:31 you should be able to see it says ID equal to and then there is an array index or 5:35 a film ID index equal to the ID. 5:40 So that's good. So when we click on one of these guys. 5:44 You'll see it's gonna give us a 404 Not Found. 5:47 So let's solve that now by creating the films.php. 5:49 I don't need this anymore, close it. 5:52 And now in here, right click, New File films. 5:56
You need to sign up for Treehouse in order to download course files.Sign up