如果不通过Codeigniter中的Excel Sheet退出,如何插入数据库?

时间:2019-01-25 09:06:39

标签: php database codeigniter bulkinsert

我有3个表要通过名为employee_detailsemployee_family_detailsemployee_policy_member的excel插入数据。

excel工作表中有emp_idemp_namefamily_dob之类的字段。

我必须首先检查emp_id中是否有employee_details。如果不是,则插入到employee_details表中。然后,它必须检查那里是否有家庭成员,然后插入employee_family_details,最后插入employee_policy_member

我是Codeigniter的新手,所以不知道该怎么做。如果有人有任何建议,请帮助我。

我的查看页面

<div class="main-content-inner">
         <form id="first_form"  method="post" enctype="multipart/form-data" action="#">
        <div class="container">
           <div class="row">

              <div class="col-md-5 mt-2">
                 <div class="card">
                    <div class="card-body card-style">
                       <h4 class="header-title color-1 title">
                          Endorsement Request
                          <hr class="dashed-line-40">
                       </h4>

                          <div class="row">
                             <div class="col-md-6">
                                <div class="form-group">
                                   <label class="col-form-label">Employer</label>
                                   <select class="form-control" name="employer" id="employer">
                                      <?php foreach($employer->result() as $emp){ ?>
                       <option value="<?php echo $emp->employer_id; ?>"><?php echo $emp->name; ?></option>
                              <?php } ?>
                                   </select>
                                </div>
                             </div>
                             <div class="col-md-6">
                                <div class="form-group">
                                   <label class="col-form-label">Insurer</label>
                                   <select class="form-control" name="insurer">
                                       <?php foreach($insurer->result() as $ins){ ?>
                       <option value="<?php echo $ins->insurer_id; ?>"><?php echo $ins->ins_co_name; ?></option>
                              <?php } ?>

                                   </select>
                                </div>
                             </div>
                             <div class="col-md-6">
                                <div class="form-group">
                                   <label class="col-form-label">Policy Type</label>
                                   <select class="form-control" name="policy_type">
                                      <?php foreach($policytype->result() as $pct){ ?>
                       <option value="<?php echo $pct->policy_type_id; ?>"><?php echo $pct->policy_name; ?></option>
                              <?php } ?>
                                   </select>
                                </div>
                             </div>
                             <div class="col-md-6">
                                <div class="form-group">
                                   <label class="col-form-label">Policy #</label>
                                   <select class="form-control" name="policy_name">
                                      <option value="GRP9898989">GRP9898989</option>
                                      <option value="GRP9849898"> GRP9849898</option>
                                   </select>
                                </div>
                             </div>
                          </div>

                    </div>
                 </div>
              </div>
              <div class="col-md-7 mt-2">
                 <div class="card">
                    <div class="card-body card-style">
                       <div class="col-md-12">
                          <h4 class="header-title color-1 title">
                             Bulk Upload
                             <hr class="dashed-line-14">
                          </h4>
                          <div class="row">
                             <div class="col-md-8 mt-4">

                                   <div class="input-group">
                                      <div class="custom-file">
                                         <input type="file" name="uploadFile" class="custom-file-input" id="inputGroupFile04">
                                         <label class="custom-file-label" for="inputGroupFile04">Upload Member</label>
                                      </div>
                                   </div>

                             </div>
                             <div class="col-md-3 mt-2">          
                                <button type="button" class="btn color-bl btn-sm mt-3" data-toggle="modal" data-target="#exampleModalCenter">Preview</button>
                             </div>
                          </div>
                          <div class="col-md-12 mt-4">
                             <div class="row" align="center">
                                <div class="col-md-6">
                                   <div class=""> <button type="button" class="btn can-btn btn-md mb-3">Cancel</button></div>
                                </div>
                                <div class="col-md-6">

                                   <input type="submit" id="btnSave" class="btn  btn-md mb-3 sub-btn" style="background-color: #046d66;  color: #fff; !important" value="Submit" />

                                </div>
                             </div>
                          </div>
                       </div>
                       <div class="modal fade" id="exampleModalCenter">
                          <div class="modal-dialog modal-dialog-centered" role="document">
                             <div class="modal-content">
                                <div class="modal-header">
                                   <h5 class="modal-title">Modal title</h5>
                                   <button type="button" class="close" data-dismiss="modal"><span>&times;</span></button>
                                </div>
                                <div class="modal-body">
                                   <p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Eius voluptates explicabo natus nobis, aperiam placeat aliquid nisi ut exercitationem dolor quisquam nam tempora voluptatem. Unde dignissimos est aliquid quidem porro dolorum ipsam suscipit animi quas, debitis ea, sunt quo distinctio doloribus eveniet dolores tempore delectus voluptatum! Possimus earum asperiores animi.</p>
                                </div>
                                <div class="modal-footer">
                                   <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                   <button type="button" class="btn btn-primary">Save changes</button>
                                </div>
                             </div>
                          </div>
                       </div>
                    </div>
                 </div>
              </div>

           </div>
        </div>
        </form>
     </div>

我的控制器

public function uploadData(){

  if ($this->input->post('policy_name')) {

     // $this->_validate();
            $path = APPPATH .'uploads/';
            require_once APPPATH . "/third_party/PHPExcel.php";
            $config['upload_path'] = $path;
            $config['allowed_types'] = 'xlsx|xls|csv';
            $config['remove_spaces'] = TRUE;
            $this->load->library('upload', $config);
            $this->upload->initialize($config);            
            if (!$this->upload->do_upload('uploadFile')) {
                $error = array('error' => $this->upload->display_errors());
            } else {
                $data = array('upload_data' => $this->upload->data());
            }
            if(empty($error)){
              if (!empty($data['upload_data']['file_name'])) {
                $import_xls_file = $data['upload_data']['file_name'];
            } else {
                $import_xls_file = 0;
            }
            $inputFileName = $path . $import_xls_file;
           // echo $inputFileName;exit;
            try {
                $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
                $objReader = PHPExcel_IOFactory::createReader($inputFileType);
                $objPHPExcel = $objReader->load($inputFileName);
                $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
                $flag = true;
                $i=0;
                foreach ($allDataInSheet as $value) {
                  if($flag){
                    $flag =false;
                    continue;
                  }

                  $inserdata[$i]['emp_id'] = $value['A'];
                  $inserdata[$i]['emp_name'] = $value['B'];
                  $inserdata[$i]['member_name'] = $value['C'];
                  $inserdata[$i]['family_dob'] = $value['D'];
                  $i++;
                }               
                $result = $this->import->importdata($inserdata);   
                if($result){
                  echo "Imported successfully";
                }else{
                  echo "ERROR !";
                }             

          } catch (Exception $e) {
               die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
                        . '": ' .$e->getMessage());
            }
          }else{
              echo $error['error'];
            }


    }

  }

我的模特

public function importData($data) {

        $res = $this->db->insert_batch('employee_policy_member',$data);
        if($res){
            return TRUE;
        }else{
            return FALSE;
        }

    }

我不知道请帮助我。

0 个答案:

没有答案