Search code examples
phploopssumcounternested-loops

Adding subtotal PHP foreach loop


i have been stuck on this and reaching for help. I believe i need to add an additional loop, or counter, i just don't know when and where :)

I am trying to accomplish a subtotal of cash, CC and total for each employeeid.

foreach ($weekly as $week){
        $week_array = getStartAndEndDate($week['week'],$week['year']);
        if(isset($week['employeeid'])){
                echo "<TR>";
                if($week['employeeid']!=$tmpEmp){
                        echo "<TD colspan='5'><strong>".$week['employeeid']."</strong></TD></TR>
                        <TR><TD>".$week_array['week_start']."</TD>";
                        $tmpEmp = $week['employeeid'];
                }
                else {
                        echo "<TD>".$week_array['week_start']."</TD>";
                        $tmpEmp = $week['employeeid'];
                }      
                echo
                "<TD>".$week['CC']."</TD>
                <TD>".$week['Cash']."</TD>
                <TD>".$week['total']."</TD>
                <TD><a href=employee.php?week=".$week['week']."&year=".$week['year'].">Report</a></TD>
                </TR>";


                $tmpCC += $week['CC'];
                $tmpCash += $week['Cash'];
                $tmpTotal += $week['total'];


        }      

}
                echo "<TR><TD><strong>Group Total</strong></TD>
                <TD>".$tmpCC."</TD>
                <TD>".$tmpCash."</TD>
                <TD>".$tmpTotal."</TD>
                <TD></TD>
                </TR>
                 </TABLE><HR />";

---Current RESULT---

Employee        CC      Cash    Total   View Report
1
2014-10-20      0.00    271.61  271.61  Report
2
2014-10-06      75.38   0.00    75.38   Report
2014-10-13      0.00    472.66  472.66  Report
4
2014-09-29      219.39  0.00    219.39  Report
5
2014-09-29      0.00    464.40  464.40  Report
2014-10-20      390.37  0.00    390.37  Report
Group Total     685.14  1208.67 1893.81

---Wanted RESULT---

Employee        CC      Cash    Total   View Report
1
2014-10-20      0.00    271.61  271.61  Report
Subtotal        XX      XX      XX     
2
2014-10-06      75.38   0.00    75.38   Report
2014-10-13      0.00    472.66  472.66  Report
Subtotal        XX      XX      XX
4
2014-09-29      219.39  0.00    219.39  Report
Subtotal        XX      XX      XX
5
2014-09-29      0.00    464.40  464.40  Report
2014-10-20      390.37  0.00    390.37  Report
Subtotal        XX      XX      XX
Group Total     685.14  1208.67 1893.81

Solution

  • Similar to the way that you hold on to the employee ID across loops, you could keep a running total.

    Above your loop:

    $tmpSubTotal = 0;
    

    After $tmpTotal += $week['total'];:

    $tmpSubTotal += $week['total'];
    

    And the if statement:

    if($week['employeeid']!=$tmpEmp){
        if($tmpEmp) {
            echo '<td colspan="3"></td><td>' . $tmpSubTotal . '</td><td></td></tr><tr>';
            $tmpSubTotal = 0;
        }
    
        echo "<TD colspan='5'><strong>".$week['employeeid']."</strong></TD></TR>
        <TR><TD>".$week_array['week_start']."</TD>";
        $tmpEmp = $week['employeeid'];
    }