Last active
October 5, 2015 18:03
-
-
Save gseilheimer/50bce3815deb5dab9a72 to your computer and use it in GitHub Desktop.
snippet php for js fnc saveRating()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by IntelliJ IDEA. | |
* User: GS | |
* Date: 13.05.15 | |
* Time: 13:13 | |
*/ | |
// settings for filenames | |
date_default_timezone_set("Europe/Berlin"); // CDT | |
$current_time = date("Y-m-d_H-i-s"); | |
// get the post from the FORM | |
$dataObject = $_POST; //Fetching all posts | |
//Writes it as json to the file, you can transform it any way you want | |
$json = json_encode($dataObject); | |
// filename for json | |
$jsonFilename = "./report/report_json_{$current_time}.txt"; | |
// writes the content in the json-file | |
file_put_contents($jsonFilename, $json); | |
// transform JSON => CSV | |
$jsonFile = file_get_contents($jsonFilename); | |
// If passed a string, turn it into an array | |
if (is_array($jsonArray) === false) { | |
$jsonArray = json_decode($jsonFile, true); | |
} | |
$csvFilename = "./report/report_csv_{$current_time}.csv"; | |
$csvFile = fopen($csvFilename, "w"); | |
$firstLineKeys = false; | |
foreach ($jsonArray as $key => $value) | |
{ | |
$fileContent = $key . "," .$value. ""; | |
fputcsv( $csvFile, array($key, $value) ); | |
echo "array-key : " . $key; | |
echo "array-val : " . $value; | |
echo "\n" . $fileContent; | |
} | |
fclose($csvFilename); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment