<?php
 //export.php
 include "../actions/dbPDO.php";
 if(!empty($_FILES["excel_file"]))
 {
      $file_array = explode(".", $_FILES["excel_file"]["name"]);
      if($file_array[1] == "xls")
      {
           include("../op/PHPExcel/IOFactory.php");
          //  $reader = PHPExcel_IOFactory::createReader("Excel5");
           $object = PHPExcel_IOFactory::load($_FILES["excel_file"]["tmp_name"]);
           foreach($object->getWorksheetIterator() as $worksheet)
           {

                $highestRow = $worksheet->getHighestRow();
                $row=2; $segl = 1 ;
                while($row<=$highestRow)
                {
                      $cust_name    =  $worksheet->getCellByColumnAndRow(1, $row)->getValue();
                     $nation       =  $worksheet->getCellByColumnAndRow(2, $row)->getValue();
                     $idSA         =  $worksheet->getCellByColumnAndRow(3, $row)->getValue();
                     $id_From      =  $worksheet->getCellByColumnAndRow(4, $row)->getValue();

                     $id_Expire    =  $worksheet->getCellByColumnAndRow(5, $row)->getValue();
                     $address      =  $worksheet->getCellByColumnAndRow(6, $row)->getValue();
                     $license      =  $worksheet->getCellByColumnAndRow(7, $row)->getValue();
                     $lic_from     =  $worksheet->getCellByColumnAndRow(8, $row)->getValue();
                     $lic_expire   =  $worksheet->getCellByColumnAndRow(9, $row)->getValue();
                     $phone        =  $worksheet->getCellByColumnAndRow(10, $row)->getValue();
                     $phone2       =  $worksheet->getCellByColumnAndRow(11, $row)->getValue();
                     $working_lic  =  $worksheet->getCellByColumnAndRow(12, $row)->getValue();
                     $w_phone      =  $worksheet->getCellByColumnAndRow(13, $row)->getValue();
                     $working_woner =  $worksheet->getCellByColumnAndRow(15, $row)->getValue();
                     $working_address    =  $worksheet->getCellByColumnAndRow(16, $row)->getValue();
                     $w_phon2      =  $worksheet->getCellByColumnAndRow(17, $row)->getValue();
                     $dgree        =  $worksheet->getCellByColumnAndRow(25, $row)->getValue();
                     $comm         =  $worksheet->getCellByColumnAndRow(26, $row)->getValue();
                     $branchID     =  $worksheet->getCellByColumnAndRow(27, $row)->getValue();
                     $start        =  $worksheet->getCellByColumnAndRow(28, $row)->getValue();

                     $id_date     = date('Y-m-d', PHPExcel_Shared_Date::ExcelToPHP($id_Expire));
                     $lic_date    = date('Y-m-d', PHPExcel_Shared_Date::ExcelToPHP($lic_expire));
                     $start_date  = date('Y-m-d', PHPExcel_Shared_Date::ExcelToPHP($start));

//max_execution_time=30
                    //  $sql = $db->prepare("SELECT idSA , cust_name FROM customers WHERE idSA = '$idSA' OR cust_name = '$cust_name' LIMIT 1");
                    //  $sql->execute();
                    //  if($sql->rowCount()>0){continue ; }

                     $maxsql= $db->prepare("SELECT max(cust_id) as maxd FROM customers");
                      $maxsql->execute();
                      if($maxsql->rowCount()>0){$result = $maxsql->fetch(PDO::FETCH_ASSOC);
                          $maxD = $result['maxd']+1;} else {$maxD = 1 ; }

                $sql = $db->prepare(" INSERT INTO customers(cust_id , cust_name, nation, idSA , id_From, id_Expire , address ,
                      license , lic_from , lic_expire , phone , phone2 , working_lic , w_phone , working_woner , working_address , w_phone2 ,
                    dgree , comm , branch , start )
                       VALUES (?, ?, ?, ?, ? , ?, ?,?, ?, ?, ?, ? , ?, ?,?, ?, ?, ?, ? , ?, ?)" );
                       $sql->bindParam(1,$maxD);
                       $sql->bindParam(2,$cust_name);
                       $sql->bindParam(3,$nation);
                       $sql->bindParam(4,$idSA);
                       $sql->bindParam(5,$id_From);
                       $sql->bindParam(6,$id_date);
                       $sql->bindParam(7,$address);
                       $sql->bindParam(8,$license);
                       $sql->bindParam(9,$lic_from);
                       $sql->bindParam(10,$lic_date);
                       $sql->bindParam(11,$phone);
                       $sql->bindParam(12,$phone2);
                       $sql->bindParam(13,$working_lic);
                       $sql->bindParam(14,$w_phone);
                       $sql->bindParam(15,$working_woner);
                       $sql->bindParam(16,$working_address);
                       $sql->bindParam(17,$w_phone2);
                       $sql->bindParam(18,$dgree);
                       $sql->bindParam(19,$comm);
                       $sql->bindParam(20,$branch);
                       $sql->bindParam(21,$start_date);
                       $sql->execute() ;
                    $segl = $segl + 1 ;
                    $row =  $row + 1 ;
                    }
 }
 echo $segl ;
 }
 }

 ?>
