Jump to content

ORDER BY - Closest match


Canman2005

Recommended Posts

Hi all

 

Im currently running a complex query and I need to try and sort the results into an order which the closest match first.

 

I was told there is a simple ORDER BY function which can be used.

 

Has anyone done a ORDER BY CLOSEST MATCH or whatever the query should be?

 

Any help would be sweet

 

Thanks

 

Ed

Link to comment
https://forums.phpfreaks.com/topic/88596-order-by-closest-match/
Share on other sites

Hi

 

Well my query is the following, it basically takes keywords entered and searches the database for anything matching, I need to get these to order with the closest match showing first.

 

Is that possible?

 

$arrSearches  = explode(" ",$_GET['q']);
$arrMatches = array();

foreach ($arrSearches as $strKey => $strValue)
{
array_push($arrMatches," `title` like '%$strValue%' ");
}

$remakequery = implode(" OR ", $arrMatches);

$sql = "SELECT * FROM news WHERE ".$remakequery."";
print $sql.'<br>';
$query = @mysql_query($sql,$connection) or die(mysql_error());
while ($row = mysql_fetch_array($query))
{
print $row['title'].'<br>';
}

 

Any help would be ace as ive been trying to crack this for days now.

 

Thanks

 

Ed

Archived

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

×
×
  • Create New...

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.