2017-02-09 45 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