I have about 1000+htm files they have data like the attached files. I have the following code that converts this data into a csv file. I then wanted to convert the whole folder into csv files so I have the following code
<?php
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
ini_set('display_errors',1);
error_reporting(E_ALL);
// have to put files in another folder that does not include this script
foreach (glob("directory/*.*") as $filename) {
$dump = file_get_contents($filename);
$stats_wanted = array('playedPositionsShort',
'name',
'playerId',
'age',
'rating',
'shotOnTarget',
'shotsTotal',
'penaltyScored',
'penaltyMissed',
'passLongBallAccurate',
'passLongBallTotal',
'passTotal',
'passCrossAccurate',
'passCrossTotal',
'passThroughBallTotal',
'passThroughBallAccurate',
'keyPassTotal',
'dribbleWon',
'dribbleTotal',
'tackleWon',
'tackleLost',
'tackleWonTotal',
'tackleTotalAttempted',
'challengeLost',
'interceptionLost',
'penaltyTaken',
'interceptionAll',
'clearanceTotal',
'offsideGiven',
'offsideProvoked',
'foulGiven',
'foulCommitted',
'dispossessed',
'duelAerialWon',
'duelAerialLost',
'duelAerialTotal',
'touches',
'totalPasses',
'offsideWonPerGame',
'offsideGivenPerGame',
'passSuccessInMatch'
);
$output = json_decode($dump);
foreach($output->playerTableStats as $o){
foreach($o as $key=>$value){
if(!in_array($key, $stats_wanted)){
unset($o->$key);
}
}
$new[$o->name] = $o;
}
$output = fopen('php://output', 'w');
fputcsv($output, $stats_wanted);
foreach ($new as $n) {
foreach($n as $a){
$ar[] = $a;
}
fputcsv($output, $ar);
unset($ar);
}}
?>
This script does export a csv file which I can save, that file has ALL the data of every file in the folder that was processed, however when I open it, the data is all wrong. Its all mixed up. I tested the above
1 file at a time = no errors
2+ files at a time = jumbled up. What I want to do is convert each of the htm files I have to individual csv files in one batch. I also want the file name to match the htm file name. I don't know if that is possible with amendments to this script or would I need a totally new script? Any help on this matter would be very much appreciated, I have been banging on this for more than a week now.
BLIGA001.htm
BLIGA002.htm
BLIGA003.htm
BLIGA004.htm
BLIGA005.htm