Jump to content

removing duplicates (not so easy)


Muncher

Recommended Posts

hey everyone,

 

sample data

+---+--------------+

| 1  +  mouse food  +

+---+--------------+

| 2  +  mouse pad    +

+---+--------------+

 

 

i have this query that i use from a search feature on my website. basically when someone searches for "mouse pad" it breaks up the phrase (using explode) into "mouse" and "pad" and runs a query for each word.

 

so of course anything that is "mouse" or "pad" are the results, and they end up looking something like this

 

Results for "Mouse"

1 mouse food

2 mouse pad

 

Results for "Pad"

2 mouse pad

 

so as you can see it duplicated 'mouse pad' because it contains more than one of the key words being searched for (mouse, pad). how do i compare the two query results and eliminate duplicates?

 

here is the actuall query (nested inside a 'foreach' of the exploded search phrase).

 

ABOUT THE VARIABLES IN MY EXAMPLE

$findThis: is the search word/phrase that a user would enter.

$searchBy: is a reference to the product category chosen by the user when searching.

'description2' and 'search' are columns in my data table that are being searched for the key words.

 

 

//TRIM THE SEARCH PHRASE INTO INDIVIDUAL WORDS

$trimmedfindThis = explode(" ",$findThis);

 

//SEARCH FOR EACH INDIVIDUAL WORD ONE AT A TIME

foreach ($trimmedfindThis as $trimm){

  $query="SELECT *

              FROM products

WHERE search_cat = '$searchBy'

AND LOWER(description2)

LIKE LOWER('%".$trimm."%')

           

              UNION

 

              SELECT *

              FROM products

WHERE search_cat = '$searchBy'

AND LOWER(search)

LIKE LOWER('%".$trimm."%')

ORDER BY name

";

 

  $result = mysql_query($query);

  $num_results = mysql_num_rows($result);

 

//DISPLAYING THE RESULTS, I USE THIS FOR STATEMENT

for ($i=0; $i <$num_results; $i++){

$row = mysql_fetch_array($result);

 

FOR THIS POST I CUT OUT THE DISPLAY CODE. DISPLAYING IS NO PROB IM JUST SHOWING DUPLICATE RESULTS

}

 

there doesnt seem to be anything that i can add to the query, like DISTINCT or UNIQUE because the query is returning the proper results for each key word. i need to be able to compare the two queries and eliminate duplicates.

 

thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/134093-removing-duplicates-not-so-easy/
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.