[英]PHP Extra row somewhere in my table using Whileloop
Well, I have a table with some dates and hours. 好吧,我有一张桌子,上面有一些日期和时间。 I get this data from my DB using a while loop. 我使用while循环从我的数据库中获取此数据。
I want to add 1 line after the date change to the next one, So I can calculate de day's total hours. 我想在日期更改后添加1行到下一行,所以我可以计算de day的总小时数。 I just need 1 row after every day change (picture below as example) 每天更换后我只需要1行(以下图片为例)
$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>
";
}
The below photo is an example of my table. 下面的照片是我的桌子的一个例子。 As you can see, I have the first 3 lines with the same date. 正如您所看到的,我有前3行具有相同的日期。 after these 3 lines, I want just 1 extra line where I can calculate the day total. 在这三行之后,我只想要一条额外的线,我可以计算一天的总数。
You can aggregate all the rows and subtotals before actually displaying them. 您可以在实际显示之前聚合所有行和小计。 This approach might take up a bit of memory, but it is very readable. 这种方法可能占用一些内存,但它非常易读。
// 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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.