CoverYourASP --> utils/Search.asp" --> Source

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
103 active users
1452 visitors today
1202 pages today
how is this done?
Tools I use

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

ASP.NET Blog
RSS submissions
E-commerce

Now open source with SourceForge!

This page shows the actual source code used on this site. If this is the first CYA source code you've seen you should read this overview first.

Did you know you can download all the source code (and the database) of this site? Then get my newsletter to be emailed when I update the source code!

Please spread the word by recommending my site to your friends and colleagues!

This is JScript (server-side JavaScript), not the more common VBScript. More...

utils/Search.asp

<%
// ============================================
// NOTE: all source code downloaded from CoverYourASP was written by
// James Shaw (unless stated otherwise), and is copyright (c) 2000-2002
// by James Shaw. You can use the code for any purpose, but do not
// publish or distribute the content in any way.
//
// See http://CoverYourASP.com/Legal.asp for up-to-date details.
// ============================================

// ============================================
// display search form
// ============================================
function ProcessSearch ( )
{
   // get data from querystring
   var sSearch = unescape ( '' + Request.QueryString );

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

   // has the form been submitted?
   if ( sSearch != 'undefined' )
   {
      var bFound = false;

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

         var sTableDescrs = new Array ( 'Article', 'Snippet' );
         var sFields = new Array ( 'Keywords', 'Snippet' );
         var sSpaces = new Array ( ' ', '' );

         var sAttempt = new Array ( 'AND', 'OR' );
         var sArray = sSearch.split ( ' ' );
         var nAttempts = 2;

         if ( sArray.length == 1 )
            nAttempts = 1;

         for ( var nAttempt=0; nAttempt<nAttempts; nAttempt++)
         {
            for ( var nTable=0; nTable<sTableDescrs.length; nTable++ )
            {
               // split into array, then reform with AND
               var sSQLSearch = '';
               var sVerbose = '';

               for ( var i=0; i<sArray.length; i++ )
               {
                  if ( i )
                  {
                     sSQLSearch += ' ' + sAttempt [ nAttempt ] + ' ';
                     sVerbose += ' ' + sAttempt [ nAttempt ] + ' ';
                  }

                  sSQLSearch += sFields [ nTable ] + ' LIKE \'%' + sSpaces [ nTable ] + sArray [ i ] + '%\'';
                  sVerbose += sArray [ i ];
               }

               // simple search in ArticlePages table
               DBInitConnection ( );

               // use static cursor to get record count
               switch ( sTableDescrs [ nTable ] )
               {
               case 'Article':
                  oRecordSet.Open ( 'SELECT URL,ShortDescr FROM ArticlePages WHERE ' + sSQLSearch, oConnection, adOpenStatic );
                  break;

               case 'Snippet':
                  oRecordSet.Open ( 'SELECT "/Snippet_snip_.html" & SnippetID AS URL,Title FROM Snippets WHERE ' + sSQLSearch, oConnection, adOpenStatic );
                  break;
               }

               if ( !oRecordSet.EOF )
               {
                  bFound = true;

                  var nCount = oRecordSet.RecordCount;

                  if ( nCount == 1 )
                     Out ( '<p>I found ' + nCount + ' ' + sTableDescrs [ nTable ] + ' matching "' + sVerbose + '". Clicking on the link below will open a new browser window containing that page.<p>' );
                  else
                     Out ( '<p>I found ' + nCount + ' ' + sTableDescrs [ nTable ] + 's matching "' + sVerbose + '". Clicking on a link below will open a new browser window containing that page.<p>' );

                  while ( !oRecordSet.EOF )
                  {
                     var sURL = '' + oRecordSet ( 0 );
                     var sShortDescr = '' + oRecordSet ( 1 );

                     Out ( '<a href="' + sURL + '" target="CYAExternal">' + sShortDescr + '</a><br>' );

                     oRecordSet.moveNext ( );
                  }
               }

               DBReleaseRecords ( );
            }

            // simple search in Snippets table

            DBReleaseConnection ( );
         }
      }

      if ( bFound )
      {
         Out ( '<p>Not quite what you were looking for? If you think this search should have turned up more pages, please <a href="/Contact.html">tell me about it</a>.' );
      }
      else
      {
         Out ( 'Sorry, I found no pages that contained "' + sSearch + '".' );
         Out ( '<p>If you think I should have, please <a href="/Contact.html">tell me about it</a>.' );

         // quietly let me know anyway so I can add it if necessary
         SendEmail ( 'SearchEngine@' + sHostDomain, 'FailedSearch@' + sHostDomain, '', 'Search failed to find "' + sSearch + '"', 'The search engine on ' + sHostDomain + ' searched for "' + sSearch + '", but found nothing.' );
      }
   }
   else
   {
      Out ( 'Use this form to search through the articles on CoverYourASP. You are searching through a database of keywords that I hand-picked for each page.' );
      
      Out ( '<p>Please bear in mind that this is only the first stage of adding a search capability to the site - there is no way yet to search for "a AND b". If you enter multiple words I will return results that match any of them.' );

      Out ( '<p><b>Tip:</b> You don\'t have to type all of the words you\'re searching for - you can type "ad", not "advertising", or "appl" instead of "application variables".' );
   }

   // always display search form
   Out ( '<p><form action="/Search.html" method="post">' );

      Out ( '<input type="text" name="SearchText" size="20">' );

      Out ( '&nbsp;<input type="submit" value="Search" title="Search CoverYourASP">' );

   Out ( '</form>' );
}
%>

Hopefully much of this is self-explanatory. If not, or if you see ways that I can improve the code, please drop me a line.

To see the source code for this page, click on the icon below.

Featured sponsor
My favorite resources

CoverYourASP Mugs, T-shirts, caps - even Boxer shorts...
I don't make a penny from these, but they're a lot of fun! Don't you need a new mouse mat?


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!


Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!


I share my content

Supporting ASPRSS

Do you need a quick and easy way to link to my articles? All the information you need is published with ASPRSS...