我循环浏览我的子页面并得到表格,但现在我需要对每周的工作时间进行排序和汇总。
到目前为止,这是我的代码:
$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 }?>