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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.