列出月份下的所有记录,其中月份为tablerow

时间:2013-10-01 00:56:37

标签: php mysql

好的,我确定有办法做到这一点,但我正在遭受睡眠剥夺,并且可以真正使用一些帮助。

这就是我需要的。我有一个表格,下面列出了标题和数据行 - 它看起来不错,但是,当月份发生变化时,我需要在数据行之间添加一个表格行来分隔月份。

这就是我所拥有的:

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);
?>

2 个答案:

答案 0 :(得分: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);
?>

答案 1 :(得分: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']));
    } 
?>