【发布时间】:2016-12-11 18:28:33
【问题描述】:
我的控制器看起来
public function importcsv()
{
$data['menu'] = $this->AdminModel->get_menu();
$data['error'] = ''; //initialize image upload error array to empty
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
// If upload failed, display error
if (!$this->upload->do_upload())
{
$data['error'] = $this->upload->display_errors();
$this->load->view('csvindex', $data);
}
else
{
$file_data = $this->upload->data();
$file_path = './assets/uploads/'.$file_data['file_name'];
if ($this->csvimport->get_array($file_path))
{
$csv_array = $this->csvimport->get_array($file_path);
foreach ($csv_array as $row)
{
$data1['mid']=$row['mid'];
$data1['category']=$row['category'];
$data1['name']=$row['name'];
$data1['price']=$row['price'];
$data1['description']=$row['description'];
if($this->db->where('mid', $data1['mid']))
{
$this->db->update('menu', $data1);
}
if($this->db->where("mid <>",$data1['mid']))
{
$this->db->insert('menu', $data1);
}
}
$this->session->set_flashdata('success', 'Csv Data Imported Succesfully');
redirect(base_url().'Admin/menu');
}
else
{
$data['error'] = "Error occured";
$this->load->view('csvindex', $data);
}
}
}
使用条件
if($this->db->where('mid', $data1['mid']))
{
$this->db->update('menu', $data1);
}
我可以更新 mid 等于 csv 文件 mid ($data1['mid'] 的 menu 表格内容)。它的工作正常。
我的需要是当 mid 在 csv 文件 中而不是在 菜单表 中时,我必须插入记录。为此我使用了一个条件
if($this->db->where("mid <>",$data1['mid']))
{
$this->db->insert('menu', $data1);
}
但它不起作用,它就像 csv 文件中的 mid 不在 菜单表 中一样,然后它插入整个csv 文件内容到表中。
我需要只插入不在菜单表中的记录。那个条件怎么写。我正在研究它很长一段时间我对这种情况很陌生。提前致谢。
【问题讨论】:
标签: php mysql codeigniter csv