Jump to content

Archived

This topic is now archived and is closed to further replies.

intrik

Sort SQL Results by count of string occurance?

Recommended Posts

Hey guys,

I made this code to count how many times the search string occurs in the results data
[code]
$desc_lower = strtolower($results['desc']);
$title_lower = strtolower($results['title']);
$desc_count = substr_count($desc_lower, $searched);
$title_count = substr_count($title_lower, $searched);
$count_searches = ($title_count+$desc_count);
echo $count_searches;
[/code]

Idealisticly what I'd like to do is order the sql results by the frequency of the occurance, so obviously if $count_searches had a value of 1234 it would order before a value of 1233.

So you get the most relevant search results at the top.

Any ideas?

Share this post


Link to post
Share on other sites
I'm confused.. if you're doing this type of "scoring" in PHP, why not sort the results in PHP?

Share this post


Link to post
Share on other sites
Well, the problem is that I can't

SORT BY relevance DESC

In my version of MySQL... So it's going to have to be done with some PHP, how do I sort the results in PHP? Any ideas? I didn't even know you could do that...

Share this post


Link to post
Share on other sites
I assume that you can write a custom sort routine, though I never have in PHP.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.