Bytes Expert Newtork: Connect with experts in IT / Business | Expert Topics



Search


Go Back   Programmers Resource > Code Section > ASP Articles
User Name
Password


 
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 08-09-2004, 06:37 PM
admin admin is offline
Administrator
 
Join Date: May 2003
Posts: 116
Hi-Lighting Key Words in Search Results

A few things one should understand before attempting to understand this
code. An understanding of the Basic string functions would help out a bit.

This search uses a basic Access Database to query through. So lets begin the code.

Here is the basic form to submit to your search page:

Code:
<Form method="post" action="/template.asp"> Search our site and find the information you need! Use and, or, and the space to search for multiple words. <Input type="text" value="" name="keyword" size="30"><br> <input type="Submit" value="Search the Site"><br> <Input type="hidden" value="yes" name="srch"> </form>


This search will look for Boolean expressions like “and” and “or” as well as blanks like “visual basic”.

Now for the asp code to get the results from the search:

Code:
<% if request("srch") = "yes" then dim count, nWord, spWord, andWord, orWord ‘ set up variables word=replace(trim(request("keyword")),"'", "''") ‘ trim spaces and replace the single quote if instr(word, "and") then ‘ check Boolean of and andW = true Word = split(word,"and") ‘now get all the words separated by and into an array for i = 0 to ubound(andWord) if i <> ubound(andWord) then kW = "'%" & andWord(i) & "%'" ‘set up sql statement elseif i = ubound(andWord) then ‘ when it hits the end kW = kW &"'%" & andWord(i) & "%' and " ‘ add the last sql statement end if next elseif instr(word,"or") then orW = true orWord = split(word,"or") for i = 0 to ubound(orWord) if i <> ubound(orWord) then kW = "'%" & orWord(i) & "%' and " elseif i = ubound(orWord) then kW = kW & "'%" & orWord(i) & "%'" end if next elseif instr(word," ") then spW = true spWord = split(word," ") for i = 0 to ubound(spWord) if i <> ubound(spWord) then kW = "'%" & spWord(i) & "%' or " elseif i = ubound(spWord) then kW = kW & "'%" & spWord(i) & "%'" end if next else ‘There is no Boolean value or Blank in the search word<br> kW = "'%" & word & "%'" end if set conn = server.createobject("adodb.connection") conn.open ("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & _ Server.MapPath("\your.mdb")) set rs = server.createobject("adodb.recordset") sql = "select * from content where pageBody like " & kW


Now you have your connection set and sql built for the database search,
all that’s left is to open the recordset and highlight:

Code:
with rs .open sql,conn,3,3 if not .eof then do while not .eof pageBody = rs("pageBody") ‘ pass the field value to a local variable if andW then ‘this is from above when we checked for a Boolean value of AND for i = 0 to ubound(andWord) ‘go through the search word array pos = instr(pageBody,andWord(i)) ‘find the position of the word in the field. strW = trim(left(andWord(i),pos)) ‘ now pass the word found to local variable. newStr = "<strong>" & strW & "</strong>" ‘ and highlight the found word with bold html tags. pageBody = replace(pageBody, strW, newStr) ‘ now put back the word into pageBody variable. 'If we didn’t pass the rs(“pageBody”) first it would overwrite 'the variable every time it goes through the loop. Next ‘Now just set up two more for loops for “OR” and for BLANKS ‘The last one will be no Boolean values or blanks. else pos = instr(pageBody, word) strW = trim(left(word, pos)) newStr = "<strong>" & strW & "</strong>" pageBody = replace(pageBody, strW, newStr) end if ‘Now write out the results from the query with the highlighted 'words in there. response.write pageBody & "<hr><br>" .movenext loop else response.write "<br>No matching records<br>" end if end with end if %>


By Jeremy Ramos
www.deffacto.com
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



All times are GMT -5. The time now is 08:02 AM.



Powered by: vBulletin Version 3.0.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
All content Copyright ©1999 - 2010, Programmers Resource, unless otherwise noted.