Skip to content

Instantly share code, notes, and snippets.

@yogasukma
Created March 16, 2017 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yogasukma/50cc1a13995fed0e0dd911e8c55a2769 to your computer and use it in GitHub Desktop.
Save yogasukma/50cc1a13995fed0e0dd911e8c55a2769 to your computer and use it in GitHub Desktop.
Parsing CSV and store it using active record
<?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