1

have a great day with your code.

Today I'm stuck with spreadsheet_excel_reader and insert_batch() when your upload file xls and automatic insert into two database say table_one and table_two with the same file xls, and how to get different insert to the database or how to automatic insert to the two (table_one & table_two) database with only single file .xls.

when I'm google about documentation spreadsheet_excel_reader, just stuck and can't found anything.

this code,

if ($this->upload->do_upload('file')) {
        $data = $this->upload->data();
        // if ($data == empty(var)) {
        //  return base_url();
        // }else{
        //  $data = $this->upload->data();
        // }
        @chmod($data['full_path'], 0777);

        $this->load->library('Spreadsheet_Excel_Reader');
        $this->spreadsheet_excel_reader->setOutputEncoding('CP1251');

        $this->spreadsheet_excel_reader->read($data['full_path']);
        $sheets = $this->spreadsheet_excel_reader->sheets[0];
        error_reporting(0);

        $data_excel = array();
        for ($i = 2; $i <= $sheets['numRows']; $i++) {
            if ($sheets['cells'][$i][1] == '') break;
                $data_excel[$i - 1]['id_hari']    = $sheets['cells'][$i][1];
                $data_excel[$i - 1]['tanggal']   = $sheets['cells'][$i][2];
                $data_excel[$i - 1]['detail'] = $sheets['cells'][$i][3];
        }
        $this->db->insert_batch('table_two', $data_excel);  

        $data_excel2 = array();
        for ($i = 2; $i <= $sheets['numRows']; $i++) {
            if ($sheets['cells'][$i][2] == '') break;
                $data_excel[$i - 1]['senin']    = $sheets['cells'][$i][1];
                $data_excel[$i - 1]['selasa']   = $sheets['cells'][$i][2];
                $data_excel[$i - 1]['rabu'] = $sheets['cells'][$i][3];
        }
        $this->db->insert_batch('table_one', $data_excel2);  

        // @unlink($data['full_path']);
        redirect('excel-import');
        //memulai else
    }else{
        $this->session->set_flashdata('error', 'Please select xls/csv file');
        //selesai session
        redirect(base_url(),'refresh');
    }

I think just problem with only this code and how to make a logic when your insert one file .xls and logic automatic insert to the 2 table of database, if your need any code and other code i can edit this and add the other code for review.

$data_excel = array();
        for ($i = 2; $i <= $sheets['numRows']; $i++) {
            if ($sheets['cells'][$i][1] == '') break;
                $data_excel[$i - 1]['id_hari']    = $sheets['cells'][$i][1];
                $data_excel[$i - 1]['tanggal']   = $sheets['cells'][$i][2];
                $data_excel[$i - 1]['detail'] = $sheets['cells'][$i][3];
        }
        $this->db->insert_batch('table_two', $data_excel);  

        $data_excel2 = array();
        for ($i = 2; $i <= $sheets['numRows']; $i++) {
            if ($sheets['cells'][$i][2] == '') break;
                $data_excel[$i - 1]['senin']    = $sheets['cells'][$i][1];
                $data_excel[$i - 1]['selasa']   = $sheets['cells'][$i][2];
                $data_excel[$i - 1]['rabu'] = $sheets['cells'][$i][3];
        }
        $this->db->insert_batch('table_one', $data_excel2);  

        // @unlink($data['full_path']);
        redirect('excel-import');

Thank you

Casual Coder
  • 81
  • 12

0 Answers0