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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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: 4 stars4 stars4 stars4 stars4 stars / 21
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway