my controller looks:
public function menu() {
$data['menu'] = $this->AdminModel->get_menu();
$this->load->view('admin/csvindex', $data);
}
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)
{
$insert_data = array(
'mid'=>$row['mid'],
'category'=>$row['category'],
'name'=>$row['name'],
'price'=>$row['price'],
'description'=>$row['description'],
);
$this->AdminModel->insert_csv($insert_data);
}
$this->session->set_flashdata('success', 'Csv Data Imported Succesfully');
redirect(base_url().'Admin/menu');
} else
$data['error'] = "Error occured";
$this->load->view('csvindex', $data);
}
}
My model looks:
public function get_menu()
{
$query = $this->db->get('menu');
if ($query->num_rows() > 0)
{
return $query->result_array();
}
else
{
return FALSE;
}
}
public function insert_csv($data)
{
$this->db->insert('menu', $data);
}
Its works fine.
My requirement is, I want to update data from uploaded csv file. And I have to upload same csv file with updated data again. On that upload, if the csv data is already exists in table, I need to update that data and I need to insert into the table if their is any new record in updated csv file that uploaded.
what all modification should I done? Is there any other better way to make it possible?