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

                $highestRow = $worksheet->getHighestRow();
                $row=2; $segl = 1 ;
                while($row<=$highestRow)
                {
                     $brand       =  $worksheet->getCellByColumnAndRow(1, $row)->getValue();
                     $kind        =  $worksheet->getCellByColumnAndRow(2, $row)->getValue();
                     $model       =  $worksheet->getCellByColumnAndRow(3, $row)->getValue();
                     $color       =  $worksheet->getCellByColumnAndRow(4, $row)->getValue();

                     $letters     =  $worksheet->getCellByColumnAndRow(5, $row)->getValue();
                     $numbers     =  $worksheet->getCellByColumnAndRow(6, $row)->getValue();
                     $skilton     =  $worksheet->getCellByColumnAndRow(7, $row)->getValue();
                     $lic_expire  =  $worksheet->getCellByColumnAndRow(8, $row)->getValue();
                     $costday     =  $worksheet->getCellByColumnAndRow(12, $row)->getValue();
                     $monthly     =  $worksheet->getCellByColumnAndRow(13, $row)->getValue();
                     $minn        =  $worksheet->getCellByColumnAndRow(14, $row)->getValue();
                     $km1         =  $worksheet->getCellByColumnAndRow(15, $row)->getValue();
                     $km2         =  $worksheet->getCellByColumnAndRow(16, $row)->getValue();
                     $km3         =  $worksheet->getCellByColumnAndRow(17, $row)->getValue();
                     $branch      =  $worksheet->getCellByColumnAndRow(20, $row)->getValue();
                     $total_cost  =  $worksheet->getCellByColumnAndRow(0, $row)->getValue();

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


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

                $sql = $db->prepare(" INSERT INTO cars(carid , brand, kind , model ,  color , letters ,numbers ,
                      skilton , lic_expire ,costday , monthly , minn , km1 , km2 , km3 ,  branch , total_cost )
                       VALUES (?, ?, ?, ?, ? , ?, ?,?, ?, ?, ?, ? , ?, ?,?, ?, ?)" );
                       $sql->bindParam(1,$maxD);
                       $sql->bindParam(2,$brand);
                       $sql->bindParam(3,$kind);
                       $sql->bindParam(4,$model);
                       $sql->bindParam(5,$color);
                       $sql->bindParam(6,$letters);
                       $sql->bindParam(7,$numbers);
                       $sql->bindParam(8,$skilton);
                       $sql->bindParam(9,$lic_date);
                       $sql->bindParam(10,$costday);
                       $sql->bindParam(11,$monthly);
                       $sql->bindParam(12,$minn);
                       $sql->bindParam(13,$km1);
                       $sql->bindParam(14,$km2);
                       $sql->bindParam(15,$km3);
                       $sql->bindParam(16,$branch);
                       $sql->bindParam(17,$total_cost);
                       $sql->execute() ;
                    $segl = $segl + 1 ;
                    $row =  $row + 1 ;
                    }
 }
 echo $segl ;
 }
 }

 ?>
