Advertisement
badlogic

import excel csv to databse

Jun 10th, 2019
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.71 KB | None | 0 0
  1. //template
  2. <form method="post" action="" enctype="multipart/form-data">
  3.     <input type="file" name="filecsv" value="">
  4.     <input type="submit" id="submit_import" name="submit_import" value="Import Logs" class="themeInputButton">
  5. </form>
  6.  
  7.  
  8.  
  9.  
  10. //crontroller
  11.  
  12.  if (count($_POST)>0) {
  13.                 //import data
  14.                 //$import_logs = $objClsImport_Logs->import_logs($_FILES['filecsv']['tmp_name']);
  15.                 $import_logs = $objClsImport_Logs->import_logs2($_FILES['filecsv']['tmp_name']);
  16.             }
  17.  
  18.  
  19. //class
  20.  
  21.     function import_logs2($excel_file_name_with_path){
  22.         $fcontent = file($excel_file_name_with_path);
  23.  
  24.         $a = 0;
  25.         $arr = array();
  26.         unset($fcontent[0]);
  27.         unset($fcontent[1]);
  28.         unset($fcontent[2]);
  29.         unset($fcontent[3]);
  30.         //printa($fcontent);
  31.         foreach ($fcontent as $fval){
  32.             $data = explode(';',$fval);
  33.             //datetime pag nag error ditong part it means na meron logs na walang date sa calendar
  34.             $datetime = DateTime::createFromFormat("y-m-d",$data[3]);
  35.             $date = $datetime->format("Y-m-d");
  36.             //$time = $datetime->format("H:i");
  37.             //printa($data);die;
  38.             $formatted_data = array(
  39.                 "emp_no" => $data[0],
  40.                 "firstname" => $data[1],
  41.                 "tks_trans_date" => $date,
  42.                 "tks_time_in" => $data[5],
  43.                 "tks_break_in" => $data[6],
  44.                 "tks_break_out" => $data[6],
  45.                 "tks_time_out" => $data[6]);
  46.            
  47.             $arr[] = $formatted_data;
  48.             $a++;
  49.  
  50.         }
  51.  
  52.         //printa($arr);die;
  53.  
  54.  
  55.         $this->import_array($arr);
  56.         $_SESSION['message'] = "<p style='color: green'>Import Logs Successfull</p>";
  57.         //header('Location:commision_benefits.php?statpos=import_logs');
  58.  
  59.     }
  60.  
  61.  
  62.     function import_array($arr = array()){
  63.         /*printa($arr);die;*/
  64.         $value = array();
  65.         $empnum = array();
  66.         foreach ($arr as $aval){
  67.             $emp_id = $this->getEmployeeIDWithEmpNo($aval['emp_no']);
  68.             $value[] = "('$emp_id','{$aval['emp_no']}', '{$aval['firstname']}', '{$aval['tks_trans_date']}', '{$aval['tks_time_in']}:00', '{$aval['tks_break_in']}:00', '{$aval['tks_break_out']}:00', '{$aval['tks_time_out']}:00')";
  69.             $sql1 = "delete from dps_hris_db.tks_emp_log where emp_id = $emp_id and tks_trans_date = '{$aval['tks_trans_date']}'";
  70.             $this->conn->Execute($sql1);
  71.  
  72.  
  73.             /*if(!empty($emp_id)) {
  74.  
  75.                 if(count($aval) >= 5){
  76.                     $value[] = "($emp_id,'{$aval[0]['emp_no']}', '{$aval[0]['lastname']} {$aval[0]['firstname']}', '{$aval[0]['time']}:00', '{$aval[1]['time']}:00', '{$aval[2]['time']}:00', '{$aval[(count($aval) - 1)]['time']}:00', '{$aval[0]['date']}')";
  77.                     $sql1 = "delete from dps_hris_db.tks_emp_log where emp_id = $emp_id and tks_trans_date = '{$aval[0]['date']}'";
  78.                     $this->conn->Execute($sql1);
  79.                 }
  80.  
  81.                 if (count($aval) == 4) {
  82.                     $value[] = "($emp_id,'{$aval[0]['emp_no']}', '{$aval[0]['lastname']} {$aval[0]['firstname']}', '{$aval[0]['time']}:00', '{$aval[1]['time']}:00', '{$aval[2]['time']}:00', '{$aval[3]['time']}:00', '{$aval[0]['date']}')";
  83.                     $sql1 = "delete from dps_hris_db.tks_emp_log where emp_id = $emp_id and tks_trans_date = '{$aval[0]['date']}'";
  84.                     $this->conn->Execute($sql1);
  85.                 }
  86.  
  87.                 if(count($aval) == 3){
  88.                     $value[] = "($emp_id,'{$aval[0]['emp_no']}', '{$aval[0]['lastname']} {$aval[0]['firstname']}', '{$aval[0]['time']}:00', '{$aval[1]['time']}:00', '', '{$aval[2]['time']}:00', '{$aval[0]['date']}')";
  89.                     $sql1 = "delete from dps_hris_db.tks_emp_log where emp_id = $emp_id and tks_trans_date = '{$aval[0]['date']}'";
  90.                     $this->conn->Execute($sql1);
  91.                 }
  92.  
  93.                 if (count($aval) == 2) {
  94.                     $value[] = "($emp_id,'{$aval[0]['emp_no']}', '{$aval[0]['lastname']} {$aval[0]['firstname']}', '{$aval[0]['time']}:00', '', '', '{$aval[1]['time']}:00', '{$aval[0]['date']}')";
  95.                     $sql1 = "delete from dps_hris_db.tks_emp_log where emp_id = $emp_id and tks_trans_date = '{$aval[0]['date']}'";
  96.                     $this->conn->Execute($sql1);
  97.                 }
  98.  
  99.                 if (count($aval) == 1) {
  100.                     $value[] = "($emp_id,'{$aval[0]['emp_no']}', '{$aval[0]['lastname']} {$aval[0]['firstname']}', '{$aval[0]['time']}:00', '', '', '', '{$aval[0]['date']}')";
  101.                     $sql1 = "delete from dps_hris_db.tks_emp_log where emp_id = $emp_id and tks_trans_date = '{$aval[0]['date']}'";
  102.                     $this->conn->Execute($sql1);
  103.                 }
  104.  
  105.  
  106.             }else{
  107.                 //dito yung data na makikita yung employee na wala sa system pero may log file siya
  108.                 $empnum[] = $aval[0]['emp_no'].'  '.$aval[0]['firstname'].'  '.$aval[0]['lastname'];
  109.  
  110.             }*/
  111.         }
  112.  
  113.         //printa($empnum);
  114.  
  115.         $values = implode(",",$value);
  116.        echo $sql = "insert into dps_hris_db.tks_emp_log (emp_id,emp_no,tks_emp_name,tks_trans_date,tks_time_in,tks_break_in,tks_break_out,tks_time_out) values $values";
  117.         echo "<br>";
  118.         //printa($value);die;
  119.         //die;
  120.         $this->conn->Execute($sql);
  121.     }
  122.  
  123.     function getEmployeeIDWithEmpNo($emp_no){
  124.  
  125.         $sql = "select emp_id
  126.                from dps_hris_db.hris_emp_masterfile
  127.                where emp_no = '$emp_no'";
  128.         $result = $this->conn->Execute($sql);
  129.  
  130.         if(!$result->EOF){
  131.             return $result->fields['emp_id'];
  132.         }
  133.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement