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