This is where ht://Dig's templates are put to work. First, let's count the number of lines returned by the search:
The first two lines are always going to be the HTTP header
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:
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
We know to look for "NOMATCH" because that's the string in results-nomatch.html). Similarly, we can check for a boolean syntax error:
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)
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
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: Conclusion and Advanced Topics 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:
Look for more tutorials later on these and other topics. But until then, happy searching!
blog comments powered by Disqus |