У меня есть 3 таблицы для вставки данных через Excel с именами employee_details
, employee_family_details
и employee_policy_member
.
В листе Excel есть поля типа emp_id
, emp_name
и family_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>×</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;
}
}