>I'm trying to take a variable in that would be usually 3 to 4 words (could
>be more or could be less). Right now, I'm exploding the variable and
>testing each individually, but this obviously is a pain with the more
>variables you get, and the result I get from the mysql search doesn't give
>me enough info to compare the outputs and then just give matching outputs.
>Am I looking at the result of the query wrong? Is there a function that
>does this? Any suggestions?
Cheat :-)
The "LIKE" operator and "=" and all the other ones that fit in a WHERE
clause will automagically turn into 1/0 for TRUE/FALSE.
So you can give an easy "point" system to your search for multiple words:
<?php
$search = "three or four words";
$words = explode(' ', $search);
$query = "select 0 "; # A sort of "yeast" or "seed" to start off our
scoring
while (list( ,$word) = each($words)){
$query .= " + (whatever like '%$word%') "; # Give 1 point for like
"match"
$query .= " + 2 * (whatever = '%$word%') "; # Give 2 *extra* points for
perfect match (3 total)
}
$query .= " as score "; # The total sum will get called 'score' in the
query
$query .= " , column1, column2, column3 "; # Include whatever you need to
display the list
$query .= " from tablewhatever ";
$query .= " where score > 0 "; # Don't bother with a non-match
$query .= " order by score desc "; # Rank the scores high to low
(descending)
$matches = mysql_query($query) or error_log(mysql_error()); # Check Apache
error_log for trouble!
while (list($score, $column1, $column2, $column3) =
mysql_fetch_row($matches)){
echo "$score: $column1, $column2, $column3<BR>\n";
}
?>
--
Like Music? http://l-i-e.com/artists.htm
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php