When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Thursday, November 30, 2000

Searching 4GuysFromRolla.com, Part 2

  • Read Part 1

  • The 4GuysFromRolla.com search scours through a SQL Server database to return relevant links based upon the user's search terms. In Part 1 we looked at how the content from the static articles makes its way into the tblArticleIndex table. In this part, we'll examine the ASP page that performs the searching and display of relevant articles!

    - continued -

    The search on 4Guys is made up of two pages:

    • /search/index.shtml - This page allows the use to enter search terms
    • /ASPScripts/search.asp - This page displays the relevant articles in a paged format

    The /search/index.shtml page is fairly straightforward: it created a FORM that accepts search terms from the user and sends them to the search page (/ASPScripts/search.asp) when the form is submitted. Below you can see the HTML that generates the form:

    <FORM METHOD=GET NAME=frmSearch ACTION="/ASPScripts/Search.asp">
      Enter the text to search for:<BR>
      <INPUT TYPE=TEXT NAME=SearchText SIZE=30><BR>
      <INPUT TYPE=SUBMIT VALUE="Search" TITLE="Click to search 4Guys!">

    The work of parsing the search terms into a SQL string and paging the database records is handled by /ASPScripts/search.asp. We'll step through the code of this page and see how the search terms are broken up into a SQL string. Before we do, however, realize the effect that I was after when creating this search system. I wanted users to be able to enter a single word or a string of words and have a search performed on those. For example, if a user entered: ActiveX Data Objects I wanted to return all articles that contained the exact string "ActiveX Data Objects" in their text. I also wanted to be able to support the AND and OR boolean operators, so that if a user entered something like: ADODB or OLE-DB it would return articles that contained the exact string ADODB or OLE-DB.

    To start the /ASPScripts/search.asp script off, I begin by reading in some variables from the QueryString:

      'How many records to display per page
      Const RecsPerPage = 8
      'What results page are we on?
      Dim iPageNo
      iPageNo = Request.QueryString("Page")
      if Not IsNumeric(iPageNo) or iPageNo = "" then iPageNo = 1
      'Get the search terms... if we have no search terms, redirect the user to the
      'starting search page.  Strip the double quotes and replace all single quotes
      'with a pair of single quotes.
      Dim txtSearchTerms
      txtSearchTerms = Replace(Request("SearchText"),"""","")
      txtSearchTerms = Replace(txtSearchTerms,"'","''")
      If txtSearchTerms = "" Then Response.Redirect "/search/"
      'Get rid of parenthesis in the search terms	
      txtSearchTerms = Replace(txtSearchTerms,"(","")
      txtSearchTerms = Replace(txtSearchTerms,")","")

    We start by defining some constants and the variables that we will be reading in through the QueryString, such as the page number. (Since we are paging the results, the page number represents what "page" of results we wish to review. If no such value is passed in, the script assumes we wish to view the first page of data.) We also use the Replace function to strip out any double quotes and to replace all single quotes with two successive single quotes. We also strip any parenthesis. (For more information on using the Replace function, be sure to read the FAQ: How can I replace each occurrence of a particular pattern in a string with another pattern? For an explanation on why we replace single quotes with two single quotes, be sure to read How to Deal with Apostrophes in your SQL String!)

    In Part 3 we'll continue our examination of the /ASPScripts/search.asp script!

  • Read Part 3!

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article