2017-08-31 86 views
1

我試圖將每個員工的出勤詳細信息作爲批量出席上傳數據從excel文件保存到數據庫中,下面的代碼顯示Excel數據導入成功但不存儲值到數據庫中,行$ last_id = $ this-> mod_common-> add_attendence_data($ data_user);不執行,這就是爲什麼它不工作,如何使這行執行?由於該代碼是由其他開發人員編寫的,我發現很難理解。從上傳的Excel文件中檢索數據並將其存儲到數據庫中

控制器:admin_list.php

\t function upload_bulk_attendance(){ 
 
\t \t $data['htmltext'] = $this->mod_common->get_allleaves(); 
 
\t $this->data['maincontent'] = $this->load->view('maincontents/upload_attendance_sheet', $data,true); 
 
\t $this->load->view('layout', $this->data); 
 
\t } 
 
\t 
 
    function importExcel() { 
 
\t //$this->mod_common->delete_attendance(); 
 
\t $this->load->library('excel'); 
 
\t 
 
    //microsoft excel 2007 
 
\t if(!$objReader = PHPExcel_IOFactory::createReader('Excel5')){ 
 
\t \t die('Unable to create reader object'); 
 
\t } 
 
    //$objReader = PHPExcel_IOFactory::createReader('Excel2007'); 
 
    //set to read only 
 
    $objReader->setReadDataOnly(false); 
 
    //load excel file 
 
\t 
 
\t global $SITE; 
 
\t if(!$upload_val=$this->admin_init_elements->do_upload_docs('user_attendance',$SITE->client_logo_image_src,false)){ 
 
\t \t $data['error'] = $this->upload->display_errors(); 
 
\t \t $this->load->view('maincontents/upload_attendance_sheet', $data,true); \t 
 
\t } else { 
 
\t \t 
 
\t \t $file_path = $upload_val['upload_data']['full_path']; 
 
\t } \t 
 
\t 
 
    $objPHPExcel = $objReader->load($file_path); 
 
    $objWorksheet = $objPHPExcel->setActiveSheetIndex(1); 
 
\t 
 
/*---------------------------------------------------------------------------*/ \t 
 
\t \t $excelrow = 1; 
 
\t \t $from_Date = $objWorksheet->getCellByColumnAndRow(4,1)->getValue(); 
 
\t \t 
 
\t \t $to_Date \t = $objWorksheet->getCellByColumnAndRow(6,1)->getValue(); 
 
\t \t $total_rows_in_sheet = $objPHPExcel->getActiveSheet()->getHighestRow(); \t 
 
\t \t 
 
\t \t if (PHPExcel_Shared_Date::isDateTime($objPHPExcel->getActiveSheet()->getCell('E1'))) { 
 
\t \t \t $dateValue = PHPExcel_Shared_Date::ExcelToPHP($from_Date); 
 
\t \t \t $from_date = date('Y-m-d',$dateValue); 
 
\t \t } 
 
\t \t 
 
\t \t if (PHPExcel_Shared_Date::isDateTime($objPHPExcel->getActiveSheet()->getCell('G1'))) { 
 
\t \t \t $dateValue = PHPExcel_Shared_Date::ExcelToPHP($to_Date); 
 
\t \t \t $to_date = date('Y-m-d',$dateValue); 
 
\t \t } 
 

 
\t \t $start = strtotime($from_date); 
 
\t \t $end = strtotime($to_date); 
 
\t \t $days_between = ceil(abs($end - $start)/86400); 
 
\t \t $excelrow = $excelrow + 2; 
 
\t \t $emp_code_id_array = $this->mod_common->get_Details2('pr_users_details',array('userid','emp_code')); \t 
 
\t \t foreach($emp_code_id_array as $e){ $emp_code_id[$e['emp_code']] = $e['userid']; } \t \t 
 
\t \t 
 
\t do { 
 
\t \t $shift_excelrow \t = ++$excelrow; 
 
\t /*--------------- Moving row wise for each employees data --------------*/ \t \t 
 
\t \t $emp_code \t = $objWorksheet->getCellByColumnAndRow(0,$excelrow)->getValue(); 
 
\t \t $emp_code \t =($emp_code==null)?0:$emp_code; \t 
 
\t \t 
 
\t \t if($emp_code<=0) continue; 
 
\t \t $emp_code = str_pad($emp_code, 3, '0', STR_PAD_LEFT); 
 
\t \t 
 
\t \t $emp_id \t =($emp_code_id[$emp_code]==null)?0:$emp_code_id[$emp_code]; 
 
\t \t $emp_name \t = $objWorksheet->getCellByColumnAndRow(1,$excelrow)->getValue(); 
 
\t /*--------------- Moving column wise for each days data --------------*/ 
 
\t \t $col_no=3; \t 
 
\t \t for($i=0;$i<=$days_between;$i++) { 
 
\t \t $dt = date('Y-m-d',strtotime($from_date)); 
 
\t \t $day = date('d', strtotime($dt .' +'.$i.' day')); 
 
\t \t 
 
\t \t $entry[] = $day."|".$objWorksheet->getCellByColumnAndRow($col_no,$shift_excelrow)->getValue(); \t 
 
\t \t $col_no++; 
 
\t \t } 
 
\t \t $present=$objWorksheet->getCellByColumnAndRow(34,$shift_excelrow)->getValue(); 
 
\t \t $absent=$objWorksheet->getCellByColumnAndRow(35,$shift_excelrow)->getValue(); 
 
\t \t $total=$objWorksheet->getCellByColumnAndRow(36,$shift_excelrow)->getValue(); 
 
\t \t \t if($emp_id!=0){ 
 
\t \t \t \t $emp_id_arr[] \t \t = $emp_id; \t 
 
\t \t \t } 
 
\t \t \t /* CREATING ARRAY FOR DB INSERT */ \t 
 
\t \t \t $data_user['emp_code'] \t \t = $emp_code; 
 
\t \t \t $data_user['emp_id'] \t \t = $emp_id; 
 
\t \t \t $data_user['name'] \t \t \t = $emp_name; 
 
\t \t \t $data_user['entry'] \t \t \t = implode(",",$entry); 
 
\t \t \t $data_user['from_date'] \t \t = $from_date; 
 
\t \t \t $data_user['to_date'] \t \t = $to_date; 
 
\t \t \t $data_user['present'] \t \t = $present; 
 
\t \t \t $data_user['absent'] \t \t = $absent; 
 
\t \t \t $data_user['total'] \t \t \t = $total; 
 
\t \t \t $data_user['current_date'] \t = date('Y-m-d'); 
 
\t \t \t 
 
\t \t \t $last_id=$this->mod_common->add_attendence_data($data_user); 
 
\t \t \t //Reset array 
 
\t \t \t $entry=array(); 
 
\t \t }while($excelrow <=$total_rows_in_sheet); 
 
\t \t 
 
\t /*----------------------------------------------------*/ 
 
\t \t if(count($emp_id_arr)==0){ 
 
\t \t \t $emp_id_arr[] \t \t = 0; \t 
 
\t \t } 
 
\t \t $this->mod_common->save_attendance_notification($last_id,implode(",",$emp_id_arr),$data_user['from_date']); 
 
\t \t 
 
\t \t $this->session->set_flashdata('success', 'Excel Data Imported Succesfully'); 
 
\t \t redirect(base_url().'admin_list/upload_bulk_attendance'); 
 
    }//importExcel() END

