Я перебираю свои дочерние страницы и получаю таблицу, но теперь мне нужно отсортировать и суммировать рабочие часы для каждой недели.
Это мой код:
$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 }?>
Вывод выглядит так:
Как я уже объяснил, я пытаюсь суммировать часы для каждой недели.
Я пытался использовать операторы if и whileно не могу заставить его работать.
Есть предложения?
Спасибо!
РЕДАКТИРОВАТЬ:
Я разделяю недели:
$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 }
Теперь проблема в том, что я не могу получить сводку часов за каждую неделю.
Есть ли какие-либо указатели?
Спасибо!
РЕДАКТИРОВАТЬ:
$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 }?>