【发布时间】:2020-04-02 04:30:48
【问题描述】:
我想在 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;
}
}
我的期望
如果有完整的验证,数据将只发布,当每个单元格中有一个空白单元格时,它会抛出错误。那可能吗?还是应该在端点端?
【问题讨论】:
-
我认为codeigniter表单验证只能验证$_POST数据。你必须改变它的工作方式来验证其他东西。
标签: php codeigniter httprequest phpexcel