Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple keyword search engine

Status
Not open for further replies.

karren

Programmer
Feb 26, 2002
42
CA
Hi everyone,

I am making a multiple keyword search engine that will search 3 different tables. it works ok, except that when i enter two keywords, it displays duplicate results.

if a person enters several keywords into the keyword text box, i explode the string into an array. then using the for loop, i loop through each keyword and within the for loop, i have an sql query statement that checks whether each word exists in any of the 3 tables.

sometimes, 2 or 3 keywords will appear in the same table within the same fields. this causes the duplicate results to display because the loop checks each keyword then prints out the result for each keyword, even if each keyword appears in the same field in the table.

i tried using array_unique function but it didn't seem to work. i might not have been using it properly as i am fairly new to php.

does anybody know how to eliminate the duplicate results from the multiple keyword search engine?

any help is greatly appreciated!! thanks :) :)

karren
 
my guess is that the sql query uses field like %somevar% AND field like %anothervar%

change the AND to OR

but hard to say without seeing the code

please post if my answer is not correct Bastien

There are many ways to skin this cat,
but it still tastes like chicken
 
Thanks for your reply, Bastien :) yes, i am using OR instead of AND in the SQL statement.

here is part of the code:

<?php
$keywords = $HTTP_POST_VARS[&quot;keywords&quot;];
$loops = 0;
$explode_key = explode(&quot; + &quot;,$keywords);
$loops = count($explode_key);
$search_results = '';
$num_row = 0;
$results = '';
$allresults = '';
for($i=0; $i < $loops; $i++)
{
$strSQL = &quot;SELECT SectionName, Title, Body FROM Content WHERE Body LIKE '%$explode_key[$i]%';&quot;;
if(!$rs_Content = mysql_query($strSQL)) die(&quot;Query died for $strSQL&quot;);

if(mysql_num_rows($rs_Content) > 0)
{
while($row = mysql_fetch_array($rs_Content))
{
$sectiontitle = $row[&quot;Title&quot;];
$sectionbody = $row[&quot;Body&quot;];
$sectionname = $row[&quot;SectionName&quot;];
$num_row = $num_row + 1;
$allresults = $allresults . &quot;<b><i><a class='contentLink' href='index.php?Part=$sectiontitle'>$sectionname</a></i></b><br /><br />&quot;;

}

}


i query 3 different tables for which i have 3 different SQL statments that repeat the same code above. then i end the loop. when i print out the results, i do it like this (its not within the for loop):



<table width=&quot;460&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; border=&quot;0&quot;>
<tr>
<td><span class=&quot;Title&quot;>Search Results</span></td>
</tr>
<tr>
<td><span class=&quot;Content&quot;>Your query produced <b><?= $num_row ?></b> results in the following pages:</span></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><span class=&quot;contentLink&quot;><?= $allresults ?></span></td>
</tr>
</table>
 
What ypou have done is to create and run the sql $loop times...what you need to do is create the sql inside a loop and execute outside the loop...

<?php
$keywords = $HTTP_POST_VARS[&quot;keywords&quot;];
$explode_key = explode(&quot; + &quot;,$keywords);
$search_results = '';
$num_row = 0;
$results = '';
$allresults = '';
$loops=1;
$strSQL = &quot;SELECT SectionName, Title, Body FROM Content WHERE Body like &quot;;

foreach($explode_key as $item){
//since the sql statement is getting created dymanically
//we need to have a way to allow for 1 or more words
//if one word then no need to add the OR keyword
if ($loops==1){
$strSQL.=&quot;'%&quot;.$items.&quot;%'&quot;;
$loops++; //add one to the counter to add in the OR
}else{
$strSQL.=&quot; or body like '%&quot;.$items.&quot;%'&quot;;
} //end the if then loop
} //end the foreach loop

//now echo the statement as a test, if the statement looks
//okay then comment out the echo line

echo $strSQL;
//now run the query and see the results...
if(!$rs_Content = mysql_query($strSQL)) die(&quot;Query died for $strSQL&quot;);

hth Bastien

There are many ways to skin this cat,
but it still tastes like chicken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top