www.webdeveloper.com
Results 1 to 5 of 5

Thread: Filtering duplicate results

  1. #1
    Join Date
    May 2011
    Posts
    14

    Filtering duplicate results

    Hello,

    I have a pretty simple PHP search function on my website. Basically, it just queries an SQL database I have and spits out the results, which is fine by me.

    One thing has been bothering me, however. The website is for an electronic component company which has tens of thousands of parts. All the parts can have multiple values (i.e., a part can be XYZ001, XYZ002, XYZ003, etc.). My database looks something like this:

    XYZ001 | <link to XYZ product page>
    XYZ002 | <link to XYZ product page>
    XYZ003 | <link to XYZ product page>

    So basically if someone just searches the phrase XYZ, all three results show, even though they all go to the same place. So basically my question is, is there a way to filter the results of the second column so that if there is a duplicate entry, it will hide the whole record, showing only one instead of all three.

    Sorry if this isnt clear, I can offer some more clarification if need.

    Regards,
    Andrew

  2. #2
    Join Date
    Oct 2005
    Location
    Cambridgeshire, UK
    Posts
    191
    Hi Andrew,

    Your probably best doing this at the MySQL level aren't you in the original query? For example:

    Code:
    SELECT `productName`, `productURL` FROM `product` WHERE `productName` LIKE '&#37;XYZ%' GROUP BY `productURL`
    Note the GROUP BY Hope that's what you mean
    BIOSTALL.com
    - Freelancing Services
    - Web Development Snippets, Hints and Tips

  3. #3
    Join Date
    Jul 2003
    Location
    Lansdale, PA
    Posts
    88
    If you don't handle it in mysql, you can simply track the last "item" you saw in the loop, then if the current item is equal to the last one, just skip it (don't print it out).

    pseudo code:

    PHP Code:
    $last "";
    while(
    $row mysql_fetchrow_array()) {
      if(
    $last != $row['id']) {
        
    // print out result
      
    }

      
    $last row['id'];
      


  4. #4
    Join Date
    May 2011
    Posts
    14
    Thank you BIOSTALL for your reply!

    This seems like a good solution, but I think my syntax might be off…I’m pretty new to MySQL.

    Here is an example of something I found, though, while experimenting with your code. I managed to organize the column by its unique product name, where ROWS is the number of similar rows and lname is the name of the product I’m trying to filter.


    ROWS | lname

    12 | <a href=”xyz001.html”>Product #1 Page</a>
    41 | <a href=”xyz002.html”>Product #2 Page</a>
    75 | <a href=”xyz003.html”>Product #3 Page</a>

    So basically, Product #1 (xyz001) has 2 similar values, Product #2 has 41 similar values, and Product #3 has 116 similar values.


    The column with the similar value is ‘fname’ and the column with the unique values is ‘lname’.

    Regards,
    Andrew

  5. #5
    Join Date
    May 2011
    Posts
    14
    Many thanks to both of you! I got it working

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles