JavaScript
  Home arrow JavaScript arrow Page 4 - Building the Server-side Component of a Se...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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? 
JAVASCRIPT

Building the Server-side Component of a Search Engine with AJAX
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2006-11-21

    Table of Contents:
  • Building the Server-side Component of a Search Engine with AJAX
  • The search engine's client module
  • Coding the search engine's server module
  • Putting the search engine to work

  • 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


    Building the Server-side Component of a Search Engine with AJAX - Putting the search engine to work


    (Page 4 of 4 )

    As I expressed in the section that you just read, implementing this AJAX-driven search engine is only a mater of coding a basic PHP script in the server, since all the hard work of doing searches is performed by AJAX.

    At this stage, I'm quite sure you're asking the following question: how does the application retrieve the corresponding results from the sample "pages" database table, in accordance with a specified search term? Well, fortunately the answer is rather simple if you take a look at the following PHP script, which not only searches in the mentioned database, but sends the returned results right back to the client.

    try{ //connect to MySQL   $db=new MySQL(array('host'=>'host','user'=>'user',
    'password'=>'password','database'=>'database'));    $searchterm=mysql_escape_string($_GET['searchterm']);    $result=$db->query("SELECT * FROM pages WHERE contents
    LIKE '%$searchterm%' ORDER BY id ASC");   echo '<h2>'.$result->countRows().' records matched your search
    criteria.</h2>';   echo '<ul>';    while($row=$result->fetchRow()){        $row['centercol']=str_replace
    ($searchterm,'<strong>'.$searchterm.'</strong>',$row['contents']);                       echo '<li><a href="http://www.mywebsite.com/index.php?id='.$row['id'].'">'
    .$row['title'].'</a><p>'.$row['contents'].'</p></li>';                     }    echo '</ul>'; } catch(Exception $e){ echo $e->getMessage(); exit(); }

    As you can see, the above code snippet first connects to MySQL, then performs the corresponding search by using the popular LIKE statement, and finally displays the returned result (if any) straight to the browser. It's short and simple!

    For this case in particular, I decided to use a LIKE command. If you're working with large databases, this isn't the best approach to follow. Instead you should use FULL-TEXT searches. I'm getting ahead of myself, since that will be the subject of the final part of the series.

    For now, try out the PHP script that I provided and enjoy incorporating this fully-functional AJAX-driven search engine into your own web applications.

    Final thoughts

    In this second part of the series, I showed you how to complete this AJAX-based search engine by defining a pair of PHP classes that actually perform searches against a selected database. While this approach will work with small websites, it's true that it can be greatly improved by implementing some handy features, such as FULL-TEXT and BOOLEAN searches, which are also supported by MySQL.

    That will be exactly the topic covered in the final installment of this series, thus you don't have any excuses to miss it!


    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.

       · Over the course of this second tutorial, this AJAX-based search engine is expanded,...
       · hello,The article was very helpful, but i have a problem how i can connect...
       · Hello Jova,Thank you for commenting on my AJAX article, and I'm glad to know it...
     

    JAVASCRIPT ARTICLES

    - Comparing Fields and Customizing Error Messa...
    - Checking Numbers and File Extensions with jQ...
    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek