Help refusing duplicates from database.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tymlls05
Forum Commoner
Posts: 30
Joined: Tue Nov 01, 2005 1:30 pm

Help refusing duplicates from database.

Post by tymlls05 »

I have a contacts list I am trying to export to csv so that users can print mailing labels. Some users have the same contact's names and addresses as other users do. My admin print function generates the csv but i do not want duplicate addresses to show up twice or more.

I am trying to use the following code to do so but it isn't working at all. It still prints the full list.

Code: Select all

        $query  = "SELECT * FROM scml_contacts WHERE NOT address='' AND user_group='".$_SESSION['group']."' ORDER BY lastname ASC";
        $result = mysql_query($query);
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $needle= $row['first'].','.$row['last'].',';
            $pos = strpos($labeldata,$needle);
            if($pos == false) {
                $labeldata = $row['id'].','.$row['firstname'].','.$row['lastname'].','.$row['nickname'].','.$row['address'].','.$row['zipcode'].','.$row['city'].','.$row['country'].','.$row['web'].','.$row['mail'].','.$row['icq'].','.$row['msn'].','.$row['ym'].','.$row['homephone'].','.$row['workphone'].','.$row['mobilephone'].','.$row['comments'].','.$row['user_group'].','.$row['state'].','.$row['day'].','.$row['month'].','.$row['year']."\n";
                fwrite($fh, $labeldata);
            }
        }
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Help refusing duplicates from database.

Post by mikosiko »

You should use a SELECT that eliminate the duplicates first... using DISTINCT...

In another idea that maybe you can consider.... why are you generating a csv to do that?.... If your users have Office or OpenOffice the simplest way to generate that kind of reports (mail labels) is using the Mailmerge option reading the data from the Database... very straight forward.

Miko
tymlls05
Forum Commoner
Posts: 30
Joined: Tue Nov 01, 2005 1:30 pm

Re: Help refusing duplicates from database.

Post by tymlls05 »

Embarrassingly, I have tried unsuccessfully setting up Office to connect to the database for the mail merge. Also, I only want them to have access to certain rows.

I appreciate your response. I will try using the distinct feature in my query and report back.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Help refusing duplicates from database.

Post by mikosiko »

tymlls05 wrote:Embarrassingly, I have tried unsuccessfully setting up Office to connect to the database for the mail merge. Also, I only want them to have access to certain rows.

I appreciate your response. I will try using the distinct feature in my query and report back.
Nothing to be embarrassed.. we always learn in some point...

Just let me know if you want more information to do the configuration... and filter rows is not a problem either.
Post Reply