I wrote some code to select duplicates and group them using first and last names. I gather them into a multidimensional array and dedupe/merge them using jQuery/Ajax on the resulting page. I would like to ask if there is a better method of creating the array than how I'm doing it. Here is my code. Thank you.
$dataArr=fetchDups($conn, 13, 5); // get a few at a time
print '<div style="clear:both;"></div><pre>';
print_r($dataArr);
print '</pre><div style="clear:both;"></div>';
function fetchDups($conn, $client_id, $limit='')
{
$sql=' SELECT * FROM `contacts` WHERE `clientid`=\'13\' GROUP BY fname, lname ';
//$sql=' SELECT DISTICT fname, lname, * FROM `clients` WWHERE `clientid`=\'13\' ';
$res=mysql_query($sql, $conn)or die(mysql_error());
$contactsRow=array();
while($row=mysql_fetch_array($res)){
echo $row['fname'].'<br>';
$contactsRow[]=$row;
}
mysql_freeresult($res);
$dataArr=array();
$i=0;
$limitNum=0;
//----------------------------------
foreach($contactsRow AS $rowNew){
$sql=' SELECT * FROM `contacts` WHERE `clientid`=\'13\' AND `id`!=\''.$rowNew['id'].'\'
AND (`fname` = \''.$rowNew['fname'].'\' OR `lname` = \''.$rowNew['lname'].'\')
';
//echo $sql;
$res=mysql_query($sql, $conn)or die(mysql_error());
$rowCountDup=mysql_num_rows($res);
if($rowCountDup>0){
$d=0;
$dataArr[$i]=array();
$dataArr[$i][$d]=$rowNew;
while($rowNew=mysql_fetch_array($res)){
$dataArr[$i][($d+1)]=$rowNew;
$d++;
}
$i++;
$limitNum++;
}
// limit the results. too many crashes the browser
if($limitNum==$limit){
break;
}
}
mysql_freeresult($res);
return $dataArr;
}