Home arrow JavaScript arrow Page 4 - Building the Server-side Component of a Search Engine with AJAX
JAVASCRIPT

Building the Server-side Component of a Search Engine with AJAX


Are you one of those web developers wishing to find new and creative ways to use the power of AJAX? Then this set of tutorials might be what you’ve looking for! Welcome to the second installment of the series “Building a search engine with AJAX.” In three comprehensive articles, this series teaches you how to create an expansible search engine which displays results without having to reload the web page.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
November 21, 2006
TABLE OF CONTENTS:
  1. · Building the Server-side Component of a Search Engine with AJAX
  2. · The search engine's client module
  3. · Coding the search engine's server module
  4. · Putting the search engine to work

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials