PDA

View Full Version : Creating A Search Form


Thatguy2001au
10-14-2002, 10:34 AM
HI

I have made a search engine for my site which uses a text box and an sql statement which uses what u type into the text box to make the search. The problem is, that the search only searches the whole string typed into the text box and it is unable to split up the words.

SO basically, if i was to type 'Hello World' into the search box, it will search a field in my database but the field must only have 'Hello World' in it for the search to return something.

So, what I want to do is be able to type in 'Hello World' and the search split up the two words and return something if either one of them is present in the database field.

I know my explanation is not the best but any help will be appreciated and i will try and explain things a little better.

glenngv
10-14-2002, 10:45 AM
searchkey = request.querystring("searchkey")

if searchkey<>"" then

arr = split(searchkey," ")
strCondition = ""
if ubound(arr)>0 then
for each item in arr
if strCondition = "" then
strCondition = "field_name like '%" & item & "%'"
else
strCondition = strCondition & " OR field_name like '%" & item & "%'"
end if
next
else
strCondition = "field_name like '%" & searchkey & "%'"
end if

strSQL = "SELECT * FROM tablename WHERE " & strCondition
response.write strSQL 'for debugging purposes

'other codes here

end if



replace italic text with appropriate names

Thatguy2001au
10-14-2002, 12:02 PM
Thanks Glenn.

You were a great help. It now works like a dream.

whammy
10-15-2002, 12:34 AM
I'd check out the Split() and Join() functions of VBScript (and javascript and other languages as well!), so you really master them since they are a huge help programming.

In VBScript, using Split() on a string where appropriate is especially helpful to make arrays easier to handle since you don't have to dim them to a certain number and then redim them - since every variable is datatype "Variant" to start with... :)

Thatguy2001au
10-15-2002, 09:12 AM
Thanks Guys

I will look into the split() function and try to get a good handle on it. I am sure it will come in handy later.

thanks.

Mhtml
10-15-2002, 09:59 AM
Cant you do it like this?

"Select * From Table_Name Where Field_Name Like ='%" & Request.From("Search")&'"

glenngv
10-15-2002, 10:23 AM
Originally posted by Mhtml
Cant you do it like this?

"Select * From Table_Name Where Field_Name Like ='%" & Request.From("Search")&'"


:eek:

read the question again :)

Mhtml
10-15-2002, 10:38 AM
I was under the impression that if you used the % it would do what was needed here. Taking into account your response I'm guessing it's not.

glenngv
10-15-2002, 10:47 AM
but I did also used % in the SQL statement in the code that I posted. :)

whammy
10-16-2002, 01:43 AM
Yup... and the code looks pretty good. ;)

I'm gonna close this thread since it seems everyone is happy. :D