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.
Code: Select all
data1, data2, data3
123, abc, def
456, ghi, jkl
789, mno, pqrCode: Select all
data4, data5, data6
987, mno, jkl
654, abc, pqr
321, ghi, defHere is the code that I already have, but I cant get it to work!!
Code: Select all
<?
$fh1 = fopen("e;fileone.txt"e;, 'r'); //the 'r' argument specifies that the file will be opened for read only
$fh2= fopen("e;filetwo.txt"e;, 'r');
//parse the first file
while ($var1ї] = fgetcsv($fh1,1024,"e;\t"e;)) //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("e;data2"e;=>$valї1],"e;data3"e;=>$valї2]);
}
unset($var1);
//parse the second file
while ($var2ї] = fgetcsv($fh2,1024,"e;\t"e;)) //assume this is the lookup file
{
// ditto
}
//transform $var2 into an easy lookup
foreach ($var2 as $key=>$val)
{
$datafromfiletwoї$valї1]]ї"e;datafromfileone"e;] = $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 "e;<table border=\"e;1\"e;>"e;;
echo "e;<tr>"e;;
echo "e;<td>data1</td>"e;;
echo "e;<td>data2</td>"e;;
echo "e;<td>data5</td>"e;;
echo"e;</tr>"e;;
foreach ($datafromfileone as $key=>$val)
{
extract($val);
echo "e;<tr>"e;;
echo "e;<td>$key</td>"e;;
echo "e;<td>$data1</td>"e;;
echo "e;<td>$data2</td>"e;;
echo "e;<td>$data5</td>"e;;
echo"e;</tr>"e;;
}
echo "e;<table>"e;;
?>TIA
Mattie P
feyd | Please use
Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]