Jump to content

Multiple Word Seach


phpretard

Recommended Posts

I have a site search but for somes reason it is limited to a one word search.

 

Search1: If I search for "Find" or "Two" it works great. 

Search2: If I were to search for "Find Two" It returns nothing yet I know both are in the DB (based on Search1).

 

 

$find="ONEWORD" //the search return results
$find="TWO WORDS" //the search return nothing and I know the info is there.

$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find);

$data = mysql_query("SELECT * FROM cases WHERE (casetype LIKE '%$find%') OR (caseinfo LIKE '%$find%') ");

while($result = mysql_fetch_array($data)) 
{ 
$id=$result['id'];
echo "<h1>".$result['casetype']."</h1>"; 
echo "<hr />";
$caseinfo=$result['caseinfo'];
$caseinfofull=$result['caseinfo'];
echo "$caseinfofull";
echo  $shortdesc = substr_replace($caseinfo, '', 400, -1) . "..."; 
echo "<br>"; 
} 

Link to comment
Share on other sites

SCENARIO 1:

"I need to Find Two words" IS IN THE DB TABLES --|casetype|-- AND --|caseinfo|--.

 

 

If I search for "Find" result returned

 

If I search for "Two" result returned

 

Problem:

If I search for  "Find Two" no result.

 

 

SCENARIO 2:

 

"I need to Find" IS IN THE DB TABLE --|casetype|--

"Two words"  IS IN THE DB TABLE --|caseinfo|--

 

If I search for "Find" result returned

 

If I search for "Two" result returned

 

Problem:

If I search for  "Find Two" no result.

 

 

 

 

Link to comment
Share on other sites

		$find = strtoupper($find); 
	$find = strip_tags($find); 
	$find = trim ($find); 
	$find = explode(" ", $find);

	//Now we search for our search term, in the field the user specified 
	foreach ($find as $term){
	$data = mysql_query("SELECT * FROM cases WHERE (casetype LIKE '%$term%') OR (caseinfo LIKE '%$term%') ");
	}
	$matchescount=mysql_num_rows($data);
	$find = implode(" ", $find);

Link to comment
Share on other sites

That doesn't seem right. You hav a loop to run queries, but don't do anything with the results -they just get overwritten by the next query. Also, running multiple queries will be inefficient. This will be more compact and will also hadle a couple other problems. 1) If the string has consecutive spaces in the middle this replaces them with just one. 2) Also "cleans" the string for query purposes:

 

//Clean up the string
$find = strip_tags(strtoupper(trim($find))); 
//Remove multiple spaces from within string
$find = preg_replace('/[ ]+/', ' ', $find);

//Create MySQL code
$find = explode(' ', $find);
foreach ($find as $term)
{
    $term = mysql_real_escape_string($term);
    $terms[] = "\n    (casetype LIKE '%$term%') OR (caseinfo LIKE '%$term%')";
}

//Create the query
$query = "SELECT * FROM cases WHERE" . implode(' OR ', $terms);
$result = mysql_query($query) or die(mysql_error());

 

If the search value was "    one  <b>two</b>  three    " the resulting query would look like this:

SELECT * FROM cases WHERE
    (casetype LIKE '%ONE%') OR (caseinfo LIKE '%ONE%') OR 
    (casetype LIKE '%TWO%') OR (caseinfo LIKE '%TWO%') OR 
    (casetype LIKE '%THREE%') OR (caseinfo LIKE '%THREE%')

 

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.