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