This is where ht://Dig's templates are put to work. First, let's count the number of lines returned by the search:
$rc = count($result);
The first two lines are always going to be the HTTP header
Content-type: text/html \n
so we can skip them (remember, ht://Dig was designed to output HTML).
If the search produced an error, we'd get no results or a one-line error, so:
echo "There was an error executing this query. Please try later.\n";
Let's also check to see that we got some matches. If we didn't, then ht://Dig will just echo out the two-line HTTP header, then the contents of the results-nomatch.html file. We use that information to perform the check:
echo "There were no matches for <B>$search</B> found on the website.<P>\n";
We know to look for "NOMATCH" because that's the string in results-nomatch.html).
Similarly, we can check for a boolean syntax error:
echo "There is a syntax error in your search for <B>$search</B>:<BR>";
echo "<PRE>" . $result . "</PRE>\n";
If none of the above conditions were true, then we have at least one match! In this case, ht://Dig first outputs the variables in the results-header.html template, so:
I'm echoing out the variable words here, instead of $search. That's because if I have fuzzy searching turned on and I search for "play", then $search will equal "play", but $words will equal "(play or played or playing or player or plays or players)" ... which what you are really searching for.
Now we echo out each of the matches.
(6 is the number of variables in results-header plus 2)
# grab the match information
$title = $result[$i];
$url = $result[$i+1];
$percent = $result[$i+2];
$excerpt = $result[$i+3];
# output the match information
echo "<A HREF=\"" . $url . "\">" . $title .
echo "(" . $percent . "% match)<BR>\n";
echo "<blockquote>" . $excerpt . "</blockquote><BR><BR>\n";
# move to the next match
$i = $i + 4;
And that's it!
There are a few quirks to keep in mind. Notably, ht://Dig outputs an additional new-line after the $(STARSLEFT) and $(STARSRIGHT) variables in the template. You need to keep this in mind when figuring out which line of $result corresponds to what piece of information. For example, if I changed $(PERCENT) to $(STARSLEFT) in my .conf file, I would need to make the following changes to the code:
But the basic strategy is always the same: PHP loops through the array $result and outputs the information. If you want to see the contents of $result for yourself (to make sure you're getting the right results), just replace the entire last else- block above with:
You should all be running out now and adding "Search this Site" buttons to your PHP-driven web pages. This tutorial covered the basics, but there are a lot of advanced things you can do with ht://Dig and PHP:
show matches in groups (e.g. "Matches 1-10", "Matches 11-20")
"fix" the URL for matches for sites that are in framesets
index and search not only HTML and PHP pages, but also PDF (Acrobat) and Microsoft Word documents using external parsers
use the raw results of the indexing process to generate a site-map
Look for more tutorials later on these and other topics.