好吧,我有一张桌子,上面有一些日期和时间。我使用while循环从数据库中获取数据。
我想在日期更改后将1行添加到下一行,这样我就可以计算出de day的总小时数。每天更换一次后,我只需要排一列(例如下面的图片)
$stmt5->execute();
while($row = $stmt5->fetch(PDO::FETCH_ASSOC)){
if($gewerkt == 0){
$gewerkt = $row['HOURS_WORKED'];
}else{
$gewerkt = $gewerkt + $row['HOURS_WORKED'];
}
echo "
<td>" .$row['TRANSACTION_ID']."</td>
<td>" .$row['RESOURCE_ID']."</td>
<td></td>
<td>" .$row['DEPARTMENT_ID']."</td>
<td>" .$row['WORKORDER_BASE_ID']."/".$row['WORKORDER_LOT_ID'].".".$row['WORKORDER_SPLIT_ID']."-".$row['WORKORDER_SUB_ID'].":".$row['OPERATION_SEQ_NO']."</td>
<td>" .date('Y-m-d', strtotime($row['TRANSACTION_DATE']))."</td>
<td>" .TimeValue($row['CLOCK_IN'])."</td>
<td>" .TimeValue($row['CLOCK_OUT'])."</td>
<td>" .floatval($row['HOURS_BREAK'])."</td>
<td>" .floatval($row['HOURS_WORKED'])."</td>
<td></td>
<td><form action='' method='POST'>
<button class='btn btn-default' type='submit' name='delete'>Verwijderen</button>
</form></td>
</tbody>
";
}
下面的照片是我的桌子的一个例子。如您所见,我的前三行具有相同的日期。在这3行之后,我只希望再增加1行就可以计算出一天的总费用。
答案 0 :(得分:0)
您可以在实际显示所有行和小计之前对其进行汇总。这种方法可能会占用一些内存,但是可读性很强。
// aggregate the rows first
while($row = $stmt5->fetch(PDO::FETCH_ASSOC)){
$row['ROW_DATE'] = date('Y-m-d', strtotime($row['TRANSACTION_DATE']));
$dateRows[$row['ROW_DATE']]['rows'][] = $row; // aggregate a 2D array
$dateRows[$row['ROW_DATE']]['HOURS_WORKED'] = isset($dateRows[$row['ROW_DATE']]['HOURS_WORKED'])
? $dateRows[$row['ROW_DATE']]['HOURS_WORKED'] + $row['HOURS_WORKED']
: $row['HOURS_WORKED'];
}
foreach ($dateRows as $date => $rows) {
// loop through all the rows of the day
foreach ($rows['rows'] as $row) {
echo "
<td>" .$row['TRANSACTION_ID']."</td>
<td>" .$row['RESOURCE_ID']."</td>
<td></td>
<td>" .$row['DEPARTMENT_ID']."</td>
<td>" .$row['WORKORDER_BASE_ID']."/".$row['WORKORDER_LOT_ID'].".".$row['WORKORDER_SPLIT_ID']."-".$row['WORKORDER_SUB_ID'].":".$row['OPERATION_SEQ_NO']."</td>
<td>" .$date."</td>
<td>" .TimeValue($row['CLOCK_IN'])."</td>
<td>" .TimeValue($row['CLOCK_OUT'])."</td>
<td>" .floatval($row['HOURS_BREAK'])."</td>
<td>" .floatval($row['HOURS_WORKED'])."</td>
<td></td>
<td>
<form action='' method='POST'>
<button class='btn btn-default' type='submit' name='delete'>Verwijderen</button>
</form>
</td>\n";
}
// display a subtotal row
echo "
<td colspan='9'>Subtotal:</td>
<td>{$rows['HOURS_WORKED']}</td>
<td></td>
<td></td>\n";
}