John Davidson

function SUM not working on CSV file in PHP

0 comments
Message:


its again me. I made my code to tried to made my code to calculate ID's total SUM
i wrote this code , but the part where i get to echo that $sumArray[$nameEmp] += $itemPacked;
i dont the get total SUM i get again the ID's single numbers . Instead of Total 78 and 155 , i get
the items as the 3rd row.. Where is my mistake ....


Thank you in advance


<?php
$row = 1;
if (($handle = fopen("packing.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$row++;
$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;

$sumArray = array();

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

echo "<br>\n". $sumArray[$nameEmp] += $itemPacked ;// add balance for NameEmp


$pickedRate = $perMin * $itemPacked;
$perCal = $pickedRate / $difference *$precentEff;

echo $nameEmp. "&nbsp;&nbsp;&nbsp;&nbsp;" . $itemPacked. "&nbsp;&nbsp;&nbsp;&nbsp;" . round($perCal, 0) . "%" . "<br>";
}

}
fclose($handle);

}
?>

Output 
Total for Each ID ID Item picked Perc
40 11 40 100%
3 23 32 80%
26 11 26 65%
12 11 12 75%
123 23 123 196%


Instead of these output i have , i need Output like this



Total for Each ID ID Item picked Perc
11 40 100%
23 32 80%
11 26 65%
78 11 12 75%
155 23 123 196%




My Answer:
It looks like you are trying to calculate the total sum of items picked for each employee ID. However, the issue is that you are echoing the sum before calculating it.

You should move the echo statement outside of the loop where you calculate the sum for each employee ID. Here is the updated code:

php
$row = 1;
$sumArray = array();

if (($handle = fopen("packing.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$row++;
$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($sumArray[$nameEmp])) {
$sumArray[$nameEmp] = 0; // create new entry for nameEmp with balance=0
}

$sumArray[$nameEmp] += $itemPacked; // add balance for NameEmp

$pickedRate = $perMin * $itemPacked;
$perCal = $pickedRate / $difference * $precentEff;

echo $nameEmp. "    " . $itemPacked. "    " . round($perCal, 0) . "%" . "
";
}

// Output the total sum for each employee ID
foreach ($sumArray as $nameEmp => $total) {
echo $total . "    " . $nameEmp . "
";
}

fclose($handle);
}


With this code, the total sum for each employee ID will be calculated and stored in the `$sumArray`. Then, after the loop, you can iterate over the `$sumArray` and echo out the total sum for each employee ID.

Rate this post

4 of 5 based on 1530 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us