PDA

View Full Version : Hi-Lighting Key Words in Search Results


admin
08-09-2004, 07:37 PM
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:


<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:


<%
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:


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