-3

i have trouble with looping fputcsv, I get just the last row of my opened packing.csv file. What do you think i'am doing wrong ? I think the fputcsv is in the while loop or im wrong ?

$row = 1;
if (($handle = fopen("packing.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $empWroked    = $data[Name];
        $dateWroked    = $data[Dated];
        $startTime          = $data[Start];
        $endTime            =  $data[Finish];
        $itemPacked         = $data[Item];
        $nameEmp            = $data[Employee];
        $startParts         = explode(":", $startTime);
        $endParts           = explode(":", $endTime);
        $startMinute        = (int)$startParts[1];
        $startHour          = (int)$startParts[0];
        $endMinute          = (int)$endParts[1];
        $endHour            = (int)$endParts[0];
        $differenceHour     = ($endHour - $startHour);
        $differenceMinute   = ($endMinute - $startMinute);
        $differenceHour     -= (($differenceMinute < 0) ? 1 : 0);

        if ($differenceMinute < 0) 
            $differenceMinute += 60;

        $difference = $differenceHour * 60 + $differenceMinute;

        if (!isset($sumArray1[$nameEmp])) {
            $sumArray1[$nameEmp] = 0; // create new entry for nameEmp with balance=0
        }

        if (!isset($sumArray[$nameEmp])) {
            $sumArray[$nameEmp] = 0; // create new entry for nameEmp with balance=0     
        }   

        $dataEff  =  [$empWroked ,   $itemPacked ,  round(($perMin * $sumArray[$nameEmp]  += $itemPacked ) / ($sumArray1[$nameEmp]  += $difference / $precentEff), 2)];

        $out = fopen('packingresults.csv', 'w');

        fputcsv($out, $dataEff , ',', ' ');
    }

    fclose($handle);
    fclose($out);
}
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
stoperan
  • 31
  • 11
  • What have you tried to resolve the problem? Where are you stuck? Have you checked whether setting `'w'` to `'w+'` resolves the problem? – Nico Haase Sep 02 '21 at 07:49
  • Have a look at https://stackoverflow.com/questions/21113919/difference-between-r-and-w-in-fopen for an explanation – Nico Haase Sep 02 '21 at 07:50
  • i checked that is not that , im sure is something with the loop but , i can't see it . it same with both w+ and w – stoperan Sep 02 '21 at 07:53
  • 5
    The problem is that you are opening the output file _inside_ the loop. And mode `w` means, quote manual: _"Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length."_ – CBroe Sep 02 '21 at 07:56
  • @CBroe why it's not working with w+ then ? – stoperan Sep 02 '21 at 07:57
  • If you think this is related to the loop, what have you tried to check this? What happens if you debug the loop, either using vardump or using a proper debugger? – Nico Haase Sep 02 '21 at 08:01

1 Answers1

1

thank you for your big help :) I found the answer , it's not either w or w+ or r+ , it's a "a" . I hope this help somebody in a future :)

$out = fopen('packingresults.csv', 'a');

    fputcsv($out, $dataEff );


    }
fclose($out);
fclose($handle);





}


    fclose($handle);
    fclose($out);
}
stoperan
  • 31
  • 11