Results 1 to 2 of 2


  1. #1
    Lujka Guest


    I need to implement a robust search engine and I am trying to figure out how to parse a string with multiple words in it using VBScript and ASP.<BR>I need to separate all words that have a blank space between them and use them to build an SQL string. I figured out how to parse out two words, but that is not good enough!!! <BR>I really appreciate any help on this!<BR>L<BR><BR>

  2. #2
    Ben Jones Guest


    I&#039m assuming that you don&#039t want to get into boolean searches for the moment (you can do that too, but it is a bit more complicated). I&#039m going to assume a few things:<BR><BR>1. a form field called "searchwords" is going to contain the words to search for<BR><BR>2. a table called "products" is what you&#039re searching, and you want to search the "product_description" field.<BR><BR>Assuming the person submitted a search for "the quick brown fox jumped", this code would generate the following:<BR><BR>SELECT product_ID, product_name FROM products WHERE (product_description LIKE &#039%the%&#039 OR product_description LIKE &#039%quick%&#039 OR product_description LIKE &#039%brown%&#039 OR product_description LIKE<BR>&#039%fox%&#039 OR product_description LIKE &#039%jumped%&#039)<BR><BR><BR>&#060;%<BR><BR>&#03 9 open dbase connection<BR>&#039 ...<BR><BR>searchwords = request.FORM("searchwords")<BR><BR>if len(searchwords) > 0 then<BR><BR> searchwords = "&#039%" & trim(searchwords) & "%&#039"<BR><BR> x = 1<BR> maxnum = 1<BR> do while x > 0 AND maxnum &#060; 10<BR> x = instr(x, searchwords, " ")<BR> if x > 0 then<BR> searchwords = left(searchwords, x-1) & "%&#039 OR product_description LIKE &#039%" & right(searchwords, len(searchwords) - x)<BR> x = x + len("%&#039 OR product_description LIKE") + 1<BR> end if<BR> maxnum = maxnum + 1<BR> loop<BR> <BR> <BR> searchwords = "(product_description LIKE " & searchwords & ")"<BR> <BR> <BR> sSQL = "SELECT product_ID, product_name FROM products WHERE " & searchwords & " ORDER BY product_name<BR> set RS = Conn.Execute(sSQL)<BR> <BR> do while RS.EOF = FALSE<BR> &#039 etc ...<BR> RS.moveNext<BR> loop<BR> <BR> set RS = nothing<BR><BR>end if<BR><BR>&#039Conn.Close<BR>&#039set Conn = nothing<BR><BR>&#037;&#062;<BR><BR>You can use the same technique to search multiple fields at once.<BR><BR>One more thing to keep in mind is that you may want to give people the option to search for exact strings, exact words with multiple keywords listed, a combination of exact words and exact phrases, etc. The more functionality you require, the more complicated the algorithm gets, but it is all possible using ASP, perl, cold fusion, or pretty much any other language.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts