Jump to content

PHP MySql Like Query, Sort by best match


langemarkdesign

Recommended Posts

I am working on a PHP search engine. What I am doing is running a MySQL Like Query for multiple keywords, and would like to sort the results according to the number of the keywords it contains.

 

Here is my current code:

 

 

$query = "SELECT * FROM library WHERE title LIKE '%$keyword1%' OR title LIKE '%$keyword2%' OR title LIKE '%$keyword3%'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo $row['title'] . "<br>";
}

 

And an example of what I want:

 

$keywords = array("homemade", "lasagna", "ingredients");

 

With those keywords I would get these results:

 

home made lasagna

worlds best lasagna

homemade ingredients

easy at home lasagna

fresh lasagna ingredients

how to make homemade lasagna with just 6 ingredients

 

And here's how I want it sorted:

 

how to make homemade lasagna with just 6 ingredients

fresh lasagna ingredients

homemade ingredients

easy at home lasagna

home made lasagna

worlds best lasagna

 

Sorting results with identical number of keywords alphabetically.

 

Any help would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/199320-php-mysql-like-query-sort-by-best-match/
Share on other sites

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.