duanlan3598 2015-09-11 20:43
浏览 16

合并PHP表中的列的值

I have a table and i want to merge the value of Total column for each month.

This is my code :

<table border="1" cellpadding="3" cellspacing="0" class="table_content">
   <tr>
      <td width="38" rowspan="2">NO.</td>
      <td rowspan="2">PILOT ON BOARD</td>
      <td colspan="2">PILOT ON BOARD</td>
      <td rowspan="2">PILOT WAITING TIME</td>
      <td rowspan="2">TOTAL</td>
      <td rowspan="2">BERTHING</td>
   </tr>
   <tr>
      <td width="100">DATE </td>
      <td width="100">TIME</td>
   </tr>

   <?php
        $no=0;
        $dataFromTable=json_decode('[{"PilotWaitingTime":"0.2","PilotOnBoardDate":"2013-01-01","PilotOnBoardTime":"17:40:00","PilotOnBoard":"2013-01-01 17:40:00","Berthing":"2013-01-01 20:25:00","rata2PilotWaitingTime":"9.067"},{"PilotWaitingTime":"19.2","PilotOnBoardDate":"2013-01-03","PilotOnBoardTime":"12:15:00","PilotOnBoard":"2013-01-03 12:15:00","Berthing":"2013-01-03 15:10:00","rata2PilotWaitingTime":"9.067"},{"PilotWaitingTime":"7.8","PilotOnBoardDate":"2013-01-03","PilotOnBoardTime":"14:45:00","PilotOnBoard":"2013-01-03 14:45:00","Berthing":"2013-01-03 17:05:00","rata2PilotWaitingTime":"9.067"},{"PilotWaitingTime":"6.4","PilotOnBoardDate":"2013-02-04","PilotOnBoardTime":"18:25:00","PilotOnBoard":"2013-02-04 18:25:00","Berthing":"2013-02-04 20:25:00","rata2PilotWaitingTime":"6.000"},{"PilotWaitingTime":"5.6","PilotOnBoardDate":"2013-02-06","PilotOnBoardTime":"05:40:00","PilotOnBoard":"2013-02-06 05:40:00","Berthing":"2013-02-06 08:30:00","rata2PilotWaitingTime":"6.000"},{"PilotWaitingTime":"26","PilotOnBoardDate":"2013-03-04","PilotOnBoardTime":"13:00:00","PilotOnBoard":"2013-03-04 13:00:00","Berthing":"2013-02-04 20:25:00","rata2PilotWaitingTime":"26.000"}]',true);

        foreach( $dataFromTable as $results ){
   ?>

   <tr>
      <td width="38" style="text-align:right"><?php echo $no+=1; ?></td>
      <td width="150"><?php echo $results['PilotOnBoard']; ?></td>
      <td width="100"><?php echo $results['PilotOnBoardDate']; ?> </td>
      <td width="100"><?php echo $results['PilotOnBoardTime']; ?></td>
      <td style="text-align:right"><?php echo $results['PilotWaitingTime']; ?></td>
      <td style="text-align:right"><?php echo $results['rata2PilotWaitingTime']; ?></td>
      <td style="text-align:right"><?php echo $results['Berthing']; ?></td>
   </tr>

   <?php }; ?>

</table>

This is the result:

PilotOnBoard            PilotOnBoardDate PilotOnBoardTime   PilotWaitingTime    Total   Berthing
2013-01-01 17:40:00     2013-01-01       17:40:00                        0.2     9.067  2013-01-01 20:25:00
2013-01-03 12:15:00     2013-01-03       12:15:00                       19.2     9.067  2013-01-03 15:10:00
2013-01-03 14:45:00     2013-01-03       14:45:00                        7.8     9.067  2013-01-03 17:05:00
2013-02-04 18:25:00     2013-02-04       18:25:00                        6.4     6.000  2013-02-04 20:25:00
2013-02-06 05:40:00     2013-02-06       05:40:00                        5.6     6.000  2013-02-06 08:30:00
2013-03-04 13:00:00     2013-03-04       13:00:00                       26      26.000  2013-02-04 20:25:00

I'm trying to merge the total column for each month. January = 9.067, February 6.000 and March 26.000.

This is the result i'm trying to make:

