user3258571 user3258571 - 20 days ago 5
MySQL Question

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'].', ';
}
}
?>

Answer

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;
Comments