我在子页面中循环浏览,我得到了表格,但是现在我需要对每周的工作时间进行排序和汇总。
到目前为止,这是我的代码:
$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 }?>
答案 0 :(得分:1)
一种简单易用的每周“加和”小时数的方法是使用一个数组,该数组以星期为键,值作为小时总和。
在您的示例中,我将在您的“ foreach”中执行以下操作:
<?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 }?>
所以我在这里要做的是将小时数存储在数组中,即您一周的每个键上。
为避免某些PHP警告,您可能希望确保在向数组添加值之前已创建数组上的键。
希望有道理。
编辑:为了更新您的代码,解决您的问题以及稍微清理您的代码,我认为这将为您做到这一点。
<?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
}
?>