<?php
require('client-assets/php/spreadsheet/php-excel-reader/excel_reader2.php');
require('client-assets/php/spreadsheet/SpreadsheetReader.php');

    $dbHost = "localhost";
	$dbDatabase = "cropmonitor";
	$dbPasswrod = "brvnCcaEYxlPCS3";
	$dbUser = "cropmonitor";
	$mysqli = new mysqli($dbHost, $dbUser, $dbPasswrod, $dbDatabase);
	//$con = mysqli_connect("localhost", "cropmonitor", "brvnCcaEYxlPCS3", "cropmonitor");


if(isset($_POST['Submit'])){

  $mimes = ['application/vnd.ms-excel','text/xls','text/xlsx','text/csv','application/vnd.oasis.opendocument.spreadsheet'];
  if(in_array($_FILES["file"]["type"],$mimes)){

    $uploadFilePath = 'client-assets/uploads/'.basename($_FILES['file']['name']);  //need to add individual folders for clients.
    move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath);

    $Reader = new SpreadsheetReader($uploadFilePath);

    $totalSheet = count($Reader->sheets());

    echo "You have total ".$totalSheet." sheets".

    $html="<table border='1'>";

    /* For Loop for all sheets */
    for($i=0;$i<$totalSheet;$i++){

      $Reader->ChangeSheet($i);

      foreach ($Reader as $Row)
      {
        $html.="<tr>";
        $title = isset($Row[0]) ? $Row[0] : '';
        $description = isset($Row[1]) ? $Row[1] : '';
        $html.="<td>".id."</td>";
        $html.="<td>".analysis_type."</td>";
        $html.="<td>".lab_no."</td>";
        $html.="<td>".batch_no."</td>";
        $html.="<td>".sample_id."</td>";
        $html.="<td>".site_id."</td>";
        $html.="<td>".crop."</td>";
        $html.="<td>".date_sampled."</td>";
        $html.="<td>".lab_no."</td>";
        $html.="<td>".batch_no."</td>";
        $html.="<td>".sample_id."</td>";
        $html.="<td>".site_id."</td>";
        $html.="<td>".tec."</td>";
        $html.="<td>".cec."</td>";
        $html.="<td>".texture."</td>";
        $html.="<td>".gravel."</td>";
        $html.="<td>".colour."</td>";
        $html.="<td>".NO3_N."</td>";
        $html.="<td>".NH3_N."</td>";
        $html.="<td>".p_mehlick."</td>";
        $html.="<td>".p_bray2."</td>";
        $html.="<td>".p_morgan."</td>";
        $html.="<td>".k_morgan."</td>";
        $html.="<td>".ca_morgan."</td>";
        $html.="<td>".mg_morgan."</td>";
        $html.="<td>".na_morgan."</td>";
        $html.="<td>".ch_h2o."</td>";
        $html.="<td>".ocarbon."</td>";
        $html.="<td>".omatter."</td>";
        $html.="<td>".fe."</td>";
        $html.="<td>".ec."</td>";
        $html.="<td>".ph_cacl2."</td>";
        $html.="<td>".ph_h2o."</td>";
        $html.="<td>".paramag."</td>";
        $html.="<td>".s_morgan."</td>";
        $html.="<td>".b_cacl2."</td>";
        $html.="<td>".mn_dtpa."</td>";
        $html.="<td>".zn_dtpa."</td>";
        $html.="<td>".fe_dtpa."</td>";
        $html.="<td>".cu_dtpa."</td>";
        $html.="<td>".al."</td>";
        $html.="<td>".sl_cacl2."</td>";
        $html.="<td>".m_dtpa."</td>";
        $html.="<td>".co_dtpa."</td>";
        $html.="<td>".se."</td>";
        $html.="<td>".ca_mehlick3."</td>";
        $html.="<td>".mg_mehlick3."</td>";
        $html.="<td>".k_mehlick3."</td>";
        $html.="<td>".na_mehlick3."</td>";
        $html.="<td>".al_mehlick3."</td>";
        $html.="</tr>";

        $query = "insert into soil_records(
            analysis_type,
            lab_no,
            batch_no,
            sample_id,
            site_id,
            crop,
            date_sampled,
            lab_no,
            batch_no,
            sample_id,
            site_id,
            tec,
            cec,
            texture,
            gravel,
            colour,
            NO3_N,
            NH3_N,
            p_mehlick,
            p_bray2,
            p_morgan,
            k_morgan,
            ca_morgan,
            mg_morgan,
            na_morgan,
            ch_h2o,
            ocarbon,
            omatter,
            fe,
            ec,
            ph_cacl2,
            ph_h2o,
            paramag,
            s_morgan,
            b_cacl2,
            mn_dtpa,
            zn_dtpa,
            fe_dtpa,
            cu_dtpa,
            al,
            sl_cacl2,
            m_dtpa,
            co_dtpa,
            se,
            ca_mehlick3,
            mg_mehlick3,
            k_mehlick3,
            na_mehlick3,
            al_mehlick3
        ) values(
            '" . $analysis_type . "',
            '" . $lab_no . "',
            '" . $batch_no . "',
            '" . $sample_id . "',
            '" . $site_id . "',
            '" . $crop . "',
            '" . $date_sampled . "',
            '" . $lab_no . "',
            '" . $batch_no . "',
            '" . $sample_id . "',
            '" . $site_id . "',
            '" . $tec . "',
            '" . $cec . "',
            '" . $texture . "',
            '" . $gravel . "',
            '" . $colour . "',
            '" . $NO3_N . "',
            '" . $NH3_N . "',
            '" . $p_mehlick . "',
            '" . $p_bray2 . "',
            '" . $p_morgan . "',
            '" . $k_morgan . "',
            '" . $ca_morgan . "',
            '" . $mg_morgan . "',
            '" . $na_morgan . "',
            '" . $ch_h2o . "',
            '" . $ocarbon . "',
            '" . $omatter . "',
            '" . $fe . "',
            '" . $ec . "',
            '" . $ph_cacl2 . "',
            '" . $ph_h2o . "',
            '" . $paramag . "',
            '" . $s_morgan . "',
            '" . $b_cacl2 . "',
            '" . $mn_dtpa . "',
            '" . $zn_dtpa . "',
            '" . $fe_dtpa . "',
            '" . $cu_dtpa . "',
            '" . $al . "',
            '" . $sl_cacl2 . "',
            '" . $m_dtpa . "',
            '" . $co_dtpa . "',
            '" . $se . "',
            '" . $ca_mehlick3 . "',
            '" . $mg_mehlick3 . "',
            '" . $k_mehlick3 . "',
            '" . $na_mehlick3 . "',
            '" . $al_mehlick3 . "'
        )";

        $mysqli->query($query);
       }

    }

    $html.="</table>";
    echo $html;
    echo "<br />Data Inserted in dababase";

  }else { 
    die("<br/>Sorry, Incorrect file."); 
  }

}
?>