2015-09-11 65 views
0

我有一個表,我想合併每個月的Total列的值。從PHP表中合併列的值

這是我的代碼:

<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> 

這是結果:

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 

我試圖合併每個月的總列。 1月= 9.067,2000年2月6日和26000年3月。

這是結果我試圖讓:

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 

謝謝您的幫助

+0

您需要基於月嗎?你對數據進行平均? –

回答

0

檢查它是否是同一個月,並添加rowspan屬性到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> 

我認爲這應該工作!