我有一个表,其记录使用数据库的结果填充,并使用foreach循环。 我想要几个单元格值的列与其他列合并。 像:
Leave Type | Max Days | Leave Taken | Leave Balance | Roll Over
Float | 3 | 2 | 1 |
Personal | 3 | 2 | 1 | 5
Sick | 3 | 2 | 1 |
Mourning | 3 | 2 | 1 |
使用foreach循环填充前四个记录,我需要单独计算第五列并附加它。
这是我的HTML:
<table class="table table-striped table-hover" id="leave_balance_table">
<thead>
<tr>
<th>Leave Type</th>
<th>Max Days</th>
<th>Leave Taken</th>
<th>Leave Balance</th>
<th>Roll Over</th>
</tr>
</thead>
<tbody>
<?php
$totalLeaveTaken = 0.00;
$totalBalance = 0.00;
$totalRows = count($GetEmployeeLeaveBalance);
foreach ($GetEmployeeLeaveBalance as $member):
$totalLeaveTaken += $member['usedDays'];
$totalBalance += $member['Remaining_Leave_Days'];
$leaveBalance = floatval($member['Remaining_Leave_Days']);
?>
<tr>
<td><?php echo $member['title']; ?></td>
<td><?php echo $member['maxDays']; ?></td>
<td><?php echo $member['usedDays']; ?></td>
<!-- <td><?php echo gettype($leaveBalance);?></td> -->
<td
<?=
($leaveBalance < 0) ?
"style='background-color:red;font-weight:bold;color:white;'" : ""
?>
>
<?php echo $member['Remaining_Leave_Days']; ?>
</td>
<!-- <td rowspan="<?= $totalRows; ?>">Some computed value</td> -->
</tr>
<?php endforeach; ?>
<tr>
<td></td>
<td></td>
<td style="background-color: #33CCFF; font-weight: bold;">Total: <?php echo number_format($totalLeaveTaken, 2); ?></td>
<td style="background-color: #33CCFF; font-weight: bold;">Total: <?php echo
number_format($totalBalance, 2); ?></td>
</tr>
</tbody>
</table>
如何从foreach循环中获取第五列并附加它?
答案 0 :(得分:2)
只需添加一个标记,可以帮助您检查是否已经打印了最后一列,如下所示:
$totalLeaveTaken = 0.00;
$totalBalance = 0.00;
$totalRows = count($GetEmployeeLeaveBalance);
$lastColumnPrinted = false; //Add This
foreach ($GetEmployeeLeaveBalance as $member):
$totalLeaveTaken += $member['usedDays'];
$totalBalance += $member['Remaining_Leave_Days'];
$leaveBalance = floatval($member['Remaining_Leave_Days']);
?>
<tr>
<td><?php echo $member['title']; ?></td>
<td><?php echo $member['maxDays']; ?></td>
<td><?php echo $member['usedDays']; ?></td>
<!-- <td><?php echo gettype($leaveBalance);?></td> -->
<td
<?=
($leaveBalance < 0) ?
"style='background-color:red;font-weight:bold;color:white;'" : ""
?>
>
<?php echo $member['Remaining_Leave_Days']; ?>
</td>
<!--Add The Line Below -->
<?php if($lastColumnPrinted == false): $lastColumnPrinted = true;?>
<td rowspan="<?= $totalRows; ?>">Some computed value</td>
<?php endif; ?>
</tr>
<?php endforeach; ?>