I have created a search function on my project utilizing mysqli
LIKE query.
My final hurdle is the ORDER
in which it outputs.
Current Scenario:
if(isset($_GET['userInput']))
{
$search = $_GET['userInput'];
$search = explode(" ", $search);
if($search[0] != ''){
$firstterm = $search[0];
}else{
$firstterm = '@@@';
}
if($search[1] != ''){
$secondterm = $search[1];
}else{
$secondterm = '@@@';
}}
$contact = $mysqli->query("
SELECT * FROM contact WHERE
f_name LIKE '%$firstterm%'
OR l_name LIKE '%$firstterm%'
OR f_name LIKE '%$secondterm%'
OR l_name LIKE '%$secondterm%'
order by id LIMIT 5");
My question is, is there a way to ORDER
by the most amount of matching characters as apposed to id
. The reason being if I type 'C Blogs' looking for 'Craig Blogs' the 'C' will register a hit on everyone of my contacts because of the email address '.[c]om'.
So if I can get it to ORDER by most amount of matching characters (the added amount from both explodes where there is more than 1) the top result should theoretically be 'Craig Blogs'.