简体   繁体   中英

setting up a unique column in mysql and showing flashdata in codeigniter

i have a codeigniter website, where i have a unique column, users are asked to insert data using input fields, if a user tries to add dupliate value in the column by default the datas wont be inserted, so i am trying to set flash data according to it:

function import_domestic_excel_data(){

if(isset($_FILES["file"]["name"])){
   $path = $_FILES["file"]["tmp_name"];
   $object = PHPExcel_IOFactory::load($path);
   foreach($object->getWorksheetIterator() as $worksheet){
       $highestRow = $worksheet->getHighestRow();
       $highestColumn = $worksheet->getHighestColumn();
       for($row=2; $row<=$highestRow; $row++){
 $sendername = $worksheet->getCellByColumnAndRow(0, $row)->getValue();
 $senderreference = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
  if(!empty($sendername)){
               $data[] = array(
                   'sendername'  => $sendername,
                   'senderreference'   => $senderreference,
               );
           }
}
       $response=$this->excel_import_model->insert_excel($data);
   if($response){
    $this->session->set_flashdata("Success","Data Added Successfully !");
    redirect('listconsignment' , 'refresh');
}
  else{
   $this->session->set_flashdata("Error","Data Upload Error !");
    redirect('listconsignment' , 'refresh');
  }
 }
 
}
}

now here the problem is, whatever the outcome like if the data gets uploaded or if the data doesnt get uploaded am only getting the error message set in flashdata, not only flashdata, i tried simply echoing "error" and "success", then also its echoing error if the data is successfully uploaded. can anyone please tell me what could be wrong in here. thanks in advance

I think the problem is you put the set_flashdata inside foreach :

foreach ($object->getWorksheetIterator() as $worksheet) {
   ...
}

Maybe there will be always a $response = false on the last $worksheet of the loop. So it will always error to the session. Take the block to check $response outside foreach will solve this problem:

$result = false;
foreach ($object->getWorksheetIterator() as $worksheet) {
    ....
    $response = $this->excel_import_model->insert_excel($data);
    if ($response) {
        $result = true; // Change to condition to set true to what you want
    }
}
if($result){
    $this->session->set_flashdata("Success", "Data Added Successfully !");
    redirect('listconsignment', 'refresh');
}
else {
    $this->session->set_flashdata("Error", "Data Upload Error !");
    redirect('listconsignment', 'refresh');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM