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>
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算