user3258571
user3258571

Reputation: 384

Duplicates Records in Loop

I currently have a loop that I am trying to use to echo email addresses based on whether a profile pic is not in a directory /pics. How can I remove duplicates so it only lists the Email once?

<?php 

while ($row_Recordset9 = mysql_fetch_assoc($Recordset9)) {
    $ID = $row_Recordset9['ID'];
    $image = '../pics/' . $ID . '.jpg';  
    if (!file_exists($image)) {
        echo $row_Recordset9['Email'].', ';         
    }
}
?>

Upvotes: 0

Views: 29

Answers (2)

mister martin
mister martin

Reputation: 6252

You could store results into a temporary array and check against that:

$temp = array();
while ($row_Recordset9 = mysql_fetch_assoc($Recordset9)) {
    $ID = $row_Recordset9['ID'];
    $image = '../pics/' . $ID . '.jpg';
    $email = $row_Recordset9['Email'];
    if (!file_exists($image) && !in_array($email, $temp)) {
        $temp[] = $email;
        echo $email.', ';
    }
}

Upvotes: 1

Barmar
Barmar

Reputation: 780714

Put the emails in an array, then use array_unique() to remove duplicates.

$emails = array();
while ($row_Recordset9 = mysql_fetch_assoc($Recordset9)) {
    $ID = $row_Recordset9['ID'];
    $image = '../pics/' . $ID . '.jpg';  
    if (!file_exists($image)) {
        $emails[] = $row_Recordset9['Email'];       
    }
}
$addresses = implode(',', array_unique($emails));
echo $addresses;

Upvotes: 2

Related Questions