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
https://forums.phpfreaks.com/topic/142796-multiple-word-seach/
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
https://forums.phpfreaks.com/topic/142796-multiple-word-seach/#findComment-748482
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
https://forums.phpfreaks.com/topic/142796-multiple-word-seach/#findComment-748508
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
https://forums.phpfreaks.com/topic/142796-multiple-word-seach/#findComment-748537
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.