Quote:
Originally Posted by ratbastid
--snip--
Then run a post-query ranking process where you test each record returned from your query for the presence of each of your search terms, and scores them. Then sort your responses by score, and bob's yer uncle.
Like this:
--snip--
Note that I didn't actually run this code--this is strictly off-the-top-of-my-head. YMMV greatly.
|
I am not sure exactly how to implement your example.. what is the purpose of returning 1, 0 or -1 on the sorting function.
Here is your code with some modifications for my situation:
Code:
$search = explode(" ",$_GET["q"]);
$query = 'SELECT * FROM posts WHERE 0=0 OR ';
foreach ($search as $term) {
$query .= "'Body' LIKE '%$term%' OR 'intro' LIKE '%$term%' OR ";
}
$query = substr($query, 0, -3);
$searchquery = mysql_query($query,$db);
while ($results = mysql_fetch_array($searchquery)) {
foreach ($results as $i => $record) {
$result[$i++][record] = $record;
foreach ($search as $word) {
if (ereg($word, $record)) {
++$result[$i][score];
}
}
}
}
//The user_sort function here
$sorted = usort($result, 'user_sort');
print_r($sorted);
How do I go about outputting results in a sorted manner?