上传csv无法正常工作

时间:2015-08-04 10:54:42

标签: php codeigniter mysqli

当我上传csv文件时,只会上传第一个数据。 这是我的控制者。

UIView

这是我的模特

public function uploadbusinesstype() 
   {

    $config['upload_path'] = APPPATH.'/assets/upload/';
    $config['allowed_types'] = 'csv';
    $config['max_size']  = '5000';
    $replace = '"';

    $this->load->library('upload', $config);
    $this->load->database();

    if ( ! $this->upload->do_upload('file_name'))
    {
        $error = array('error' => $this->upload->display_errors());
     $this->session->set_flashdata('msg_excel','Choose a .csv file to upload');
         redirect(base_url().'admin/businesstype/managecategories'); 
    }
 else

    {
       $data = array('upload_data' => $this->upload->data());
       $userfile = $data['upload_data']['file_name'];
       $this->load->library('csvreader');
       $upload_data = $this->upload->data(); 
       $file =  $upload_data['full_path'];
       $file_name =  $upload_data['userfile']; 
       $data = $this->csvreader->parse_file($file);
      foreach($data as $row)
      {
            if($row['main_type_name']=='')
       {
        $this->session->set_flashdata('msg_excel','Please check the Type Name.It is empty');
         redirect(base_url().'admin/businesstype/managecategories');     
     }

    else if(!preg_match("/^[a-zA-Z& ]+$/",$row['main_type_name']))
     {
      $this->session->set_flashdata('msg_excel','Please check the Type Name.Only alphabets are allowed');
         redirect(base_url().'admin/businesstype/managecategories');   
     }

     else
     {

          $results_array = array(
                           'type_id' => $row['type_id'],
                           'main_type_name' => $row['main_type_name'] 
                           );  

           $this->load->model('admin/businesstype_model');    
           $this->businesstype_model->catupload($results_array);
     }
            $success_message='Successfully Upload!';
            $this->session->set_flashdata('success_message',$success_message); 
            redirect(base_url().'admin/businesstype/managecategories');                  
      }      
      } 
      }

这里有主要的类型名称。当我在csv中提供配件,手表和手机作为主要类型名称。只有第一个值才会进入数据库。

2 个答案:

答案 0 :(得分:0)

试试这个..

将所有值添加到数组并将数组传递给模型“businesstype_model”

$results_array=array();
    foreach($data as $row)
      {
            if($row['main_type_name']=='')
       {
        $this->session->set_flashdata('msg_excel','Please check the Type Name.It is empty');
         redirect(base_url().'admin/businesstype/managecategories');     
     }

    else if(!preg_match("/^[a-zA-Z& ]+$/",$row['main_type_name']))
     {
      $this->session->set_flashdata('msg_excel','Please check the Type Name.Only alphabets are allowed');
         redirect(base_url().'admin/businesstype/managecategories');   
     }

     else
     {

//将值存储到数组

          $results_array[] = array(
                           'type_id' => $row['type_id'],
                           'main_type_name' => $row['main_type_name'] 
                           );  


     }
     if(count($results_array))
     {
     $this->load->model('admin/businesstype_model');    
    $this->businesstype_model->catupload($results_array);
    }
            $success_message='Successfully Upload!';
            $this->session->set_flashdata('success_message',$success_message); 
            redirect(base_url().'admin/businesstype/managecategories');                  
      } 

答案 1 :(得分:0)

否则

 {
  $results_array = array(
                  'type_id' => $row['type_id'],
                  'main_type_name' => $row['main_type_name'] 
                       );    
       $this->load->model('admin/businesstype_model');    
      $this->businesstype_model->catupload($results_array);
 }
}      
  $success_message='Successfully Upload!';
  $this->session->set_flashdata('success_message',$success_message); 
  redirect(base_url().'admin/businesstype/managecategories')
} 
}

我通过使用这种方法得到它