【问题标题】:Import CSV file into database with codeigniter使用 codeigniter 将 CSV 文件导入数据库
【发布时间】:2017-02-09 12:48:40
【问题描述】:

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

这是我的代码:

型号

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

查看

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

【问题讨论】:

  • 你在为这个问题努力吗
  • 非常困难 :) 并在这个问题上花费了大量时间...阅读所有其他帖子和文档,但我无法弄清楚...
  • 感谢您的链接。几天前我试过了,它没有用....

标签: php database codeigniter csv


【解决方案1】:

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

}

【讨论】:

  • 谢谢。这是用于下载报告的。我也需要这个,但首先我需要将 CSV 文件放入数据库:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
  • 1970-01-01
  • 1970-01-01
  • 2015-12-10
  • 2016-12-30
  • 1970-01-01
相关资源
最近更新 更多