Page 1 of 1

searching a database using multiple words

Posted: Tue Aug 13, 2002 4:21 am
by parms
helloe there

I am putting together a searcg engine on my web page to query the fields of a database.

Where the search is for a whole word or short title, it is okay. The problem i have is when i get the user to do a search for multiple words where each word is searched independantly.

Could somebody help me with this please.

Posted: Tue Aug 13, 2002 5:18 am
by Wayne

Code: Select all

SELECT * FROM table_name WHERE col1 LIKE "%$word1%" OR col1 LIKE "%$word2%"
etc...

Posted: Tue Aug 13, 2002 5:24 am
by parms
Thanks but that bit i understand that part.
BUT there could be 2 words or there could be 10 words. So how would i do that.
All the words are typed into one text box. How do i first retrieve the values and break the string into its individual word parts.

Then how/what do i do to take all that information and construct the querystring.

Posted: Tue Aug 13, 2002 10:30 am
by BDKR
Now this is the kind of thing that seperates the men from the boys so to speak. Do I figure out a way based on the tools I have, or do I ask someone else?

Well, you have the tools! And that's what's fun about programming. Having something that needs (or you want) to be done and formulating an approach based on what's available to you as a programmer. So, rather then just tell you, I'll give you a hint or two.

Use an array.
Use a loop.
sizeof() is your friend.
The "." operator is your friend.

Hate me yet!? :twisted:

Later on,
BDKR

Posted: Tue Aug 13, 2002 10:38 am
by llimllib
Special bonus round: explode() could help too.

If you really make an effort at it, and fail, post it back here, and we'll def help you out. Until then, good luck!

Posted: Tue Aug 13, 2002 10:46 am
by BDKR
Yeah(!), explode() could based on how you create the form. My thinking heretofore didn't need the explode() function, but a form that needs one would make more sense.

Later on,
BDKR

Posted: Tue Aug 13, 2002 11:17 am
by JPlush76
lets not forget to code in for if the user really wants a two or three word string.. does that OR count as an AND/OR?

IE: white tennis shoes

you'd want all of those words together, not searched seperate right?

Posted: Tue Aug 13, 2002 12:01 pm
by llimllib
not really, you'd want "white tennis shoes" together but white tennis shoes apart, i would imagine

Posted: Tue Aug 13, 2002 12:06 pm
by JPlush76
what if the data was in the database as "reeboks white, hightop tennis shoes"

then the string "white tennis shoes" wouldn't work but they are still searching on something valid.

how would you code for something like that?

Posted: Tue Aug 13, 2002 12:15 pm
by llimllib
:lol: use phpDig :lol:

but seriously, no, if the user entered <"white tennis shoes"> (without the carets) then that would not be a match, becuse they'd be searching for that exact string.
if they entered <white tennis shoes>, then I would want that to be a match and would explode() the terms and search for them.

that's what i was trying to say in my last post, but it wasn't very clear