Jump to content

Mysql Search Order help


gvp16

Recommended Posts

Hi, im doing a search which looks like this :

 

if(isset($_GET["q"]) && $_GET["q"] !="")// get the search string
{
$words = explode(" ",$_GET["q"]);//explode on the spaces
$count = count($words);
for($i =0; $i<= $count; $i++)
{
if($words[$i] !="")
{
	$qt .= "(description LIKE '%".addslashes($words[$i])."%' OR ";  //build sql query
	$qt.= "productcode like '%".addslashes($words[$i])."%' ) AND ";
}	
}
$q = "select * FROM Main WHERE $qt stock > 0 AND wholesale = '1' AND itemStatus = '1' LIMIT 15";//run search

 

the search works exactly as we want it, so it picks up any words with the key word in.

 

But how can i order the results by relevance, eg. if i searched for "cat" i would get results like :

 

dreamcatcher

cat box

cats picture

 

how could i order the results so the whole word is at the top? eg.

 

cat box

casts picture

dream catcher

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/252744-mysql-search-order-help/
Share on other sites

eg. write and if statement or something that checks the results and orders them based on if they area whole word etc...?

Pretty much -- unless you're going to eliminate some of the before you sent them back, there's little advantage to doing the processing in the DB.

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.