Welcome Guest, Not a member yet? Register   Sign In
Problem with inserting csv file entries to db
#1

What i'm facing here has kinda confused me.
I'm trying to insert some entries parsed from csv file into db. I found the csv library, it works and so far so good.

I have the following script in controller: "Products/insert",

Code:
if ($this->csvimport->get_array($file_path)) {
$csv_array = $this->csvimport->get_array($file_path);
  foreach ($csv_array as $row) {
   $insert_data = [];
   $insert_data['product_id']    = $row['id'];
   $insert_data['model'    ]    = $row['ean'];
   $insert_data['stock']    = $row['stock'];
   $insert_data['price']    = $row['price_w_o_vat'];
   $insert_data['noise']    = $row['noise'];
   $insert_data['warehouse']    = $row['warehouse'];
   $insert_data['minimum']    = 1;
   $insert_data['supplier']    = 1;
   $insert_data['image']    = $row['photo'];
   $insert_data['created_at']    = date('Y-m-d H:i:s');

   $manuf_exists = $this->product_model->manufacturer_exists($row['manufacturer']);
   if($manuf_exists !== FALSE){
    $insert_data['manufacturer_id'] = $manuf_exists;
   } else {
    $manufacturer_data['manufacturer_name'] = $row['manufacturer'];
    $manufacturer_data['manufacturer_slug'] = strtolower($row['manufacturer'].'-tyres');
    $add_manuf = $this->product_model->add_manufacturer($manufacturer_data);
    $insert_data['manufacturer_id'] = $add_manuf['manufacturer_id'];
   }
    $this->product_model->insert_product($insert_data);
}
                
}

And here's my model:
Code:
    public function insert_product($data){
        $query = $this->db->insert('products', $data);
        if($query){
            return true;
        } else {
            return false;
        }
    }

    public function manufacturer_exists($manufacturer){
        $this->db->select('manufacturer_id');
        $this->db->where('manufacturer_name', $manufacturer);
        $query = $this->db->get('manufacturers');
        if($query->num_rows() > 0){
            return $query->row()->manufacturer_id;
        } else {
            return false;
        }
    }

    public function add_manufacturer($data){
        $insert = $this->db->insert('manufacturers', $data);
        if($insert){
            return $this->db->insert_id();
        } else {
            return false;
        }
    }

I've also tried batch_insert but i can't check other tables and return the id. If anyone has ever made it, please feel free to suggest a solution on this..

Thanks in advance...

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply


Messages In This Thread
Problem with inserting csv file entries to db - by HarrysR - 05-16-2019, 08:02 AM



Theme © iAndrew 2016 - Forum software by © MyBB