[Codeigniter phpexcel导入之前的验证数组

问题描述 投票:0回答:1

我想在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;
  }
}

我的期望

数据将仅发布如果具有完整的验证,则当每个单元格之一中有空白单元格时,将引发错误。那可能吗?还是应该在端点那边?

php codeigniter httprequest phpexcel
1个回答
0
投票
//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;

这应该为您工作。

© www.soinside.com 2019 - 2024. All rights reserved.