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
Our search functionality is working great. The only issue we’ll see is if our search results return more than 8 items. We only show the first 8 items, but our link to the second page does not keep our search term. We need to finish integrating our pagination with our search results.
Example Code
$pagination .= "s=".$search."&";
if (!empty($search)) {
$limit_results = "s=".urlencode(htmlspecialchars($search))."&";
} else
Additional Suggestions
- Display the title along with the thumbnail on the catalog listing pages.
- Search in more than just the title so someone could search by author or movie star.
- Add a dropdown of categories to the search form and allow users to narrow their search.
- Create advanced search feature such as the use of a wildcard within the search term.
- Allow the user to choose how many items they would like to have displayed on each page.
Our search functionality is working great.
0:00
A user can type any term they want
into the search bar on each page.
0:02
They will see a list of item results,
which they can click for more details.
0:06
If there are no results, we let them know.
0:11
And if they submit without a search term,
they will see the full catalog.
0:13
The only issue we'll see is if our search
results return more than eight items.
0:18
We only show the first eight items, but
0:23
are link to the second page
does not keep our search term.
0:25
We need to finish integrating our
pagination with our search results.
0:29
Back in catalog.php,
let's go to our pagination section.
0:33
We have a conditional that checks for
category.
0:38
Let's add a new conditional to check for
search.
0:41
Again, make sure we add else if for
our second condition.
0:55
If we do have a search term,
1:00
then we wanna add that to our URL just
like we've done with the category.
1:02
We'll add concatenation and
then our search.
1:11
We want to finish off using our ampersand,
1:19
because of the way our data is coming in
and the way we have filtered our data.
1:22
This should be all we need to do.
1:27
However, there are two functions
that we could run on this data
1:29
just to make sure that it's
formatted the way we want.
1:32
First, because this is user input, we want
to make sure that we escape the output.
1:36
We'll use our HTML special cares again.
1:42
Second, we want to make sure we're passing
this data in a way that the query string
1:51
can interpret properly.
1:55
The function for this is urlencode.
1:58
There's one more place where we
should update this just to be sure.
2:03
The redirects for our page numbers.
2:07
Let's copy this first if all
the way down to the else,
2:09
then we'll go up to our limit results.
2:14
We'll paste this first conditional in and
we'll change our spacing.
2:20
The only thing that we have to
change is the pagination variable.
2:27
Let's rename this $limit_results.
2:32
We can also remove the dot for
concatenation,
2:36
because we're replacing
the variable not adding to it.
2:39
Let's test this out in our browser.
2:43
We'll use something fairly common,
like the with a space.
2:46
We can see that there are two pages.
2:53
And when you click on the second page,
2:55
you can see that our search term has been
properly formatted with a plus at the end.
2:57
If we change the page number to 3,
3:03
you can see that the redirect,
redirects us properly to the last page.
3:07
You've done a fantastic job so far.
3:12
You've had opportunity to pull
results out of a database and
3:15
using a variety of methods.
3:18
You've learned about the importance
of refactoring to help maintain and
3:20
improve code.
3:24
You've added a pagination feature and
a search feature, and
3:25
our personal media library is
really starting to become useful.
3:28
There are still plenty of things that we
could do to improve this application.
3:33
So, why don't you give them a try?
3:38
If you'd like some suggestions, how about
trying any or all of the following?
3:40
Display the title along with the
thumbnails on the catalog listing pages.
3:45
Search and more than just the title,
so someone could search by author or
3:50
movie star.
3:54
Add a drop-down of categories
from the search form and
3:54
allow users to narrow their search.
3:58
Create advanced search features,
4:02
such as the use of wildcards
within the search term.
4:04
Allow the user to choose how many items
they would like to have displayed
4:08
on each page.
4:11
Don't forget to check out our community.
4:14
It's a great place to get help
if you have any questions.
4:15
You might even start contributing and
helping someone else on their journey.
4:19
You'll be surprised how much you
benefit when you get involved.
4:23
So, have fun and keep learning.
4:27
You need to sign up for Treehouse in order to download course files.
Sign up