Skip to content

Instantly share code, notes, and snippets.

@sohag-pro
Created April 12, 2022 06:12
Show Gist options
  • Save sohag-pro/4d8a299c143e697aa1e431f1e3632f31 to your computer and use it in GitHub Desktop.
Save sohag-pro/4d8a299c143e697aa1e431f1e3632f31 to your computer and use it in GitHub Desktop.
Generate SQL from CSV
<?php
$file = fopen( 'Countries-IT.csv', 'r' );
$csv = [];
while ( ( $line = fgetcsv( $file ) ) !== FALSE ) {
$csv[] = $line;
}
fclose( $file );
print_r( $csv[0] );
$sql = "DELETE FROM translations WHERE table_name = 'countries' AND locale = 'it';" . PHP_EOL;
foreach ( $csv as $row ) {
$id = $row[0];
$value = $row[3];
if( $id == 'ID' ) {
continue;
}
if( $value == '' ) {
continue;
}
$value = str_replace( "'", "\'", $value );
$sql .= "INSERT INTO `translations`(`table_name`, `column_name`, `foreign_key`, `locale`, `value`, `created_at`, `updated_at`) VALUES ('countries','name','$id','it','$value','2022-04-12 00:00:00','2022-04-12 00:00:00');" . PHP_EOL;
}
file_put_contents( 'countries_it.sql', $sql . PHP_EOL );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment