Import excel file 2nd sheet into mysql

2020-02-28 php mysql excel phpexcelreader

i want to import excel into mysql. I can import the data which is the file only have 1 sheet, my code was like :

function import(){
if(isset($_FILES["file"]["name"])){
   $path = $_FILES["file"]["tmp_name"];
   $object = PHPExcel_IOFactory::load($path);
   foreach($object->getWorksheetIterator() as $worksheet){
    $highestRow = $worksheet->getHighestRow();
    $highestColumn = $worksheet->getHighestColumn();
    for($row=2; $row<=$highestRow; $row++){
         $Tanggal     = $worksheet->getCellByColumnAndRow(0, $row)->getValue();
         $Outlet      = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
         $Nama        = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
         $Email       = $worksheet->getCellByColumnAndRow(3, $row)->getValue();
         $No_tlp      = $worksheet->getCellByColumnAndRow(4, $row)->getValue();
         $Rasa        = $worksheet->getCellByColumnAndRow(5, $row)->getValue();
         $Porsi       = $worksheet->getCellByColumnAndRow(6, $row)->getValue();
         $Kecepatan   = $worksheet->getCellByColumnAndRow(7, $row)->getValue();
         $Keramahan   = $worksheet->getCellByColumnAndRow(8, $row)->getValue();
         $Total       = $worksheet->getCellByColumnAndRow(9, $row)->getValue();
         $Rekomendasi = $worksheet->getCellByColumnAndRow(10, $row)->getValue();
         $Komentar    = $worksheet->getCellByColumnAndRow(11, $row)->getValue();
         $data[]    = array(
                        'Tanggal'       => $Tanggal,
                        'Outlet'        => $Outlet,
                        'Nama'          => $Nama,
                        'Email'         => $Email,
                        'No_telp'       => $No_tlp,
                        'Rasa'          => $Rasa,
                        'Porsi'         => $Porsi,
                        'Kecepatan'     => $Kecepatan,
                        'Keramahan'     => $Pelayanan,
                        'Total'         => $Total,
                        'Rekomendasi'   => $Rekomendasi,
                        'Komentar'      => $Komentar); 
    }
   }    
   $this->M_FImport->insert($data);
   echo 'Data Imported successfully';
} 

}

Now, the excel have more than 1 sheets. For example, i want import data from second sheet only. What should i do?

$this->M_FImport->insert($data);

this line is my model. Here the code :

function insert($data){
    $this->db->insert_batch('Comment_card', $data);
}

This model for insert my data to mysql.

Answers

Related