mumunotes

  • Home
  • Contact Us

Thursday, 5 November 2015

Export Data to PDF with MPDF Extension

 mumunotesss     10:12     Yii, Yii2     No comments   

Hello every body, Assalamualaikum Wr. Wb.
Nearly 1 month, i don't share anything in my blog. And this section, i will post about "Export Data to PDF with MPDF Extension". Ok, let's begin.
First, please open composer.json file and change with following this content.
others code
"require": {
        "php": ">=5.4.0",
        "yiisoft/yii2": ">=2.0.4",
        "yiisoft/yii2-bootstrap": "*",
        "yiisoft/yii2-swiftmailer": "*",
"himiklab/yii2-recaptcha-widget" : "*",
"abhi1693/yii2-sms": "2.0.0",
"robregonm/yii2-pdf": "*",
"mpdf/mpdf":"*",
         },
others code
Then, please update your composer.json. A new vendor(mpdf) has instaled.
Create a new file as main1.php in views/layouts and add this code.
<?php
use yii\helpers\Html;
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
<meta charset="<?= Yii::$app->charset ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?= Html::csrfMetaTags() ?>
<title><?= Html::encode($this->title) ?></title>
<?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>
<div class="wrap">
<div class="container">
<?= $content ?>
</div>
</div>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
Create a new file as template.php in views/site and add this code.
<?php
use yii\helpers\Html;
use yii\grid\GridView;
?>
<style type="text/css">
table {
*border-collapse: collapse; /* IE7 and lower */
border-spacing: 0;
width: 100%;  
}
.zebra td, .zebra th {
padding: 10px;
border-bottom: 1px solid #f2f2f2;  
}

.zebra tbody tr:nth-child(even) {
background: #f5f5f5;
-webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
-moz-box-shadow:0 1px 0 rgba(255,255,255,.8) inset;
box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;      
}

.zebra th {
text-align: left;
text-shadow: 0 1px 0 rgba(255,255,255,.5);
border-bottom: 1px solid #ccc;
background-color: #eee;
background-image: -webkit-gradient(linear, left top, left bottom, from(#f5f5f5), to(#eee));
background-image: -webkit-linear-gradient(top, #f5f5f5, #eee);
background-image:    -moz-linear-gradient(top, #f5f5f5, #eee);
background-image:     -ms-linear-gradient(top, #f5f5f5, #eee);
background-image:      -o-linear-gradient(top, #f5f5f5, #eee);
background-image:         linear-gradient(top, #f5f5f5, #eee);
}

.zebra th:first-child {
-moz-border-radius: 6px 0 0 0;
-webkit-border-radius: 6px 0 0 0;
border-radius: 6px 0 0 0;
}

.zebra th:last-child {
-moz-border-radius: 0 6px 0 0;
-webkit-border-radius: 0 6px 0 0;
border-radius: 0 6px 0 0;
}

.zebra th:only-child{
-moz-border-radius: 6px 6px 0 0;
-webkit-border-radius: 6px 6px 0 0;
border-radius: 6px 6px 0 0;
}

.zebra tfoot td {
border-bottom: 0;
border-top: 1px solid #fff;
background-color: #f1f1f1;
}

.zebra tfoot td:first-child {
-moz-border-radius: 0 0 0 6px;
-webkit-border-radius: 0 0 0 6px;
border-radius: 0 0 0 6px;
}

.zebra tfoot td:last-child {
-moz-border-radius: 0 0 6px 0;
-webkit-border-radius: 0 0 6px 0;
border-radius: 0 0 6px 0;
}

.zebra tfoot td:only-child{
-moz-border-radius: 0 0 6px 6px;
-webkit-border-radius: 0 0 6px 6px
border-radius: 0 0 6px 6px
}
</style>
<table class="zebra" style="margin:auto; width:100%;">
<thead>
<tr>
<th>No</th>
<th>NIM</th>
<th>NAMA</th>
<th>JURUSAN</th>
<th>ANGKATAN</th>
<th>ALAMAT</th>
<th>FOTO</th>
</tr>
</thead>
<?php
$i=1;
foreach($model as $data){
echo '
<tr>
<td>'.$i.'</td>
<td>'.$data['nim'].'</td>
<td>'.$data['nama'].'</td>
<td>'.$data['jurusan'].'</td>
<td>'.$data['angkatan'].'</td>
<td>'.$data['alamat'].'</td>
<td><img src="'.Yii::$app->request->baseUrl.'/uploads/'.$data['foto'].'" width="100px"></td>
</tr>
';
$i++;
}
?>
</table>

Oke, next open your SiteController.php file and create a new action as Exportpdf like this code.
public function actionExportpdf() {
$this->layout='main1';
$model = Mahasiswa::find()->All();
$mpdf=new mPDF();
$mpdf->WriteHTML($this->renderPartial('template',['model'=>$model]));
$mpdf->Output('MyPDF.pdf', 'D');
exit;
}
And the last, add url ('/site/exportpdf') to your menu.
Well, now it's time to try it. Access site/exportpdf url on your application!


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

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

Tuesday, 11 August 2015

Export Data to Excel Without Extension on Yii2

 mumunotesss     12:59     Yii, Yii2     No comments   

Assalamualaikum Wr. Wb. Hello everybody. For along times i dont publish article. And now i will share to you all about export data table to excel file without extension.
Okay, lets go.
the first, add menu with url to /site/export in main.php.
Second, open SiteController file and change this code to it.
public function actionExport(){
    $model = Mahasiswa::find()->All();
    $filename = 'Data-'.Date('YmdGis').'-Mahasiswa.xls';
    header("Content-type: application/vnd-ms-excel");
    header("Content-Disposition: attachment; filename=".$filename);
    echo '<table border="1" width="100%">
        <thead>
            <tr>
                <th>Mim</th>
                <th>Nama</th>
                <th>Jurusan</th>
                <th>Angkatan</th>
                <th>Alamat</th>
                <th>Foto</th>
            </tr>
        </thead>';
        foreach($model as $data){
            echo '
                <tr>
                    <td>'.$data['nim'].'</td>
                    <td>'.$data['nama'].'</td>
                    <td>'.$data['jurusan'].'</td>
                    <td>'.$data['angkatan'].'</td>
                    <td>'.$data['alamat'].'</td>
                    <td><img src="'.Yii::$app->request->baseUrl.'/uploads/'.$data['foto'].'" width="100px"></td>
                </tr>
            ';
        }
    echo '</table>';

}
Well, now it's time to try it. Access your application!

Then click on menu export.
 result of export excel.

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
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)
      • Export Data to PDF with MPDF Extension
    • ►  September (1)
    • ►  August (1)
    • ►  June (8)
    • ►  May (5)

Translate

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