简体   繁体   中英

Import CSV file into database with codeigniter

I have a big problem. I dont know what I am doing wrong, but the code dos not work. I get some errors in my page. Can anyone help me please?

Here is my code:

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

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>

for generating csv try this

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

}

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