AasimAzam Posted March 14, 2016 Share Posted March 14, 2016 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 Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted March 15, 2016 Solution Share Posted March 15, 2016 the following implements your logic, saving the output from each input .htm file to a corresponding .csv file - <?php ini_set('display_errors',1); error_reporting(E_ALL); $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' ); $keys = array_flip($stats_wanted); // change stats wanted into keys $source_path = 'directory/'; $dest_path = ''; // enter the path, with trailing /, where you want the destination files to be written foreach (glob("$source_path*.*") as $filename) { echo "Processing: $filename<br>"; $info = pathinfo($filename); // get the ['filename'] $content = json_decode(file_get_contents($filename),true); // read file contents as an array $output = fopen("$dest_path{$info['filename']}.csv",'w'); // create output file fputcsv($output, $stats_wanted); // write header line to file // loop over players in the data foreach($content['playerTableStats'] as $arr){ $arr = array_intersect_key($arr,$keys); // keep only the stats wanted elements fputcsv($output, $arr); // write the data to the file } fclose($output); // close the current file } this makes the same assumption that your code does, that the order of the $stats_wanted elements matches the order that the data elements will exist as. if not, you can throw in a sort operation before writing the data to the file. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.