1

I need to make 2 tables from this csv file. How can i split the file, after the second line,

This is how the csv file looks.

i've marked what i need as headers. i dont need the data from those lines.

Palle_nr;Varenummer;Ordre_nr;Operatoer;Maskin_nr

1234;1234_2019_01_14_17_11_23;1234; TN;1234

;;;;

Name;Datum;Property;Criterion;Type

1) Height 130;;avg(L)Y;;inspection_dimension_scalar

And this is how my code looks. again what i want this code to do is split the csv file after the second line. so i get a table with the Palle_nr and a table with the Name

if (isset($_POST['btn-upload'])){

copy("$sourcepath/$latest_filename","$copy/$latest_filename");

$row = 1;
if (($openfile = fopen("$copy/$latest_filename", "r")) !== FALSE) {

    $csvuser->createPalleTable($latest_filename);
    while ($getpdata = fgetcsv($openfile, 1000, ";")) {
        $getpdata = array_map("utf8_encode", $getpdata);
        $totalp = count($getpdata);

        $row++;

        for ($i=0; $i < $totalp; $i++) {
            $pdata = implode(";", $getpdata);
            $palledata = explode(";", $pdata);
        }
        $csvuser->insertPalleTable($latest_filename,$palledata);
        }

///////// This is where i want the file to split ///////

     $header = fgetcsv($openfile, 1000, ";");
     $header = array_map("utf8_encode", $header);
     $totalheader = count($header);
     for ($i=0; $i < $totalheader; $i++) {
         $headerdata = implode(";", $header);
         $th = explode(";", $headerdata);
         }

   $csvuser->createCsvTable($latest_filename);
   while ($getdata = fgetcsv($openfile, 1000, ";")) {
   $getdata = array_map("utf8_encode", $getdata);
   $total = count($getdata);

   $row++;

   for ($c=0; $c < $total; $c++) {
        $csvdata = implode(";", $getdata);
        $fncsvdata = explode(";", $csvdata);
    }
     $csvuser->insertCsvTable($latest_filename,$fncsvdata);
    }
  }
}  

If you need the whole code so you can recreate the problem. then ill upload it but thought it would be to much code.

Other ways of reading the file is also appreciated. i just need to be able to split the file.

ShakyIpsen
  • 79
  • 1
  • 9

1 Answers1

1

There is so much that I have to question in your code, but because your first four lines in the file are predictable, you can consume them without looping.

if (isset($_POST['btn-upload'])){
    copy("$sourcepath/$latest_filename","$copy/$latest_filename");

    if (($openfile = fopen("$copy/$latest_filename", "r")) !== false) {

        $header1 = fgetcsv($openfile, 1000, ";"); // consume, but don't use

        $csvuser->createPalleTable($latest_filename);
        $csvuser->insertPalleTable($latest_filename, array_map("utf8_encode", fgetcsv($openfile, 1000, ";")));

        $delimiting_row = fgetcsv($openfile, 1000, ";"); // consume, but don't use 

        $header2 = fgetcsv($openfile, 1000, ";"); // consume, but don't use

        $csvuser->createCsvTable($latest_filename);
        while ($getdata = fgetcsv($openfile, 1000, ";")) {
            $csvuser->insertCsvTable($latest_filename, array_map("utf8_encode", $getdata));
        }
    }
}

This is completely untested (and posted from my phone).

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • Can you tell my why this works? tried to echo out the variables, and the $header2 doesnt show anything. i think its the empty row. can you confirm? :) Thanks again! – ShakyIpsen Jan 22 '19 at 14:07
  • http://php.net/manual/en/function.fgetcsv.php returns an array, so `echo` won't do the trick. You can implode with a glue to echo, or you can use `var_export()` or `print_r()` or `var_dump()` to output the array data. When `fgetcsv()` is called, it extracts a single row/line, and advances the "pointer". Making these calls manually (outside of a loop) give you the ability to step through the known rows until you get to the 2nd section of data that has variable lines of data. – mickmackusa Jan 22 '19 at 19:33
  • Thanks alot for your help! :) – ShakyIpsen Jan 24 '19 at 09:06