php - 列出月份下的所有记录,以月份为表格行

标签 php mysql

好吧,我确信有办法做到这一点,但我 sleep 不足,确实需要一些帮助。

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

这是我所拥有的:

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

最佳答案

试试这个:

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

关于php - 列出月份下的所有记录,以月份为表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19106288/

相关文章:

php - 是否可以使用另一个连接中的列在另一个连接 mysql 中创建条件

php - fatal error : Call to undefined function tally()

java - 使用 Maven,子模块如何继承父模块的存储库?

mysql - 将 Mysql 转储文件从 INSERT 转换为 INSERT IGNORE

Mysql长子查询 "in"问题

mysql - 如何在 SQL 中为运行时参数定义日期

php - 计算与 id 相关的数据库行数

php - 在 Highcharts 中从 PHP 加载 JSON 编码数据时,条形图不显示

php - 对继承对象重新排序/移动 Silverstripe 3 CMS 选项卡

mysql 与 information_schema.tables 相关的问题