Im looping through my childpages and I get the table but now I need to sort and sum the working hours for each week.
This is my code so far:
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$dow = date('w', strtotime($date));
$week = (int)$week;
?>
<tr>
<td><b>(<?php echo $week;?>) <?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }?>
As I explained I'm trying to sum the hours for each week.
I have tried with if and while statements but can't get it to work.
Any suggestions?
Thank you!
EDIT:
I get the weeks separated:
$last_date =null;
$weeklyhours = 0;
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$week = (int)$week;
?>
<?php
if ( $last_date != $week ) {
$weeklyhours = $h->worktime + $weeklyhours;?>
<tr style="background: #f8f8f8;">
<td>Vecka: <?= $week;?></td>
<td></td>
<td></td>
<td><?= $weeklyhours;?></td>
<td></td>
</tr>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
else if ( $last_date == $week ){
$weeklyhours = $h->worktime + $weeklyhours;
?>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
The problem now is that I can't get the summary of the hours per each week.
Any pointers?
Thanks!
EDIT:
$weeklyhours = 0;
// NEW LINE HERE - Create an array to use.
$hoursPerWeek = [];
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$week = (int)$week;
// NEW LINE HERE - Add to array.
$hoursPerWeek[$week] += $h->worktime;
?>
<?php
if ( $last_date != $week ) {?>
<tr style="background: #f8f8f8;">
<td>Vecka: <?= $week;?></td>
<td></td>
<td></td>
<td><?= $hoursPerWeek[$week];?></td>
<td></td>
</tr>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
else if ( $last_date == $week ){?>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
//echo $h->workdate."-".$weeklyhours;
$last_date = $week;
?>
<?php }?>
A simple and easy way to "sum" the hours per week is by having an array that has the week as the key, and the value as the sum of hours.
In your example i would in your 'foreach' do following:
<?php
// NEW LINE HERE - Create an array to use.
$hoursPerWeek = [];
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$dow = date('w', strtotime($date));
$week = (int)$week;
// NEW LINE HERE - Add to array.
$hoursPerWeek[$week] += $h->worktime;
?>
<tr>
<td><b>(<?php echo $week;?>) <?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }?>
So what I'm doing here is to store the hours in an array, on each key which is your week.
To avoid some PHP warnings, you might wanne make sure key on the array is created before you add value to it..
Hope it makes sense.
EDIT: To update your code, to fix your problem, and also to clean your code abit, I think this would do it for you.
<?php
// Our data source.
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
// We need to loop through the array first to use the summed up value later.
$hoursPerWeek = [];
foreach ($hours as $h) {
$hoursPerWeek[$week] += $h->worktime;
}
$last_date = 0;
$weeklyhours = 0;
// Loop hours again, now make the output.
foreach ($hours as $h) {
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$week = (int)$week;
// Previous, was the last of that week.
if ($last_date != $week) {
?>
<tr style="background: #f8f8f8;">
<td>Vecka: <?= $week;?></td>
<td></td>
<td></td>
<td><?=$hoursPerWeek[$week];?></td>
<td></td>
</tr>
<?php
}
$last_date = $week;
// Create a output for our current hourlog.
?>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php
}
?>