2013-10-01 23 views
0

好吧,我敢肯定有一種方法可以做到這一點,但我患有睡眠剝奪,真的可以使用一些幫助。列出所有記錄下月份作爲表格

這是我需要的。我有一個表頭和下面列出的數據行 - 它看起來不錯,但我需要在月份更改以分隔出月份時在數據行之間添加一個表格行。

這是我有:

Date   Time   Event   
08-31-2013 6:00 pm EST Horseshoe Tourney 
09-07-2013 8:00 pm EST Movie Night 
09-28-2013 5:00 pm EST Dinner on the Quad 
10-12-2013 4:30 pm EST Sing-a-long 
10-31-2013 7:00 pm EST Halloween Party 
11-14-2013 4:00 pm EST Hay Ride 

這就是我需要:

Date   Time   Event 
AUGUST (to span the whole table row)   
08-31-2013 6:00 pm EST Horseshoe Tourney 
SEPTEMBER (to span the whole table row) 
09-07-2013 8:00 pm EST Movie Night 
09-28-2013 5:00 pm EST Dinner on the Quad 
OCTOBER (to span the whole table row) 
10-12-2013 4:30 pm EST Sing-a-long 
10-31-2013 7:00 pm EST Halloween Party 
NOVEMBER (to span the whole table row) 
11-14-2013 4:00 pm EST Hay Ride 

任何幫助將不勝感激。下面是我用來創建表格的代碼。

<table style="width: 100%;"> 
<thead> 
    <tr style="background-color:#8a0028; color:white;"> 
     <th style="background-color:#8a0028; color:white; text-align:left;">Date</th> 
     <th style="background-color:#8a0028; color:white; text-align:left;">Time</th> 
     <th style="background-color:#8a0028; color:white; text-align:left;">Event</th> 
    </tr> 
</thead> 
    <tbody> 
    <?php 
    // Write rows 
    mysql_data_seek($result, 0); 
    while ($row = mysql_fetch_assoc($result)) { 
    ?> 
    <tr> 
    <td><?php echo date("M d, Y", strtotime($row['date']));?></td> 
    <td><?php echo $row['time'];?></td> 
    <td><?php echo $row['event'];?></td> 
    </tr> 
<?php } ?> 
    </tbody> 
</table> 
<?php 
mysql_free_result($result); 
?> 

這是什麼結束了爲我工作!當然,請欣賞幫助!

<?php 
    // Write rows 
    mysql_data_seek($result, 0); 
    $month=''; 
    while ($row = mysql_fetch_assoc($result)) { 
     if(date("F", strtotime($row['date']))!==$month){ 
      $month=date("F", strtotime($row['date'])); ?> 
      <tr><td colspan="3"><?= $month ?></td></tr> 
      <?php 
     } 
    ?> 
    <tr> 
    <td><?php echo date("M d, Y", strtotime($row['date']));?></td> 
    <td><?php echo $row['time'];?></td> 
    <td><?php echo $row['event'];?></td> 
    </tr> 
<?php 
    $month=date("F", strtotime($row['date'])); 
    } 
?> 
    </tbody> 
</table> 
<?php 
mysql_free_result($result); 
?> 
+0

感謝皮特Bekisz - 我試圖找出如何讓這些排隊。顯然,這是我第一次使用Stackoverflow。 – user2833089

+0

不用擔心!我們一直在那裏:) – Pete

回答

0

我建議是這樣的:

從本質上講,在每個循環的開始,你正在測試,看看是否月份是從以前的一個不同。如果是,則輸出一個新行。

<?php 
     mysql_data_seek($result, 0); 
     $last_month = ''; 
     while ($row = mysql_fetch_assoc($result)) { 
?> 

<?php if (date("M", strtotime($row['month'])) != $last_month): ?> 
    <tr> 
     <td colspan="4"><?php echo date("M", strtotime($row['month'])); ?></td> 
    </tr> 
<?php endif; ?> 

     <tr> 
     <td><?php echo date("M d, Y", strtotime($row['date']));?></td> 
     <td><?php echo $row['time'];?></td> 
     <td><?php echo $row['event'];?></td> 
     </tr> 

<?php 
     $last_month = date("M", strtotime($row['month'])); 
    } 
?> 
+0

首先 - 非常感謝你的幫助!還有一個問題。如果我想檢查$ row ['month']的值,以便檢查月份的數值並寫出實際的月份名稱 - 我該如何去做? – user2833089

+0

而不是'<?php echo date(「M」,strtotime($ row ['month'])); ?>,使用'date(「F」,strtotime($ row ['month']))''。我建議查看http://www.pep.net/date。兩個都感謝 – Pete

1

試試這個:

<table style="width: 100%;"> 
<thead> 
    <tr style="background-color:#8a0028; color:white;"> 
     <th style="background-color:#8a0028; color:white; text-align:left;">Date</th> 
     <th style="background-color:#8a0028; color:white; text-align:left;">Time</th> 
     <th style="background-color:#8a0028; color:white; text-align:left;">Event</th> 
    </tr> 
</thead> 
    <tbody> 
    <?php 
    // Write rows 
    mysql_data_seek($result, 0); 
    $month=''; 
    while ($row = mysql_fetch_assoc($result)) { 
     if(date("F", strtotime($row['date']))!==$month){ 
      $month=date("F", strtotime($row['date'])); ?> 
      <tr><td colspan="3"><?= $month ?></td></tr> 
      <?php 
     } 
    ?> 
    <tr> 
    <td><?php echo date("M d, Y", strtotime($row['date']));?></td> 
    <td><?php echo $row['time'];?></td> 
    <td><?php echo $row['event'];?></td> 
    </tr> 
<?php } ?> 
    </tbody> 
</table> 
<?php 
mysql_free_result($result); 
?> 
+0

!這是兩個結合起來爲我工作的組合。 – user2833089

相關問題