ASP/.NET Web Hosting
* 3 Months FREE *
CLICK HERE!
 CoverYourASP --> Searching articles for hand-picked keywords, Part 3

Free membership

Join in the fun! Sign in
Member Services

Site navigation
Download the entire site!
Search my articles
Free Magazines
Browse the directory

Send me feedback
Buy my boxer shorts

Recommend this page
Printer-friendly page

Resources I recommend
Link to my site
Advertising slashed!
About your privacy
Legal stuff
Site statistics
152 active users
3120 visitors today
4684 pages today
how is this done?
ASP.NET Showcase
ASP.NET Graph and Chart Component with FREE developer version

ASP.NET Shopping Cart and E-commerce Control with FREE trial download

ASP.NET Web Hosting with free month and setup

$15 Domain Name Registration from .DNR.
As seen on these sites
Special thanks go to the sites that support me by publishing my articles.
See more

CoverYourASP
Copyright © 1999-2008 James Shaw.
All rights reserved.

ASP.NET Blog
ASPRSS
ShawThing

Now open source with SourceForge!

Creating the SQL statement

The first task is to turn the search expression into a SQL statement.

// get data from form
var sSearch = '' + Request.Form ( 'SearchText' );

if ( sSearch.length )
{
   // remove all reserved characters
   sSearch = sSearch.replace ( /['"%]/g, '' );

   // split into array, then reform with AND
   var sArray = sSearch.split ( ' ' );
   var sSQLSearch = '';

   for ( var i=0; i<sArray.length; i++ )
   {
      if ( i )
         sSQLSearch += ' AND ';

      sSQLSearch += 'Keywords LIKE \'% ' + sArray [ i ] + '%\'';
   }

Starting from the top, we ask the form for the SearchText value and make sure there was something entered by testing that the length property is non-zero.

Next, I use the replace method with a regular expression that removes any ' " % characters in the search expression to make it "SQL safe".

I then use the split method to create an array of items using a space as the separator.

Lastly I loop through the array items making up the sSQLSearch string. I separate multiple words with AND, and wrap the expressions you typed with the wildcard % character.

Note that I include a space after the first %, so you'll only find keywords that start with your expressions. (I prefix every word in the Keywords field with a space, even the first one)

Part 4: Querying the database...

Featured sponsor
My favorite resources

Selling products online - read how James does it now



See my source code
wherever you see this icon...

You can also download the entire site source code for FREE!


New Proposal Kit Professional 5.1
Brand yourself as a top professional: create quotes and amazing proposals and get many legal documents free!

The latter saved me 3 times the purchase price on the first day I owned it!