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.
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.
Let's go.
First, create a model class with name CsvForm.php in models folder and add the code below to it.
<?phpThen, open your SiteController.php file and change with following the content.
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',
];
}
}
public function actionUpload(){Create a view file in site folder as upload.php and add this content
$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]);
}
}
<?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.
0 comments:
Post a Comment