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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化