How would I not print a line that has duplicate data? I want to remove duplicate data based on the name column.
Here's an example (John):
(TEACHER,AGE,SUBJECT,NAME)
MS James,10,math,john
MS James,10,math,jamie
MS James,9,english,judy
MS James,9,english,john
MS James,10,english,jordan
MS James,9,english,david
MR Henington,10,english,tina
MR Henington,9,math,mike
MR Henington,9,math,mathew
would be:
MS James,10,math,john
MS James,10,math,jamie
MS James,9,english,judy
MS James,10,english,jordan
MS James,9,english,david
MR Henington,10,english,tina
MR Henington,9,math,mike
MR Henington,9,math,mathew
The code I'm using is provided by Nogdog:
<?php
$fh = fopen('test.csv', 'r');
if($fh)
{
$thisRow = 1;
echo "<table>\n";
$data = array();
while(($line = fgetcsv($fh, 1024)) !== FALSE)
{
if((int)$line[1] != 10)
{
continue; // skip this one, wrong age
}
$data[] = $line;
}
usort($data, create_function('$a, $b', 'return(strcmp($a[3],$b[3]));'));
foreach($data as $line)
{
$bg = ($thisRow++ % 2) ? "#E9F5D6" : "#DEF0C2" ;
printf(
"<tr style='background-color: %s'>\n" .
"<td>Hi I'm %s and I'm %d years old.\n" .
"My favorite subject is %s.</td>\n</tr>\n",
$bg,
ucwords($line[3]),
(int)$line[1],
$line[2]
);
}
echo "</table>\n";
}
else
{
echo "<p style='color: red'>ERROR: unable to read CSV fiel.</p>\n";
}
?>