I am a php newbie. I have written a small program to compare 2 csv files. I think I have got the 'comparison' part right, but I am stuck on how to display the different records from those 2 files in a nice table format?
file examples -
File 1 -
1000000001,CRUISE,TOM,BOB,MR,10-10-1999,2,2,4,8010,20,9225,41735383093112012,QSM2992 GOLD,200917574KA
1000000002,BIEBER,JUSTIN,ALEX,MR,11-11-1988,1,2,3,7010,20,9226,41535383093112012,QSM2892 GOLD,201017574KA
File 2 -
1000000001,CRUISE,TOM,BOB,MR,10-10-1999,2,2,4,8010,20,9225,41735383093112012,QSM2992 GOLD,200917574KA
1000000004,GATES,BILL,BOB,MR,10-10-1999,2,2,4,8010,20,9225,41735383093112012,QSM2992 GOLD,200917574KA
I want to display the records that are different (which is the second record in this case) like this -
File 1 File 2
ID 1000000001 1000000004
First Name Tom Bill
Last Name Cruise Gate
...etc...
What I have so far -
Populate the 2 multi-dimension arrays using code below
-
Code: Select all
if (($handle = fopen("file1.csv", "r")) !== FALSE) {
# Set the parent multidimensional array key to 0.
$nn1 = 0;
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
# Count the total keys in the row.
$c1 = count($data);
# Populate the multidimensional array.
for ($x=0;$x<$c1;$x++)
{
$csvarray1[$nn1][$x] = $data[$x];
echo "$csvarray1($nn1)($x) = ". $csvarray1[$nn1][$x] . "<br />\n";
echo "<br />\n";
}
$nn1++;
echo "<br />\n";
}
# Close the File.
fclose($handle);
}Code: Select all
$a = 0;
$b = 0;
# start comparing each field in array 1 and array 2
for ($a=0;$a<$nn1;$a++)
{
for($b=0;$b<$c1;$b++)
{
if ( ($csvarray1[$a][$b] != $csvarray2[$a][$b]) )
{
echo "<br /> \n";
echo "File 1: ". "$csvarray1($a)($b) = ".$csvarray1[$a][$b]." is NOT equal to File 2: ". "$csvarray2($a)($b) = ".$csvarray2[$a][$b]."<br /> \n";
echo "<br /> \n";
}
else echo "File 1 " . "$csvarray1($a)($b) is equal to " . "File 2 $csvarray2($a)($b) <br /> \n";
}
}At the moment I am using 'echo' to display, can someone help me to put them in a nice table format?
Thanks in advance.