dpauf28808 2019-03-22 05:10
浏览 57
已采纳

使用While循环在我的表格中的某个地方添加PHP

Well, I have a table with some dates and hours. I get this data from my DB using a while loop.

I want to add 1 line after the date change to the next one, So I can calculate de day's total hours. I just need 1 row after every day change (picture below as example)

            $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. after these 3 lines, I want just 1 extra line where I can calculate the day total.

The photo

  • 写回答

1条回答 默认 最新

  • doulun1915 2019-03-22 05:24
    关注

    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>
    ";
        }
    
        // display a subtotal row
        echo "
        <td colspan='9'>Subtotal:</td>
        <td>{$rows['HOURS_WORKED']}</td>
        <td></td>
        <td></td>
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么