0

我有一个包含两列的数据库表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
4

2 回答 2

0

在您的 MYSQL 查询中:

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

PS:我不确定您查询中变量周围的引号。

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

于 2013-06-30T18:35:47.100 回答
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...
?>
于 2013-07-08T16:41:17.267 回答