Jump to content

How to write a multi criteria search script


lnenad

Recommended Posts

So let's assume i'm making a script for searching cars by different criteria. Name of the car, color, and mileage. I used to do this by using LIKE and just adding AND for as many times as needed. But i've read that that is obsolete and is giving servers a hard time, so i went ahead and started making a new script that will use fulltext search. I've hit a dead end.

 

   function search($table,$what,$string,$limit,$start,$country) {
   global $totrows;
   global $pages;
   $string = explode("*",$string);
   $what = explode("*",$what);
   $cname = trim($string[0]);
   $t = 0;
   $parameters = "";
   while (isset($what[$t])){
   if ($t>0) { $paramm .= ", "; }
   $paramm .= trim($what[$t]);
   if ($t>0) { $parama .= " "; }
   $parama .= trim($string[$t]);
   $t++;
   }
   $parameters .= " AND locationc = '".$country."'";
   $sql = "SELECT *, MATCH(".$paramm.") AGAINST('".$parama."') AS score FROM main WHERE MATCH(".$paramm.") AGAINST('".$parama."') ORDER BY score DESC";
   $result = mysql_query($sql) or die(mysql_error());
   while ($row = mysql_fetch_array($result)) {
 var_dump($row);   
   
    }
   }

 

How does this work:

 

The user selects and enters his parameters>

The script then joins them all into a single variable which is then sent to the function(the reason i do this i because the user might input only the name, so that way i can add more criteria and not need to change the script)>

The script explodes those into arrays and then it processes them into a form for use with sql>

I run the sql query and then return the results.

 

What's the problem? There's two of them actually>

 

The first one is that mysql returns an error "Can't find FULLTEXT index matching the column list" even though i did setup fulltext indexes.

 

(phpmyadmin c/p)

name	FULLTEXT	No	No	name	0		YES	
color	FULLTEXT	No	No	color	0		YES	
mileage	FULLTEXT	No	No	mileage	0		YES	

 

 

The second problem is that the script is not selective and will not work as intended. For example, a car's name is 300, the users input's 300, and the script will return those rows that had mileage 300 or 300000 or whatever.

 

How do i fix this, and is FULLTEXT the right way to go with multi criteria search?

 

 

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.