型號:mod_common.php

function add_attendence_data($datauser) { 
 
\t 
 
\t $emp_code = str_pad($datauser['emp_code'], 3, '0', STR_PAD_LEFT); 
 
\t $this->db_replace->select('*'); 
 
\t $this->db_replace->from($this->myTables['attendance']); 
 
\t $this->db_replace->where('emp_code',$emp_code); 
 
\t $this->db_replace->where('from_date',$datauser['from_date']); 
 
\t $query=$this->db_replace->get(); 
 
\t if($query->num_rows() <= 0) 
 
\t { 
 
\t \t $datauser['emp_code'] = $emp_code; 
 
\t \t 
 
\t \t $this->db_replace->insert($this->myTables['attendance'],$datauser); 
 
\t \t $instanceid = $this->db_replace->insert_id(); 
 
\t \t 
 
\t \t return $instanceid; 
 
\t } else return false; 
 
\t 
 
} \t

視圖:upload_attendance_sh eet.php

<section class="content-header"> 
 
      <h1> 
 
\t \t \t Upload Attendance 
 
      </h1> 
 
      <ol class="breadcrumb"> 
 
      <li><a href="<?php echo base_url(); ?>"><i class="fa fa-home"></i> Home</a></li> 
 
\t \t \t <li><a href="<?php echo base_url(); ?>admin_list"> Admin</a></li> 
 
\t \t \t <li><a href="<?php echo base_url(); ?>admin_list#peopleconnect">Attendance</a></li> 
 
\t \t \t <li class="active">Upload Attendance</li> 
 
      
 
      </ol> 
 
    </section> 
 
    <section class="content"> 
 
      <div class="container-fluid"> 
 
        <div class="row"> 
 
         <!-- Thought Day--> 
 
         <div class="panel wrapper clearfix m-b-none"> 
 
       <div class="box-header with-border"> 
 
