PHP日历显示事件列表

时间:2013-06-30 18:21:47

标签: php

我有一个包含两列eventName|eventDate的数据库表。我创建了一个接收startDate和endDate的函数,我想在ListView中显示事件列表,每个日期都作为标题。

在下面的简短示例中,我知道我可以使用SQL检索完整的事件列表。然后我如何插入事件标题,以便我可以在格式正确的数组中返回它们?

function retrieveEvents($startDate, $endDate) {
    // run SQL query
    // 
    if($stmt->rowCount() > 0) {
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            // how do I write this part such that I can output event headers in my array        
            $events = $row;
        }
    }
}

所以我的预期输出是

1st July 2013 ($startDate)
- Tea with President - 1300h
- Mow the lawn - 1330h
- Shave the cat - 1440h
2nd July 2013
- Shave my head - 0800h
3rd July 2013
4th July 2013 ($endDate)
- Polish the car - 1000h

2 个答案:

答案 0 :(得分:0)

在您的MYSQL查询中:

SELECT * FROM `yourTableName` WHERE `eventDate` >= $startDate AND `eventDate` <= $endDate

PS:我不确定引用查询中的变量的引号。

PPS:从不使用*来选择列,始终只选择您需要的列。我在这里使用它是因为我不知道列的名称

答案 1 :(得分:0)

我最后在PHP中检查并仅在检测到其他日期时打印新行。

以下代码,以备将来满足某人的需求。

<?php
        $currentPrintDay = 0;
        $currentPrintMonth = 0;
        $currentPrintYear = 0;

        echo "<table>"
        foreach($reservationsToShow as $row):
        // get day, month, year of this entry
        $timestamp = strtotime($row['timestamp']);
        $day = date('d', $timestamp);
        $month = date('m', $timestamp);
        $year = date('Y', $timestamp);

        // if it does not match the current printing date, assign it to the current printing date,
        // assign it, print a new row as the header before continuing
        if($day != $currentPrintDay || $month != $currentPrintMonth || $year != $currentPrintYear) {
            $currentPrintDay = $day;
            $currentPrintMonth = $month;
            $currentPrintYear = $year;

            echo 
            "<tr>" .
            "<td colspan='100%'>". date('d-m-Y', $timestamp) . "</td>" .
            "</tr>";
        }
        // continue to print event details from here on...
?>