Home arrow JavaScript arrow Page 3 - 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 - Coding the search engine's server module
(Page 3 of 4 )

Since all the processes for performing the search against a selected database will be implemented with PHP 5, then logically I'll first create a couple of straightforward MySQL processing classes. These classes will take care of doing all sorts of clever tasks, like connecting to the server, selecting a particular database, handling MySQL result sets, and so forth.

That being said, here are the respective definitions for these two new PHP 5 classes:

// define 'MySQL' class class MySQL{     private $conId;     private $host;     private $user;     private $password;     private $database;     private $result;     const OPTIONS=4;     public function __construct($options=array()){         if(count($options)!=self::OPTIONS){             throw new Exception('Invalid number of
connection parameters');       }         foreach($options as $parameter=>$value){             if(!$value){                 throw new Exception('Invalid
parameter '.$parameter);          } $this->{$parameter}=$value;      }         $this->connectDB(); } // connect to MySQL     private function connectDB(){         if(!$this->conId=mysql_connect($this->host,
$this->user,$this->password)){             throw new Exception('Error connecting to the
server');        }      if(!mysql_select_db($this->database,$this->conId)){             throw new Exception('Error selecting
database');         } } // run query     public function query($query){         if(!$this->result=mysql_query($query,$this->conId)){             throw new Exception('Error performing
query '.$query);         }         return new Result($this,$this->result); } } // define 'Result' class class Result {     private $mysql;     private $result; public function __construct(&$mysql,$result){         $this->mysql=&$mysql;         $this->result=$result; } // fetch row     public function fetchRow(){         return mysql_fetch_assoc($this->result); } // count rows     public function countRows(){       if(!$rows=mysql_num_rows($this->result)){           return '0';       }         return $rows; } // count affected rows     public function countAffectedRows(){         if(!$rows=mysql_affected_rows($this->mysql->conId)){         throw new Exception('Error counting affected
rows');       }       return $rows; } // get ID from last-inserted row     public function getInsertID(){         if(!$id=mysql_insert_id($this->mysql->conId)){             throw new Exception('Error getting ID');      }         return $id; } // seek row     public function seekRow($row=0){         if(!is_int($row)||$row<0){             throw new Exception('Invalid result set
offset');       }         if(!mysql_data_seek($this->result,$row)){             throw new Exception('Error seeking data');       } } }

At this point, after you grasped the logic followed by the couple of PHP classes shown above, it's clear to see here that the only methods that I'm going to use are those that are useful for connecting to MySQL, as well as for running queries and fetching rows from a selected database table. It makes sense, doesn't it?

Having listed the previous PHP 5 classes responsible for performing searches in the server, let me explain briefly how I plan to use them to implement  a functional search engine. Basically, since the application will be used as part of a fictional dynamic website, I'll assume that there's a unique database table called "pages," containing the fields "ID," "Title" and "Contents" respectively.

Logically, the "ID" field will store the identifier of each dynamic web page that comprises the whole site. The "Title" field will house the web document's title, and finally the "Contents" column will contain the dynamic contents delivered to visitors. Of course, this is only a basic database schema that can be easily adapted to work with larger websites, but for now, it's more than enough for demonstrating how this search engine will work.

Speaking of that, in the next few lines I'll show you how to put the AJAX-based search application to work by including some basic PHP code. Click on the link below and keep reading.


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