Interesting, complicated search dilema

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
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Interesting, complicated search dilema

Post 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?
User avatar
lafever
Forum Commoner
Posts: 99
Joined: Sat Apr 05, 2008 2:03 pm
Location: Taylor, MI

Re: Interesting, complicated search dilema

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Interesting, complicated search dilema

Post 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?
User avatar
lafever
Forum Commoner
Posts: 99
Joined: Sat Apr 05, 2008 2:03 pm
Location: Taylor, MI

Re: Interesting, complicated search dilema

Post 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?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Interesting, complicated search dilema

Post 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.
User avatar
lafever
Forum Commoner
Posts: 99
Joined: Sat Apr 05, 2008 2:03 pm
Location: Taylor, MI

Re: Interesting, complicated search dilema

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