order of relevance?
Posted: Mon Aug 01, 2005 10:24 pm
Hi, I am doing a search script for people names (lname,fname). I would search the database with this built query
When it is executed it will give the list of names that matches the begining of either lastname or firstname.
Now I want to sort the result so that the closest match is at the begining of the list. Do I have to add ORDER BY statement? If yes, how?
Thank you in advance.
Code: Select all
$toSearch = 'Harrison Dano';
$conditions = array();
$aray = explode(' ',$toSearch);
foreach($aray as $keyword){
$conditions[] = "lname like '$keyword%' OR fname LIKE '$keyword%'";
}
$query = "SELECT lname,fname FROM directory WHERE ".implode(' OR ',$conditions);
/* the query must be ...
SELECT lname,fname FROM directory
WHERE lname LIKE 'Harrison%' OR fname LIKE 'Harrison%' OR
lname LIKE 'Dano%' OR fname LIKE 'Dano%'
*/Code: Select all
George Harrison
Harrison Ford
Emanuel Danobelle
Harrison Dano <-- The exact match is sadly not the first itemThank you in advance.