导入前的 Codeigniter phpexcel 验证数组
Codeigniter phpexcel validation array before import
我想在 Codeigniter 中将 excel 作为 json 数据导入,并对每一行进行验证。
excel列
|A |B |C
|demo|DM1|11231
|demo|DM2|87128
...
我从 excel
获取数组的代码
//start loop excel from 2nd row. Row 1 is title row
for ($j=2; $j < $lastRow; $j++ ){
$myArray[] = array(
'site_id' => $objWorksheet->getCell('B'.$j)->getValue(),
'site_name' => $objWorksheet->getCell('A'.$j)->getValue(),
'id_site_doc'=> $objWorksheet->getCell('C'.$j)->getValue()
}
//validate the array
$this->form_validation->set_data($myArray);
$this->form_validation->reset_validation();
foreach ($myArray as $key => $value) {
$columnB = $myArray[$key]['site_id'];
$columnA = $myArray[$key]['site_name'];
$columnC = $myArray[$key]['id_site_doc'];
}
if (empty($columnB )){
$this->form_validation->set_rules('mr_submit_target', 'site_id on row ' . $j, 'required');
}
else if (empty($columnA )){
$this->form_validation->set_rules('short_desc', 'site_name on row ' . $j, 'required');
}
else if (empty($columnC )) {
$this->form_validation->set_rules('cd_id', 'id_site_doc on row ' . $j, 'required');
}
if ($this->form_validation->run() == FALSE){
$errorArray[$j]=$this->form_validation->error_array();
print_r($errorArray[$j]);
}else{
//post to endpoint
$data_to_post = json_encode($myArray);
$curl = curl_init('http://myendpoint/implementation_bom_op');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_to_post))
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_to_post);
// Send the request
$result = curl_exec($curl);
echo $result;
}
}
我的期望
数据将 post 只有 如果有完整验证,当每个单元格中的一个单元格中有空白单元格时,它将抛出错误。那可能吗?或者它应该在端点端?
//start loop excel from 2nd row. Row 1 is title row
for ($j=2; $j < $lastRow; $j++ ){
$columnB = $objWorksheet->getCell('B'.$j)->getValue();
$columnA = $objWorksheet->getCell('A'.$j)->getValue();
$columnC = $objWorksheet->getCell('C'.$j)->getValue();
//validate the array
$error = 0;
if($columnB == ""){
$errorArray[$j][] = 'your-error-here';
$error +=1;
}
if($columnA == ""){
$errorArray[$j][] = 'your-error-here';
$error +=1;
}
if($columnC == ""){
$errorArray[$j][] = 'your-error-here';
$error +=1;
}
if($error != 0){
continue; //do something with the error
}else{
$myArray['site_id'] = $columnB;
$myArray['site_name'] = $columnA;
$myArray['id_site_doc'] = $columnC;
}
}
//post to endpoint
$data_to_post = json_encode($myArray);
$curl = curl_init('http://myendpoint/implementation_bom_op');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_to_post))
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_to_post);
// Send the request
$result = curl_exec($curl);
echo $result;
这应该适合你。
我想在 Codeigniter 中将 excel 作为 json 数据导入,并对每一行进行验证。
excel列
|A |B |C
|demo|DM1|11231
|demo|DM2|87128
...
我从 excel
获取数组的代码 //start loop excel from 2nd row. Row 1 is title row
for ($j=2; $j < $lastRow; $j++ ){
$myArray[] = array(
'site_id' => $objWorksheet->getCell('B'.$j)->getValue(),
'site_name' => $objWorksheet->getCell('A'.$j)->getValue(),
'id_site_doc'=> $objWorksheet->getCell('C'.$j)->getValue()
}
//validate the array
$this->form_validation->set_data($myArray);
$this->form_validation->reset_validation();
foreach ($myArray as $key => $value) {
$columnB = $myArray[$key]['site_id'];
$columnA = $myArray[$key]['site_name'];
$columnC = $myArray[$key]['id_site_doc'];
}
if (empty($columnB )){
$this->form_validation->set_rules('mr_submit_target', 'site_id on row ' . $j, 'required');
}
else if (empty($columnA )){
$this->form_validation->set_rules('short_desc', 'site_name on row ' . $j, 'required');
}
else if (empty($columnC )) {
$this->form_validation->set_rules('cd_id', 'id_site_doc on row ' . $j, 'required');
}
if ($this->form_validation->run() == FALSE){
$errorArray[$j]=$this->form_validation->error_array();
print_r($errorArray[$j]);
}else{
//post to endpoint
$data_to_post = json_encode($myArray);
$curl = curl_init('http://myendpoint/implementation_bom_op');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_to_post))
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_to_post);
// Send the request
$result = curl_exec($curl);
echo $result;
}
}
我的期望
数据将 post 只有 如果有完整验证,当每个单元格中的一个单元格中有空白单元格时,它将抛出错误。那可能吗?或者它应该在端点端?
//start loop excel from 2nd row. Row 1 is title row
for ($j=2; $j < $lastRow; $j++ ){
$columnB = $objWorksheet->getCell('B'.$j)->getValue();
$columnA = $objWorksheet->getCell('A'.$j)->getValue();
$columnC = $objWorksheet->getCell('C'.$j)->getValue();
//validate the array
$error = 0;
if($columnB == ""){
$errorArray[$j][] = 'your-error-here';
$error +=1;
}
if($columnA == ""){
$errorArray[$j][] = 'your-error-here';
$error +=1;
}
if($columnC == ""){
$errorArray[$j][] = 'your-error-here';
$error +=1;
}
if($error != 0){
continue; //do something with the error
}else{
$myArray['site_id'] = $columnB;
$myArray['site_name'] = $columnA;
$myArray['id_site_doc'] = $columnC;
}
}
//post to endpoint
$data_to_post = json_encode($myArray);
$curl = curl_init('http://myendpoint/implementation_bom_op');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_to_post))
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_to_post);
// Send the request
$result = curl_exec($curl);
echo $result;
这应该适合你。