2017-02-09 46 views
-1

我有一个很大的问题。我不知道我在做什么错,但代码dos不起作用。我的页面出现一些错误。任何人都可以帮助我吗?将CSV文件导入数据库与codeigniter

这里是我的代码:

模型

function upload_active() 
{ 

    $fp = fopen($_FILES['userfile']['tmp_name'],'r') or die("can't open file"); 
    while($csv_line = fgetcsv($fp,1024)) 
    { 

     for ($i = 0, $j = count($csv_line); $i < $j; $i++) 
     { 
      $insert_csv = array(); 
      $insert_csv['email_ac'] = $email_ac[0]; 
     } 

     $data = array(
      'email_ac' => $insert_csv['email_ac'] 
     ); 

     $data['muc_active']=$this->db->insert('muc_active', $data); 
    } 

    fclose($fp) or die("can't close file"); 
    $data['success']="success"; 
    return $data; 
} 

function get_active_info() 
{ 

    $get_details=$this->db->query("select * from muc_active"); 
    return $get_details; 
} 

控制器

function upload_actives() 
{ 

    $data['result']=$this->Muc_model->upload_active(); 
    $data['query']=$this->Muc_model->get_active_info(); 

    $this->load->view(' muc ',$data); 
} 

VIEW

<form action="muc/upload_actives" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
    <table> 
     <tr> 
      <td>Choose your file: </td> 
      <td> 
       <input type="file" class="form-control" name="userfile" id="userfile" align="center"/> 
      </td> 
      <td> 
       <div class="col-lg-offset-3 col-lg-9"> 
        <button type="submit" name="submit" class="btn btn-info" >Save/button> 
       </div> 
      </td> 
     </tr> 
    </table> 
</form> 
+0

你努力了这个问题 –

+0

很难:),花了很多时间在这个问题上...阅读所有其他职位和文档但我不知道... – Dasa

+0

按照此http://wwwsourcecodester.com/php/6477/how-import-csv-data-codeigniter.html –

回答

0

生成CSV试试这个

function get_report(){ 
$this->load->model('my_model'); 
$this->load->dbutil(); 
$this->load->helper('file'); 
/* get the object */ 
$report = $this->my_model->index(); 
/* pass it to db utility function */ 
$new_report = $this->dbutil->csv_from_result($report); 
/* Now use it to write file. write_file helper function will do it */ 
write_file('csv_file.csv',$new_report); 
/* Done */ 

}

+0

谢谢。这是为了下载报告。我也需要这个,但首先我需要把CSV文件放入数据库:) – Dasa