Created
March 16, 2017 12:05
-
-
Save yogasukma/50cc1a13995fed0e0dd911e8c55a2769 to your computer and use it in GitHub Desktop.
Parsing CSV and store it using active record
This file contains hidden or 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 | |
// define the path | |
$filePath = "data.csv"; | |
// check if we can read the file | |
if (($handle = fopen($filePath, "r")) !== FALSE) { | |
// do looping until no more line on csv | |
while (($data = fgetcsv($handle)) !== FALSE) { | |
// insert into database | |
$news = new News(); | |
$news->title = $data[0]; | |
$news->content = $data[1]; | |
$news->save(); | |
} | |
fclose($handle); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment