Page 1 of 1

Help refusing duplicates from database.

Posted: Mon Mar 08, 2010 2:32 pm
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);
            }
        }

Re: Help refusing duplicates from database.

Posted: Mon Mar 08, 2010 5:43 pm
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

Re: Help refusing duplicates from database.

Posted: Tue Mar 09, 2010 10:21 am
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.

Re: Help refusing duplicates from database.

Posted: Tue Mar 09, 2010 5:38 pm
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.