Created
April 12, 2022 06:12
-
-
Save sohag-pro/4d8a299c143e697aa1e431f1e3632f31 to your computer and use it in GitHub Desktop.
Generate SQL from CSV
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 | |
$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