使用codeigniter将CSV文件导入数据库

时间:2017-02-09 12:48:40

标签: php database codeigniter csv

我有一个大问题。我不知道我做错了什么,但代码不起作用。我的页面出现了一些错误。有人可以帮帮我吗?

这是我的代码:

MODEL

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;
}

CONTROLLER

function upload_actives()
{

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

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

查看

<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>

1 个答案:

答案 0 :(得分: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    */

}