Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CONTOH PREVIEW DATA EXCELL SEBELUM SAVE/UPDATE DATABASE
- (1) LOGIC
- function index()
- {
- if ($this->session->userdata('url')=='')
- {
- $data = array();
- $data['title'] = 'Kategori Barang';
- $data['ktgr_brng'] = $this->m_all->semua($this->table);
- $data['subktgr']=$this->m_all->getList($this->table, $this->primary, null, null);
- $this->template->display('ktgr_brng/index',$data);
- }
- else redirect($this->session->userdata('url'));
- }
- function preview()
- {
- if(isset($_REQUEST['func']) && $_REQUEST['func']=="uploadfile")
- {
- $this->fileupload($_FILES);
- }
- }
- function fileupload($file_array)
- {
- /*BEGIN FILE UPLOADING CODE */
- $uploaded_files = array();
- if(isset($file_array['input01']) && $file_array['input01']['name']!="")
- {
- $tmp = @explode(".",$file_array['input01']['name']);
- $currentfile_extension = end($tmp);
- if ($currentfile_extension == 'xlsx')
- {
- $filename = "data/ktgr_brng/".date("YmdHis").rand(1000,9999).".".$currentfile_extension;
- if(@move_uploaded_file($_FILES['input01']['tmp_name'], $filename))
- {
- $status= "sukses";
- $pesan = "File has been updated successfully.";
- $this->session->set_userdata('url',"ktgr_brng/preview_excell");
- $this->session->set_userdata('filename', $filename);
- //$response['url'] = site_url("user_ktr/preview_excell");
- } else {
- $status = "gagal";
- $pesan = "Error in file uploading.";
- }
- }
- else {
- $status= "gagal";
- $pesan = "Format Excell 2007.";
- }
- }
- /*END FILE UPLOADING CODE */
- $response = array('status'=>$status, 'pesan'=>$pesan);
- echo json_encode($response);
- }
- function preview_excell()
- {
- if ($this->session->userdata('filename') != '')
- {
- $this->session->unset_userdata('url');
- $excelreader = new PHPExcel_Reader_Excel2007();
- $loadexcel = $excelreader->load($this->session->userdata('filename')); // Load file yang tadi diupload ke folder tmp
- $sheet = $loadexcel->getActiveSheet()->toArray(null, true, true ,true);
- //$sheet = $this->auth->get_sheet($this->session->userdata('filename'));
- $data['sheet'] = $sheet;
- $data['title'] = 'Preview Data Excell';
- $this->template->display('ktgr_brng/preview_excell',$data);
- }
- else redirect('ktgr_brng/index');
- }
- (2) JQUERY/AJAX (index.php)
- $('#btn-import').on("click", function(e){
- $('#form-import')[0].reset(); // reset form on modals
- $('.modal-title').text('Import Data');
- $('#modal-import').modal('show');
- });
- $('#input01').on('change',function(){
- if(confirm("File sudah benar ?")) {
- //$(this).after('<span class="temp-message">File Uploading.....</span>');
- $.ajax({
- url:"<?php echo site_url('ktgr_brng/preview?func=uploadfile')?>",
- type:"POST",
- data:new FormData($("#form-import")[0]),
- enctype: 'multipart/form-data',
- contentType:false,
- processData:false,
- dataType: 'json',
- success:function(response){
- $('#form-import').on('hidden.bs.modal', function (e) {
- $(this)
- .find("input")
- .val('')
- .end()
- })
- if (response.status == 'sukses')
- {
- $("#form-import").modal('hide');
- location.href = location.pathname;
- }
- else
- {
- alert(response.pesan);
- location.href = location.pathname;
- }
- },
- error: function (jqXHR, textStatus, errorThrown)
- {
- alert(xhr.responseText);
- }
- });
- }
- else
- {
- $('#input01').val('');
- }
- });
- (3) VIEW PREVIEW_EXCELL
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
- <div class="page-title">
- </div>
- <div class="clearfix"></div>
- <br />
- <div class="row">
- <div class="col-md-12 col-sm-12 col-xs-12">
- <div class="x-panel">
- <div class="x_title">
- <h2><?php echo $title?></h2>
- </div>
- </div>
- <div class="x_content">
- <div id="view">
- <table id="pre-excell" class="table table-striped responsive-utilities jambo_table">
- <thead>
- <tr class="headings">
- <th>Kategori Barang</th>
- <th>Sub</th>
- <th>Keterangan</th>
- </tr>
- </thead>
- <tbody>
- <?php
- $i=1; $kosong = 0;
- foreach($sheet as $key)
- {
- if ($i%2 == 0)
- $class = "odd pointer";
- else $class = "even pointer";
- $ktgr_brng = $key['A']; // Ambil data NIS
- $sub = $key['B']; // Ambil data nama
- $keterangan = $key['C'];
- if(is_null($ktgr_brng))
- continue;
- if ($i>1)
- {
- $ktgr_brng_td = ( ! is_null($ktgr_brng))?" " : " style='background: #E07171;'";
- $sub_td = ( $sub != '')?" " : " style='background: #E07171;'";
- $keterangan_td = ( ! is_null($keterangan))?" " : " style='background: #E07171;'";
- // Jika salah satu data ada yang kosong
- if(is_null($ktgr_brng)){
- $kosong++; // Tambah 1 variabel $kosong
- }
- ?>
- <tr class="<?php echo $class?>">
- <td class="align-middle"><?php print $ktgr_brng_td.">".$ktgr_brng ?></td>
- <td class="align-middle"><?php print $sub_td.">".$sub?></td>
- <td class="align-middle"><?php print $keterangan_td.">".$keterangan?></td>
- </tr>
- <?php
- }
- $i++;
- }
- ?>
- </tbody>
- </table>
- <?php
- if($kosong == 0)
- {
- echo "<hr>";
- // Buat sebuah tombol untuk mengimport data ke database
- echo "<button name='import' id='btn-import' class='btn btn-primary'><span class='glyphicon glyphicon-upload'></span> Save</button>";
- }
- // Buat sebuah tombol untuk mengimport data ke database
- echo "<button name='tutup' id='btn-close' class='btn btn-default'><span class='glyphicon glyphicon-remove'></span> Close</button>";
- ?>
- </div>
- </div>
- </div>
- </div>
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement