【问题标题】:merge a column with one cell that rowspans other rows with foreach loop将一列与一个单元格合并,该单元格使用 foreach 循环跨越其他行
【发布时间】:2017-11-16 19:52:58
【问题描述】:

我有一个表,其记录由数据库的结果填充,并且使用了 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 循环中取出第五列并附加它?

【问题讨论】:

    标签: php html foreach html-table


    【解决方案1】:

    只需添加一个标志,它可以帮助您检查您是否已经打印了最后一列,如下所示:

    $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; ?>
    

    【讨论】:

    • 别提了兄弟:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多