<?php if($bulkUploadMsg!=''){?> 
 
<?php echo $bulkUploadMsg;?> 
 
<?php } ?> 
 

 
<?php 
 
if($this->session->flashdata('success') == TRUE){ 
 
echo $this->session->flashdata('success'); 
 
} 
 
?> 
 

 

 
<br> 
 

 
<?php 
 
echo form_open_multipart('admin_list/importExcel', array('method'=>'post','name'=>'uploadUserForm','id'=>'upload_file','class'=>"custom-form")); 
 
?> 
 
Please download attendance upload format by clicking <a href="<?php echo base_url(); ?>uploads/attendance_format.xls" class="text-info" style="color:#23b7e5">here</a><br><br> 
 
<table cellpadding="10px" style="background:none;width:100%;" RULES="ROWS"> 
 
<thead> 
 
<tr> 
 
\t <td width="50px;">Upload</td> 
 
\t <td>:</td> 
 
\t <td> 
 
\t \t <?php echo form_upload(array("required"=>"required","name"=>"user_attendance","id"=>"user_attendance")); ?> 
 
\t </td> 
 
</tr> 
 
</table> 
 
<h3>Instructions to Upload : </h3> 
 
<ul> 
 
\t <li>Complete the sheet below and upload to update attendance records of all employees 
 
</li><li> 
 
You may add as many rows as needed - one for each employee 
 
</li><li>Enter the start and end dates in the dd/mm/yyyy format only 
 
</li><li>Only use the legend given to enter primary attendance details - Present at assigned office location-X, Absent without notification-A, On Official Travel-O, Present but not in office location-P, Week Off-W, Holiday-H, Half day working-D 
 

 
</li><li>Use the following legends for entering Leave Details-<?php echo $htmltext; ?> 
 

 
</li><li>As good practice, ensure that this sheet is uploaded at approximately the same interval each month 
 

 
</li></ul><br> 
 

 
</p> 
 
<br> 
 
<?php 
 
echo form_submit(array('name'=>'submit','value'=>'Upload','class'=>'btn btn-info btn-submit pull-right add')); 
 
echo form_close(); 
 
?> 
 
</div> 
 
</div> 
 
</div> 
 
</div> 
 
<script type="text/javascript" src="<?php echo base_url(); ?>assets/plugins/data-tables/DT_bootstrap.js"></script>

表:pr_attendance

enter image description here

回答

1

請試試這個方法: -

<?php 

public function importContacts($id) 
{ 
    if(isset($_POST["Import"])) 
    { 
     $filename=$_FILES["file"]["name"]; 
     $f_extension = explode('.',$filename); 
     $f_extension = strtolower(end($f_extension)); 

     if($f_extension == 'csv') // for import CSV file 
      { 
       $file_tmp_name=$_FILES["file"]["tmp_name"]; 
       if($_FILES["file"]["size"] > 0) 
        { 
        $file = fopen($file_tmp_name, "r"); 
        $row = array(); 
        while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) 
        { 
         array_push($row, $emapData); 
        } 
        fclose($file); 
        unset($row[0]); 
        foreach(array_values($row) as $r){ 
          $data = array(
            'gc_group_id' => $id, 
            'gc_user_id' => $this->session->userdata('userId'), 
            'gc_name' => @$r[1], 
            'gc_number' => @$r[0], 
            'gc_email' => @$r[2], 
            'gc_added_date' => date('Y-m-d'), 
           ); 
          $this->user_model->add('groups_contact',$data); 
          array_push($valid_no, $r[0]); 
        } 
        fclose($file); 
        } 
      } 

     if($f_extension == 'xlsx' || $f_extension == 'xls') 
      { 
       /* PHPExcel library */ 
       set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/'); 
       require_once APPPATH.'third_party/PHPExcel/IOFactory.php'; 
       $file_tmp_name=$_FILES["file"]["tmp_name"]; 
       try { 
        $objPHPExcel = PHPExcel_IOFactory::load($file_tmp_name); 
       } catch(Exception $e) { 
        die('Error loading file :' . $e->getMessage()); 
       } 

       $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true); 
       unset($sheetData[1]); 
       foreach(array_values($sheetData) as $s){ 
         $data = array(
            'gc_group_id' => $id, 
            'gc_user_id' => $this->session->userdata('userId'), 
            'gc_name' => @$s[B], 
            'gc_number' => @$s[A], 
            'gc_email' => @$s[C], 
            'gc_added_date' => date('Y-m-d'), 
           ); 
         $this->user_model->add('groups_contact',$data); 
         array_push($valid_no, $s[A]); 
       } 
      } 
     $msg = 'Contacts Added Successfully '; 
    } 

} 

?> 

讓我如果您發現任何問題,請知道。

相關問題