0

Hi. I tried looking for this problem on Google, and found a lot, but nothing that actually seems to work. I basically have a search script that works fine when using one keyword, but when one uses more than that, it returns every single row per keyword. So a search for "hello kitty" doesn't look for "hello" and "kitty", but for "hello" and then another time for "kitty".

I know there's something wrong with my code, but, what? And how can I solve this issue.

// get the search variable from URL
$var = @$_GET['q'] ;
// trim whitespace from the stored variable
$trimmed = trim($var);  
// separate key-phrases into keywords
$trimmed_array = explode(" ",$trimmed);

// build SQL Query for each keyword entered 
foreach ($trimmed_array as $trimm)
{
	
	$query = "SELECT * FROM (articles LEFT JOIN authors ON articles.author_id = authors.author_id) WHERE text LIKE '%$trimm%' ORDER BY issue DESC" ;

	// and then the queries and everything; with which nothing's wrong

}

Can someone help me?

Edited by empoor: clarification

3
Contributors
4
Replies
6
Views
8 Years
Discussion Span
Last Post by dasatti
0

Explode on spaces and use ORs for the extra terms. http://php.net/explode

I'm already using explode to separate terms. And shouldn't it be "AND", 'cause I want the results to match all the results. It's basically already doing an "OR" search, which results in duplicate search results showing.

0

I have solved the issue myself by trying to fix things with experimentation.

My first problem was that the code I used to remove duplicate entries from the search array was basically looping twice. So, I edited the source to keep it from looping twice.

do
		{
			$adid_array[] = $row['page_id'];
		}
	
	while($row = mysql_fetch_array($numresults));
$tmparr = array_unique($adid_array); 
	$i = 0; 
	foreach ($tmparr as $newarr_v)
		{ 
			$newarr[$i] = $newarr_v; 
			$i++; 
		}

And then I use newarr to execute the search query (with foreach).

Secondly, because this is my first experience with building a search page, I used stock code and didn't notice that it was erroneously building a query for each keyword entered, instead of a query that incorporates all keywords (if multiple). So I removed

foreach ($search_array as $search_keyword)

, and wrote a little code to separate each keyword with AND.

$var = addslashes(htmlspecialchars($_GET['q'])) ;
// trim whitespace from the stored variable
$trimmed = trim($var);  
// separate key-phrases into keywords
$trimmed_array = explode(" ",$trimmed);
// count keywords
$trimm_total = count($trimmed_array);
$i = 0;
$searchstring = '';

// looping to get the search string
foreach ($trimmed_array as $trimm)
{ 
	if ($i != 0 and $i != $wordcount)
	{ 
		$searchstring .= " AND ";
	} 

	$searchstring .= "text LIKE '%$trimm%' OR page_title LIKE '%$trimm%' OR author LIKE '%$trimm%'"; 
	
	// incrementing the value 
	$i = $i + 1; 
}

And now it finally works.

Edited by empoor: clarification

0

Try this

// get the search variable from URL
$var = @$_GET['q'] ;
// trim whitespace from the stored variable
$trimmed = trim($var);  
// separate key-phrases into keywords
$trimmed_array = explode(" ",$trimmed);

// build SQL Query for each keyword entered
$query = "SELECT * FROM (articles LEFT JOIN authors ON articles.author_id = authors.author_id) WHERE";
 
$first = true;
foreach ($trimmed_array as $trimm)
{
                if(!$first)
	{
	    $query . = " AND ";
	}
                 else
                 {
                     $first = false;
                 }
	
	 
	$query = $query . " text LIKE '%$trimm%' " ;
 
	// and then the queries and everything; with which nothing's wrong
 
}
$query = $query. " ORDER BY issue DESC";

Edited by dasatti: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.