Files
cdnPRO/millihome.pl/index.php

65 lines
1.8 KiB
PHP

<?
function convertToJson($input) {
// Usunięcie klamer
$input = trim($input, "{}");
// Zamiana kluczy na poprawny format JSON (dodanie cudzysłowów wokół kluczy)
$input = preg_replace('/(\d+):/', '"$1":', $input);
// Dodanie nawiasów klamrowych, aby uzyskać poprawny JSON
$json = "{".$input."}";
return $json;
}
$content = file_get_contents( 'export.csv' );
$lines = explode( "\n", $content );
$header = explode( ',', array_shift( $lines ) );
for ( $i = 1; $i < count( $lines ); $i++ ) {
$data = str_getcsv( $lines[$i] );
$json_field = $data[7] ?? null;
if ( $json_field ) {
$json_data = json_decode( $json_field, true );
if ( json_last_error() === JSON_ERROR_NONE ) {
$json_data['rate'] = convertToJson( $json_data['rate'] );
$rate = json_decode( $json_data['rate'], true );
if ( $rate ) {
$json_data['rate'] = $rate;
}
$data[7] = $json_data;
}
}
$rows[] = array_combine( $header, $data );
}
echo '<pre>';
print_r( $rows[22] );
foreach ( $rows as $row ) {
// csv z ocenami zamówień
unset( $order_csv_line );
$order_csv_line['order_id'] = ++$z;
$order_csv_line['UNIX_TIMESTAMP'] = strtotime( $row['created_at'] );
$order_csv_line['RATING'] = str_replace( 'Ocena sklepu::', '', $row['rate2'] );
$order_csv_line['TEXT_REVIEW'] = $row['opinion'];
if ( $order_csv_line['RATING'] )
$order_csv_lines[] = $order_csv_line;
// csv z ocenami produktów
unset( $product_csv_line );
}
unlink( 'order.csv' );
$csv_file = fopen( 'order.csv', 'w' );
fputcsv( $csv_file, array_keys( $order_csv_lines[0] ) );
foreach ( $order_csv_lines as $line ) {
fputcsv( $csv_file, $line );
}
fclose( $csv_file );
// show url to csv
echo '<a href="https://cdn.projectpro.pl/millihome.pl/order.csv">Pobierz CSV</a>';