Advertisement
yhoezt_27

Untitled

Mar 10th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. CONTOH PREVIEW DATA EXCELL SEBELUM SAVE/UPDATE DATABASE
  2.  
  3. (1) LOGIC
  4.  
  5. function index()
  6. {
  7. if ($this->session->userdata('url')=='')
  8. {
  9. $data = array();
  10.  
  11. $data['title'] = 'Kategori Barang';
  12. $data['ktgr_brng'] = $this->m_all->semua($this->table);
  13. $data['subktgr']=$this->m_all->getList($this->table, $this->primary, null, null);
  14. $this->template->display('ktgr_brng/index',$data);
  15. }
  16. else redirect($this->session->userdata('url'));
  17. }
  18.  
  19. function preview()
  20. {
  21. if(isset($_REQUEST['func']) && $_REQUEST['func']=="uploadfile")
  22. {
  23. $this->fileupload($_FILES);
  24. }
  25. }
  26.  
  27. function fileupload($file_array)
  28. {
  29. /*BEGIN FILE UPLOADING CODE */
  30. $uploaded_files = array();
  31. if(isset($file_array['input01']) && $file_array['input01']['name']!="")
  32. {
  33. $tmp = @explode(".",$file_array['input01']['name']);
  34. $currentfile_extension = end($tmp);
  35. if ($currentfile_extension == 'xlsx')
  36. {
  37. $filename = "data/ktgr_brng/".date("YmdHis").rand(1000,9999).".".$currentfile_extension;
  38. if(@move_uploaded_file($_FILES['input01']['tmp_name'], $filename))
  39. {
  40. $status= "sukses";
  41. $pesan = "File has been updated successfully.";
  42. $this->session->set_userdata('url',"ktgr_brng/preview_excell");
  43. $this->session->set_userdata('filename', $filename);
  44. //$response['url'] = site_url("user_ktr/preview_excell");
  45. } else {
  46. $status = "gagal";
  47. $pesan = "Error in file uploading.";
  48. }
  49. }
  50. else {
  51. $status= "gagal";
  52. $pesan = "Format Excell 2007.";
  53. }
  54. }
  55. /*END FILE UPLOADING CODE */
  56. $response = array('status'=>$status, 'pesan'=>$pesan);
  57. echo json_encode($response);
  58. }
  59.  
  60. function preview_excell()
  61. {
  62. if ($this->session->userdata('filename') != '')
  63. {
  64. $this->session->unset_userdata('url');
  65.  
  66. $excelreader = new PHPExcel_Reader_Excel2007();
  67. $loadexcel = $excelreader->load($this->session->userdata('filename')); // Load file yang tadi diupload ke folder tmp
  68. $sheet = $loadexcel->getActiveSheet()->toArray(null, true, true ,true);
  69. //$sheet = $this->auth->get_sheet($this->session->userdata('filename'));
  70. $data['sheet'] = $sheet;
  71. $data['title'] = 'Preview Data Excell';
  72.  
  73. $this->template->display('ktgr_brng/preview_excell',$data);
  74. }
  75. else redirect('ktgr_brng/index');
  76. }
  77.  
  78. (2) JQUERY/AJAX (index.php)
  79.  
  80. $('#btn-import').on("click", function(e){
  81. $('#form-import')[0].reset(); // reset form on modals
  82. $('.modal-title').text('Import Data');
  83. $('#modal-import').modal('show');
  84. });
  85.  
  86. $('#input01').on('change',function(){
  87. if(confirm("File sudah benar ?")) {
  88. //$(this).after('<span class="temp-message">File Uploading.....</span>');
  89. $.ajax({
  90. url:"<?php echo site_url('ktgr_brng/preview?func=uploadfile')?>",
  91. type:"POST",
  92. data:new FormData($("#form-import")[0]),
  93. enctype: 'multipart/form-data',
  94. contentType:false,
  95. processData:false,
  96. dataType: 'json',
  97. success:function(response){
  98. $('#form-import').on('hidden.bs.modal', function (e) {
  99. $(this)
  100. .find("input")
  101. .val('')
  102. .end()
  103. })
  104. if (response.status == 'sukses')
  105. {
  106. $("#form-import").modal('hide');
  107. location.href = location.pathname;
  108. }
  109. else
  110. {
  111. alert(response.pesan);
  112. location.href = location.pathname;
  113. }
  114. },
  115. error: function (jqXHR, textStatus, errorThrown)
  116. {
  117. alert(xhr.responseText);
  118. }
  119. });
  120. }
  121. else
  122. {
  123. $('#input01').val('');
  124. }
  125. });
  126.  
  127. (3) VIEW PREVIEW_EXCELL
  128.  
  129. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
  130.  
  131. <div class="page-title">
  132.  
  133. </div>
  134. <div class="clearfix"></div>
  135. <br />
  136.  
  137. <div class="row">
  138. <div class="col-md-12 col-sm-12 col-xs-12">
  139. <div class="x-panel">
  140. <div class="x_title">
  141. <h2><?php echo $title?></h2>
  142.  
  143. </div>
  144. </div>
  145. <div class="x_content">
  146. <div id="view">
  147. <table id="pre-excell" class="table table-striped responsive-utilities jambo_table">
  148. <thead>
  149. <tr class="headings">
  150. <th>Kategori Barang</th>
  151. <th>Sub</th>
  152. <th>Keterangan</th>
  153. </tr>
  154. </thead>
  155. <tbody>
  156. <?php
  157. $i=1; $kosong = 0;
  158. foreach($sheet as $key)
  159. {
  160. if ($i%2 == 0)
  161. $class = "odd pointer";
  162. else $class = "even pointer";
  163.  
  164. $ktgr_brng = $key['A']; // Ambil data NIS
  165. $sub = $key['B']; // Ambil data nama
  166. $keterangan = $key['C'];
  167.  
  168. if(is_null($ktgr_brng))
  169. continue;
  170.  
  171. if ($i>1)
  172. {
  173. $ktgr_brng_td = ( ! is_null($ktgr_brng))?" " : " style='background: #E07171;'";
  174. $sub_td = ( $sub != '')?" " : " style='background: #E07171;'";
  175. $keterangan_td = ( ! is_null($keterangan))?" " : " style='background: #E07171;'";
  176.  
  177. // Jika salah satu data ada yang kosong
  178. if(is_null($ktgr_brng)){
  179. $kosong++; // Tambah 1 variabel $kosong
  180. }
  181.  
  182. ?>
  183. <tr class="<?php echo $class?>">
  184. <td class="align-middle"><?php print $ktgr_brng_td.">".$ktgr_brng ?></td>
  185. <td class="align-middle"><?php print $sub_td.">".$sub?></td>
  186. <td class="align-middle"><?php print $keterangan_td.">".$keterangan?></td>
  187. </tr>
  188. <?php
  189. }
  190. $i++;
  191. }
  192. ?>
  193. </tbody>
  194. </table>
  195. <?php
  196. if($kosong == 0)
  197. {
  198. echo "<hr>";
  199.  
  200. // Buat sebuah tombol untuk mengimport data ke database
  201. echo "<button name='import' id='btn-import' class='btn btn-primary'><span class='glyphicon glyphicon-upload'></span> Save</button>";
  202. }
  203. // Buat sebuah tombol untuk mengimport data ke database
  204. echo "<button name='tutup' id='btn-close' class='btn btn-default'><span class='glyphicon glyphicon-remove'></span> Close</button>";
  205.  
  206. ?>
  207. </div>
  208. </div>
  209. </div>
  210. </div>
  211. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement