Wife/Girlfriend/Sister?
Pepper Spray &
Stun Gun Specials!
KEEP THEM SAFE.
 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
36 active users
1886 visitors today
2476 pages today
(only part of today)
Tools I use

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

ASP.NET Blog
RSS submissions
E-commerce

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

Tiki Statues - Tiki Masks - Tiki Totems



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!