PHP
  Home arrow PHP arrow Page 6 - Search This!
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Search This!
By: Colin Viebrock
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 24
    1999-03-15


    Table of Contents:
  • Search This!
  • Configuring ht://Dig
  • Indexing the Site
  • Building the Search Page
  • Performing the Search
  • Displaying the Results

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Search This! - Displaying the Results
    ( Page 6 of 6 )

    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:


    <?php else: $matches = $result[2]; $firstdisplayed = $result[3]; $lastdisplayed = $result[4]; $words = $result[5]; echo "Your search for <B>$words</B> returned <B>$matches</B> match"; echo ($matches==1) ? "" : "es"; ?>

    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:


    <?php $title = $result[$i]; $url = $result[$i+1]; $stars = $result[$i+2]; $excerpt = $result[$i+4]; ... $i=$i+5; ?>

    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:


    <?php while (list($k,$v)=each($result)) { echo "$k -> $v \n"; } ?>
    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:

    • 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.

    But until then, happy searching!



     
     
    >>> More PHP Articles          >>> More By Colin Viebrock
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT