which keywords found from search

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
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

which keywords found from search

Post by hongco »

not sure how to put it, but let say that I enter a phrase of 4 key words (eg: apple orange banana pine)to search on a column 'text' of a table:

Code: Select all

$query = &quote;SELECT * FROM books WHERE text ='apple' OR text='orange' OR text='banana' OR text='pine'&quote;;
let say the result yields 10 rows in which any row can have text contain either one of the fruits, a few, or all. My question is, how do we return which row contains which keyword when the result is returned? In most searching function, we return the "text" with the keyword highlighted. I want to do the opposite, I need to display the 4 keywords, and underline anyone of them which "text" contains.

If we search 1 key word at a time, that can do, but in this case, i have have than 1 phrase to begin with; thus search 1 keyword at a time sounds not a good idea, but not sure if there is any better alternative.

Thanks.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

This will be impossible with a single MySQL statement alone.
You'll need to perform another check (PHP alone) from the query-result to figure out which rows contain which keywords - 2 dimensional array.
Have you tried using LIKE in MySQL ?
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

thanks for your reply. I thought about it and decided not to go this way. It takes too many queries for this part.

cheers!
Post Reply