Jump to content

How do I automate this script to create individual CSV files?


AasimAzam
Go to solution Solved by mac_gyver,

Recommended Posts

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

Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.