This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Let's wrap up our project by cleaning up or user experience a bit.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Let's go back to our films.php file, and
then how can we tidy this up.
0:00
Well, we're doing a try-catch here that's
good, but
0:05
what we're not checking is that what do we
do if the ID is not set,
0:07
and then if it's not set why would we
wanna run this code here?
0:14
So actually let's just get rid of all of
this code that's in here, and
0:19
put it inside of our if statement.
0:24
We're actually gonna grab all the way
through to line 19 cuz
0:26
there's certainly no point of us doing this
if we don't have it.
0:29
So we'll go ahead and hit paste in here to
drop it in and
0:33
I'm gonna do a quick tidy up.
0:37
[BLANK_AUDIO]
0:39
Okay, so, at this point right now if we
look at this code,
0:45
it says if we have an ID, if it's there,
it's not empty,
0:49
then we'll go ahead and set our, film ID,
make sure it's an integer value.
0:53
And then we'll run a query on our database
by going through the prepare statement,
0:59
the bind parameter statement and execute
so on so forth.
1:04
But what we're concerned with is if
nothing is returned if it
1:07
doesn't actually have anything in it
whatsoever, or if the ID is empty.
1:10
So if the ID is not set then we don't
wanna run the code, so that's what this is
1:16
covering but now we wanna make sure if we
don't actually have a film.
1:21
So let's go down here to the bottom and
1:25
see that if we don't have a film it's not
gonna echo any kind of title back to us.
1:27
So let's put an if statement down here to
test whether or
1:32
not that film has anything in it.
1:36
Let's do that by making this php tag a
little more encompassing.
1:40
We'll actually get rid of the h2 on either
side of it.
1:44
And then bring it down a few lines so we
can just concentrate on the code.
1:49
Okay.
1:55
So now here at the bottom of our file
we're gonna echo film title.
1:56
Well, we wanna make sure that there
actually is a film title.
2:01
so, if there isn't a film title or there's
no film at all, then we wanna return
2:04
a message that say, sorry there's no film
could be found with the specified ID.
2:08
The way that we can do that is to just
check if the film object itself, or
2:13
the film variable
2:17
is set, it actually has something in
it.
2:19
We can do that with the isset function of
php, so if I actually type php isset,
2:22
which is all one word, here's the manual
on php.net.
2:29
And then we're looking to see if a
variable is set and not null, or
2:34
basically not empty.
2:38
All right, so close that out and then
we'll go back to our films, and
2:40
then we'll say if open up our parens, and
then open up our curly braces.
2:44
We'll close out our curly braces, and
then, what are we testing?
2:50
We're testing that isset.
2:54
Open and close parens again.
2:58
And then do $film.
3:00
Okay, so we wanna make sure that it's
actually set and
3:02
it's gonna return something to us.
3:05
If it's empty, we wanna do something else.
3:07
Let's make sure that it's working before
we test our actual error case.
3:10
So here we'll go back and do, number nine
again, hit enter.
3:14
Okay, that's working.
3:19
It's still returning it to us, but now
it's this normal text.
3:20
That's because we lost our h2 that was
here before.
3:23
What we're gonna want to, the h2 for
3:27
the film title or our statement that says
we can't find a film.
3:29
So I'm going to put that outside here.
3:33
Okay.
And make sure we close it
3:36
out after our php tags are closed.
3:38
[BLANK_AUDIO]
3:40
All right.
There's our h2.
3:44
Save it, let's refresh our page here.
3:46
Okay.
Much better.
3:49
That's back to the h2 that we were
expecting.
3:50
And, now, we'll do our else statement.
3:53
So, else, what do we wanna do,
3:57
we want to return some kind of a message
that says, sorry it cannot be found.
3:59
So we'll do an echo.
4:03
[BLANK_AUDIO]
4:04
Add our quotes, close it out with a
semicolon, and
4:08
then whatever we wanna return to our user
in the h2.
4:11
So we'll say, sorry, comma, no film was
found
4:15
with the provided ID.
4:22
Yeah, that's good enough for now.
4:28
All right, let's save that and refresh our
page.
4:30
Okay, still works, but
4:33
if we go with a number that's outside of
this, still not returning what we need.
4:35
Okay.
4:39
So film must be set so where are we gonna
need to modify our code?
4:42
Let's see what film looks like here.
4:47
If we go up to line 16, clearly film is
set after our results test is run.
4:49
All right, so we'll go here and let's just
do a var_dump.
4:57
Some rudimentary debugging here for you.
5:04
$film, hit the semicolon,
5:06
save it and go back up and hit refresh.
5:11
Okay, all is returning to us, a boolean
false.
5:15
Okay, so false is technically set.
5:18
It's a boolean, it's not considered empty.
5:22
So the isset will not work for us.
5:24
So that code really isn't what we want.
5:28
Okay, so let's switch back over to our
code,
5:30
we don't need this if statement down here
for this particular instance.
5:32
We'll just leave it in we're gonna need an
if l statement I'm sure later.
5:38
But we're not gonna need this particular
message.
5:41
So I'll just remove the else for now and
leave this in place.
5:43
Then we'll go up here and
5:48
this is where were going to want to test
whether or not film is what we expect.
5:50
So right here under line sixteen we'll
create a new line and
5:56
we're gonna do another test or another
conditional.
6:00
Now the conditional that we're testing
here is that we've taken the ID.
6:02
We've passed it through.
6:07
We've, you know, requested it from the
database.
6:08
It's returning to us a false because
nothing was found.
6:11
We want to test to see whether or not film
is false, so we can return a message.
6:15
So we'll do if, and then we'll do $film
equals,
6:21
equals, capital FALSE here for our
keyword.
6:26
Okay.
And then if it is false,
6:30
then we wanna return a message that says
the film could not be found.
6:34
Now we can do that by actually using the
code that already exists here.
6:38
It's in an H2.
6:43
It's film, film title.
6:44
So we can just set our film title if we
want, or
6:46
we could return a message and die.
6:49
Let's just return a message and then kill
the process for now.
6:51
So we will echo and then we're gonna use
what we previously put down there before.
6:55
So, sorry a film could not be
7:01
found with the provided ID.
7:07
All right.
7:13
Now, let's see if this works as we expect,
so
7:14
we'll go back over to our page here, after
saving.
7:16
And we'll hit refresh.
7:20
That says, sorry, a film could not be
found with the provided ID.That's good,
7:21
except we wanna kill the process at the
end by typing, die.
7:25
Okay, so we'll hit refresh on this, and
now it says sorry, a film could not be
7:30
found with the provided ID, and then we go
back here and hit number nine like we
7:35
had before, and now we're back to ALABAMA
DEVIL which is ID nine.
7:39
Okay, so just as a review here, we have
gone through,
7:45
we've got our binded and prepared
parameters.
7:49
We execute our statement, and then we go
down here and
7:52
make sure that the film is actually there,
if something actually exists.
7:56
And if it doesn't, then we return a
message to kill the process.
8:01
We could definitely do more there,
8:04
a little bit more user interface friendly
error messages.
8:05
But for now, this is great as an example
for you.
8:09
Now the workspace here that's set up for
8:13
you has the database, it has all these
tables.
8:14
Just as an example here we'll do one last
little thing.
8:17
Instead of it saying if it is set film,
8:20
I just want to change the code just here
for a second.
8:24
And instead of echoing it out, I'll
actually continue to echo it out, but
8:28
afterwards, I'm gonna do a print_r, and
8:33
pass through the actual film object, or
the film array variable.
8:35
[BLANK_AUDIO]
8:40
Let's save this, and go back and hit
refresh.
8:43
Okay, if you see here, we have our array,
film ID,
8:47
the description, all the information about
the film.
8:51
We're really, we're only needing the film
ID.
8:56
So something I say that I would do to
continue learning on this,
8:59
would be to take what's existing now and,
and build a little something out.
9:04
Maybe run your query so it's just getting
the title and maybe just the ID or
9:10
just a few other small items instead of
everything about the film.
9:15
Perhaps on our main page on our index
page, instead of doing everything here,
9:19
enlisting a thousand of them, maybe try to
build in some pagination.
9:26
Do, you know, 10 per page or 20 per page.
9:30
Feel free to play around with it.
9:33
The workspace is yours to use and enjoy.
9:34
I hope you've enjoyed the course.
9:36
Now you have a good base understanding of
how to work with a database in php
9:39
using php data objects.
9:43
Practice is very important to learning
more about how we work with databases, and
9:45
what else is possible with PDO.
9:49
That said, I would suggest you take a look
at the notes for
9:52
this course, and read all the
documentation thoroughly.
9:54
Take some time to use work spaces, and
practice with the example database, and
9:58
perhaps look at some open source php
projects.
10:02
To see how they handle working with
databases.
10:05
The next logical step after understanding
how PDO works, is to
10:07
look into further database abstraction
with ORMs, or Object Relational Mappers.
10:11
Such as Doctrine, which you can find at
doctrine-project.org.
10:16
See you next time, and remember to work
hard, and have a little joy.
10:22
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up