mumunotes

  • Home
  • Contact Us

Wednesday, 2 September 2015

Import Data from CSV File to Database Without Extension in Yii2

 mumunotesss     10:27     Yii, Yii2     No comments   

Hello everybody, Assalamualaikum Wr. Wb. Nearly 1 month I don't publish article. Now, I publish an article about "Import Data from CSV File to Database Without Extension in Yii2".
Let's go.
First, create a model class with name CsvForm.php in models folder and add the code below to it.
<?php

namespace app\models;

use Yii;
use yii\base\Model;

class CsvForm extends Model{
    public $file;
   
    public function rules(){
        return [
            [['file'],'required'],
            [['file'],'file','extensions'=>'csv','maxSize'=>1024 * 1024 * 5],
        ];
    }
   
    public function attributeLabels(){
        return [
            'file'=>'Select File',
        ];
    }
}
Then, open your SiteController.php file and change with following the content.
public function actionUpload(){
    $model = new CsvForm;
   
    if($model->load(Yii::$app->request->post())){
        $file = UploadedFile::getInstance($model,'file');
        $filename = 'Data.'.$file->extension;
        $upload = $file->saveAs('uploads/'.$filename);
        if($upload){
            define('CSV_PATH','uploads/');
            $csv_file = CSV_PATH . $filename;
            $filecsv = file($csv_file);
            print_r($filecsv);
            foreach($filecsv as $data){
                $modelnew = new Mahasiswa;
                $hasil = explode(",",$data);
                $nim = $hasil[0];
                $nama = $hasil[1];
                $jurusan = $hasil[2];
                $angkatan = $hasil[3];
                $alamat = $hasil[4];
                $foto = $hasil[5];
                $modelnew->nim = $nim;
                $modelnew->nama = $nama;
                $modelnew->jurusan = $jurusan;
                $modelnew->angkatan = $angkatan;
                $modelnew->alamat = $alamat;
                $modelnew->foto = $foto;
                $modelnew->save();
            }
            unlink('uploads/'.$filename);
            return $this->redirect(['site/index']);
        }
    }else{
        return $this->render('upload',['model'=>$model]);
    }
}
Create a view file in site folder as upload.php and add this content
<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

?>

<?php $form = ActiveForm::begin(['options' => ['enctype'=>'multipart/form-data']]); ?>
    <?= $form->field($model,'file')->fileInput() ?>
   
    <div class="form-group">
        <?= Html::submitButton('Save',['class'=>'btn btn-primary']) ?>
    </div>
<?php ActiveForm::end(); ?>

Don't forget to create uploads folder in web folder and add menu to access site/upload url.
Create CSV file and input like table format on database. Ex : I use mahasiswa table. This image is mahasiswa table format :

Then create CSV file with sequence like mahasiswa table in database.

Well, now it's time to try it. Access site/uload url on your application!

Browse to CSV file.
Click save button to import data.
Please check in your table to view the result.


It's easy, isn't? Now you can implement this method to your application project. So good luck. Thank you. Wassalamualaikum Wr. Wb.
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Posts Older Posts Home

About Me

mumunotesss
View my complete profile

Popular Posts

  • Change password with validation and repeat password on Yii2
    Hello everybody. I could not post an article because I didn't have new knowledge to share. Ok, this day I will share about "...
  • Import Data from CSV File to Database Without Extension in Yii2
    Hello everybody, Assalamualaikum Wr. Wb. Nearly 1 month I don't publish article. Now, I publish an article about " Import Data fro...
  • Membuat login Yii2 dengan Database
    Selamat pagi guys. Sorry beberapa hari kamaren sibuk. Jadi belum sempet update blog lagi. Mumpung kali ini sempet, hari ini mau berbagi te...
  • Membuat fungsi CRUD dengan Gii Yii2
    Selamat pagi guys. Setelah kemaren kita membuat login dari database, hari ini kita akan membuat CRUD (create, read, update, delete) dengan ...

Blog Archive

  • ▼  2015 (16)
    • ►  November (1)
    • ▼  September (1)
      • Import Data from CSV File to Database Without Exte...
    • ►  August (1)
    • ►  June (8)
    • ►  May (5)

Translate

Copyright © mumunotes
Distributed By My Blogger Themes | Blogger Theme By NewBloggerThemes