2

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

1 Answer 1

2

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'];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply, but it is not quiet what i am looking for. The results show a subtotal on the very first line, and no subtotal for the last employee. --Result-- postimg.org/image/5g3xn7txh
The statement I included (if($tmpEmp)) is trying to exclude the case where it tries to print a row on the very first line, but I don't know the value of $tmpEmp before the loop begins. It should be set to false or atleast something falsy. The last row was a mistake on my part, you will need to include the following after the loop but before you echo the grouped totals: echo '<tr><td colspan="3"></td><td>' . $tmpSubTotal . '</td><td></td></tr>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.