Page 1 of 1

Interesting, complicated search dilema

Posted: Wed Apr 09, 2008 12:31 pm
by Citizen
Basically, I have a search function here that searches for each of the words that a person types in.

A search for
american men
will bring up all movies that have either "american" or "men" in the title (this is good)

However, there is one problem. There is no way to search for a keyphrase instead of multiple keywords. I'm sure there's a way, but not one that i can think of that would allow them to search
"american men"
and come up with something that requires both. Sort of how most search engines work.

Here's my code:

Code: Select all

if($_POST['title'] != ""){
    $title = explode(" ", strip_tags(mysql_real_escape_string($_POST['title'])));
    $movies = array();
    foreach($title as $value){
        $sql = "SELECT * FROM `videos` WHERE `tags` LIKE '%$value%'";
        $result = mysql_query($sql);
        while($row = mysql_fetch_array($result)){
            if(!in_multi_array($row, $movies)){
                $movies[] = $row;
            }
        }
    }           
}
Any ideas?

Re: Interesting, complicated search dilema

Posted: Wed Apr 09, 2008 1:50 pm
by lafever
I'm probably not much of help but couldn't you a check with maybe a regular expression like preg_match before the explode that checks to see if the string begins and ends with a quote and if it matches that run a separate loop that searches for that entire string, else run your current loop?

Just spitting out ideas.

Re: Interesting, complicated search dilema

Posted: Wed Apr 09, 2008 1:58 pm
by John Cartwright

Code: Select all

if(!empty($_POST['title'])){ //avoid E_NOTICE warnings
    $title = mysql_real_escape_string($_POST['title']);
    $sql = "SELECT * FROM `videos` WHERE `tags` LIKE '%$title%'";
    
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result)){
        $movies[] = $row;  
    }
}
Instead of exploding by spaces, simply pass the title to the query. Am I missing something obvious?

Re: Interesting, complicated search dilema

Posted: Wed Apr 09, 2008 2:25 pm
by lafever
I think he wants to do the explode, so let's say I typed

Code: Select all

movie title
It would display all results containing 'movie' and 'title'

And if it's encased in "

Code: Select all

"movie title"
It will return only results containing the whole string.


Correct me if I'm wrong?

Re: Interesting, complicated search dilema

Posted: Wed Apr 09, 2008 3:29 pm
by onion2k
I think that's right.

The answer is to write a simple parser that detects the quotes and breaks the terms up a bit more intelligently than just exploding on the spaces. I'm sure I wrote one that could handle AND and OR patterns as well ... can't find the code now though.

Re: Interesting, complicated search dilema

Posted: Wed Apr 09, 2008 4:13 pm
by lafever
I played with the regular expressions a little bit and I'm not so great with them but I'm pretty sure I got it working correctly. This will only return data enclosed in the first set of " " also.

Code: Select all

 
$title='"movie title"';
 
if (preg_match('@^(?:"){1}?([^"]{1}+)@i', $title)) {
    echo "in quotes";
} else {
    echo "not in quotes";
}
 
I hope I could help a little with your issue.