This is where ht://Dig's templates are put to work. First, let's count the number of lines returned by the search:
<?php
$rc = count($result);
?>
The first two lines are always going to be the HTTP header
Content-type: text/html \n
\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:
<?php
if ($rc<3):
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:
<?php
elseif ($result[2]=="NOMATCH"):
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:
<?php
elseif ($result[2]=="SYNTAXERROR"):
echo "There is a syntax error in your search for <B>$search</B>:<BR>";
echo "<PRE>" . $result[3] . "</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.
<?php
$i=6;
?>
(6 is the number of variables in results-header plus 2)
<?php
while($i<$rc) {
# 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 .
"</A><BR>\n";
echo "(" . $percent . "% match)<BR>\n";
echo "<blockquote>" . $excerpt . "</blockquote><BR><BR>\n";
# move to the next match
$i = $i + 4;
}
endif;
?>
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.