Hi,
I am writing a code to take the data from 2 text files and output it to a table. I need to take only one of the columns from the second file only if the value in the first column matches the value in the first file.
e.g.
Text file 1:
data1, data2, data3
123, abc, def
456, ghi, jkl
789, mno, pqr
Text file 2:
data4, data5, data6
987, mno, jkl
654, abc, pqr
321, ghi, def
output all of text file 1, and the data from data6 if data2=data5.
Here is the code that jpadie already helped me out with, but I cant get it to work!!
<?
$fh1 = fopen("fileone.txt", 'r'); //the 'r' argument specifies that the file will be opened for read only
$fh2= fopen("filetwo.txt", 'r');
//parse the first file
while ($var1[] = fgetcsv($fh1,1024,"\t")) //assume this is the lookup file
{
// bad coding but dont need to do anything
}
//now have an array or arrays which need cleaning up
foreach ($var1 as $key=>$val)
{
$datafromfileone[$val[0]]=array("data2"=>$val[1],"data3"=>$val[2]);
}
unset($var1);
//parse the second file
while ($var2[] = fgetcsv($fh2,1024,"\t")) //assume this is the lookup file
{
// ditto
}
//transform $var2 into an easy lookup
foreach ($var2 as $key=>$val)
{
$datafromfiletwo[$val[1]]["datafromfileone"] = $val[1]; //this takes the second col and assigns it to the earlier variable with the ssame key as in the second col.
}
unset ($var2); //destroy the variable to free memory
//close the csv files
fclose($fh1);
fclose($fh2);
//assuming this is a standard vlookup with a single input column
// you can now lookup each item in the first file and find its corresponding value using something like this:
//you could also choose just to read the first file now and perform the lookup on the incoming data
echo "<table border=\"1\">";
echo "<tr>";
echo "<td>data1</td>";
echo "<td>data2</td>";
echo "<td>data5</td>";
echo"</tr>";
foreach ($datafromfileone as $key=>$val)
{
extract($val);
echo "<tr>";
echo "<td>$key</td>";
echo "<td>$data1</td>";
echo "<td>$data2</td>";
echo "<td>$data5</td>";
echo"</tr>";
}
echo "<table>";
?>
If you have read this far then thanks!! I know its long, but theres a lot to explain.
TIA
Mattie P
I am writing a code to take the data from 2 text files and output it to a table. I need to take only one of the columns from the second file only if the value in the first column matches the value in the first file.
e.g.
Text file 1:
data1, data2, data3
123, abc, def
456, ghi, jkl
789, mno, pqr
Text file 2:
data4, data5, data6
987, mno, jkl
654, abc, pqr
321, ghi, def
output all of text file 1, and the data from data6 if data2=data5.
Here is the code that jpadie already helped me out with, but I cant get it to work!!
<?
$fh1 = fopen("fileone.txt", 'r'); //the 'r' argument specifies that the file will be opened for read only
$fh2= fopen("filetwo.txt", 'r');
//parse the first file
while ($var1[] = fgetcsv($fh1,1024,"\t")) //assume this is the lookup file
{
// bad coding but dont need to do anything
}
//now have an array or arrays which need cleaning up
foreach ($var1 as $key=>$val)
{
$datafromfileone[$val[0]]=array("data2"=>$val[1],"data3"=>$val[2]);
}
unset($var1);
//parse the second file
while ($var2[] = fgetcsv($fh2,1024,"\t")) //assume this is the lookup file
{
// ditto
}
//transform $var2 into an easy lookup
foreach ($var2 as $key=>$val)
{
$datafromfiletwo[$val[1]]["datafromfileone"] = $val[1]; //this takes the second col and assigns it to the earlier variable with the ssame key as in the second col.
}
unset ($var2); //destroy the variable to free memory
//close the csv files
fclose($fh1);
fclose($fh2);
//assuming this is a standard vlookup with a single input column
// you can now lookup each item in the first file and find its corresponding value using something like this:
//you could also choose just to read the first file now and perform the lookup on the incoming data
echo "<table border=\"1\">";
echo "<tr>";
echo "<td>data1</td>";
echo "<td>data2</td>";
echo "<td>data5</td>";
echo"</tr>";
foreach ($datafromfileone as $key=>$val)
{
extract($val);
echo "<tr>";
echo "<td>$key</td>";
echo "<td>$data1</td>";
echo "<td>$data2</td>";
echo "<td>$data5</td>";
echo"</tr>";
}
echo "<table>";
?>
If you have read this far then thanks!! I know its long, but theres a lot to explain.
TIA
Mattie P