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
We need to handle an empty result list, when there are no items that match the search term. We also want them to know we are on the search results page as well as the search term they used. Let’s take a look at the finished results.
Example Code
if ($search != null) {
echo "Search Results for \"".htmlspecialchars($search)."\"";
}
if ($total_items < 1) {
echo "<p>No items were found matching that search term.</p>";
echo "<p>Search Again or "
. "<a href=\"catalog.php\">Browse the Full Catalog</a></p>";
}
-
0:00
In the last video we got our search form mostly working.
-
0:04
We just need to handle an empty results list.
-
0:06
When there are no items that match our search term.
-
0:09
We also want them to know when we are on a search results page,
-
0:13
as well as what the search term was.
-
0:16
Let's take a look at the finished results.
-
0:19
If I search for Alina, we'll see the page title
-
0:22
showing that we are on the search page and our search term Alena.
-
0:27
We also see that our search term did not return any results.
-
0:31
Let's add these messages back in work spaces.
-
0:33
Back in catalog.php let's scroll down to the h one header.
-
0:42
We're going to add another conditional.
-
0:44
We can check the same way that we did for the section.
-
0:47
If search is not equal to null.
-
0:53
Then we want to display a different header.
-
1:04
Search results for and our search term.
-
1:07
Since we're displaying user input as output,
-
1:10
let's filter that output with the built in function htmlspecialchars.
-
1:22
Now, let's add quotes around the search term.
-
1:26
We can't just add another double quote, so
-
1:28
we use our escape sequence of slash and then a quote.
-
1:32
Let's also add this to the end.
-
1:39
Now we add the else block,
-
1:40
containing our existing title including the other if-statement.
-
1:57
We have our header changed.
-
1:59
Now let's add a message if there are no search results.
-
2:02
After closing the H1 tag we're going to create another conditional.
-
2:06
This time we'll use an if total items is less than one.
-
2:20
If our total items is less than 1 then we're going to add a message.
-
2:24
But let's finish off our else block first.
-
2:27
After a message we're going to add an else that contains the resulting items as well
-
2:31
as our pagination.
-
2:42
Make sure you close this else block.
-
2:45
Now we're ready to go back up and add our message.
-
2:53
No items were found.
-
3:02
We'll also add another paragraph asking them to search again or
-
3:06
browse the full catalog.
-
3:14
Let's add our link to browse.
-
3:38
There's one more thing we need to do to make this work properly and
-
3:41
to clean things up a little more.
-
3:43
If our total items are less than 1, we really don't need to do any more
-
3:47
calculations before we grab the catalog results.
-
3:50
We also don't want to redirect to the full catalog.
-
3:54
So let's put all that within another conditional.
-
4:06
If our total items are greater than 0, then we can add these calculations.
-
4:22
We'll put all this within our if statement.
-
4:25
So if our total items are less than 1, we'll skip over this entire block.
-
4:30
We do need to add a couple default for total pages and offset.
-
4:36
We'll add them right after total items.
-
4:49
We can also move our pagination up here as well.
-
5:08
Great, let's take a look at this in the browser again.
-
5:12
Now when we do a search We see our search results,
-
5:17
and when we search for something that doesn't exist we see our message that no
-
5:22
items were found matching that search term.
-
5:26
The last thing we need to do is integrate the search feature
-
5:29
with the pagination feature.
-
5:30
We'll tackle that next.
You need to sign up for Treehouse in order to download course files.
Sign up