PilotOnBoard            PilotOnBoardDate PilotOnBoardTime   PilotWaitingTime   Total    Berthing
2013-01-01 17:40:00     2013-01-01       17:40:00                        0.2   |      | 2013-01-01 20:25:00
2013-01-03 12:15:00     2013-01-03       12:15:00                       19.2   | 9.067| 2013-01-03 15:10:00
2013-01-03 14:45:00     2013-01-03       14:45:00                        7.8   |______| 2013-01-03 17:05:00
2013-02-04 18:25:00     2013-02-04       18:25:00                        6.4   | 6.000| 2013-02-04 20:25:00
2013-02-06 05:40:00     2013-02-06       05:40:00                        5.6   |______| 2013-02-06 08:30:00
2013-03-04 13:00:00     2013-03-04       13:00:00                       26     |26.000|     2013-02-04 20:25:00

Thank you for your help

  • 写回答

1条回答 默认 最新

  • dongmi4720 2015-09-11 21:36
    关注

    Check if it's the same month and add rowspan attribute to the td!

       <table border="1" cellpadding="3" cellspacing="0" class="table_content">
       <tr>
          <td width="38" rowspan="2">NO.</td>
          <td rowspan="2">PILOT ON BOARD</td>
          <td colspan="2">PILOT ON BOARD</td>
          <td rowspan="2">PILOT WAITING TIME</td>
          <td rowspan="2">TOTAL</td>
          <td rowspan="2">BERTHING</td>
       </tr>
       <tr>
          <td width="100">DATE </td>
          <td width="100">TIME</td>
       </tr>
    
       <?php
            $no=0;
            $rows = 1;
            $lastDate = null;
            $dataFromTable=json_decode('[{"PilotWaitingTime":"0.2","PilotOnBoardDate":"2013-01-01","PilotOnBoardTime":"17:40:00","PilotOnBoard":"2013-01-01 17:40:00","Berthing":"2013-01-01 20:25:00","rata2PilotWaitingTime":"9.067"},{"PilotWaitingTime":"19.2","PilotOnBoardDate":"2013-01-03","PilotOnBoardTime":"12:15:00","PilotOnBoard":"2013-01-03 12:15:00","Berthing":"2013-01-03 15:10:00","rata2PilotWaitingTime":"9.067"},{"PilotWaitingTime":"7.8","PilotOnBoardDate":"2013-01-03","PilotOnBoardTime":"14:45:00","PilotOnBoard":"2013-01-03 14:45:00","Berthing":"2013-01-03 17:05:00","rata2PilotWaitingTime":"9.067"},{"PilotWaitingTime":"6.4","PilotOnBoardDate":"2013-02-04","PilotOnBoardTime":"18:25:00","PilotOnBoard":"2013-02-04 18:25:00","Berthing":"2013-02-04 20:25:00","rata2PilotWaitingTime":"6.000"},{"PilotWaitingTime":"5.6","PilotOnBoardDate":"2013-02-06","PilotOnBoardTime":"05:40:00","PilotOnBoard":"2013-02-06 05:40:00","Berthing":"2013-02-06 08:30:00","rata2PilotWaitingTime":"6.000"},{"PilotWaitingTime":"26","PilotOnBoardDate":"2013-03-04","PilotOnBoardTime":"13:00:00","PilotOnBoard":"2013-03-04 13:00:00","Berthing":"2013-02-04 20:25:00","rata2PilotWaitingTime":"26.000"}]',true);
    
            foreach( $dataFromTable as $results ){
       ?>
    
       <tr>
          <td width="38" style="text-align:right"><?php echo $no+=1; ?></td>
          <td width="150"><?php echo $results['PilotOnBoard']; ?></td>
          <td width="100"><?php echo $results['PilotOnBoardDate']; ?> </td>
          <td width="100"><?php echo $results['PilotOnBoardTime']; ?></td>
          <td style="text-align:right"><?php echo $results['PilotWaitingTime']; ?></td>
    
          <?php 
          if(explode("-",$results['PilotOnBoardDate'])[1] !== $lastDate){?>
            <td style="text-align:right" rowspan="<?php echo $rows;?>"><?php echo $results['rata2PilotWaitingTime']; ?></td>
          <?php 
          } else {
            $rows++;
          }
          $lastDate = explode("-",$results['PilotOnBoardDate'])[1];
          ?>
          <td style="text-align:right"><?php echo $results['Berthing']; ?></td>
       </tr>
    
       <?php }; ?>
    
    </table>
    

    I think this should work!

    评论

报告相同问题?

悬赏问题

  • ¥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的速度时间图像)我想问线路信息是什么