searching a database using multiple words

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
parms
Forum Newbie
Posts: 6
Joined: Wed Aug 07, 2002 5:57 am

searching a database using multiple words

Post 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.
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

Code: Select all

SELECT * FROM table_name WHERE col1 LIKE "%$word1%" OR col1 LIKE "%$word2%"
etc...
parms
Forum Newbie
Posts: 6
Joined: Wed Aug 07, 2002 5:57 am

Post 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.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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!
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post 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?
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

not really, you'd want "white tennis shoes" together but white tennis shoes apart, i would imagine
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post 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?
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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
Post Reply