在PHP中关联两个值

时间:2013-05-23 15:20:16

标签: php mysql

我在使用电视节目制作的日历时遇到了麻烦。

基本上,我想输出我每周的日子和每天播出的节目。 (显然没有做7个不同的查询)

以下输出的节目在一周的开头和结尾之间有一个airdate

$stmt = $conn->prepare("SELECT * 
FROM show_episode_airdate, show_episode
WHERE show_episode_airdate.airdate BETWEEN :weekbeginning AND :weekend AND show_episode.episode_id = show_episode_airdate.episode_id ");

$stmt->execute(array(':weekbeginning' => $begin_date, ':weekend' => $end_date));

while($row = $stmt->fetch()) {

}

这将输出一周中的七个日期。

foreach ($listofdays as $num=>$jour) {
     $date_table[$num] = date('m-d-Y',$weekbegin[0]+$num*DUREE_UN_JOUR);
     $daysoftheweek = $date_table[$num];
     var_dump($daysoftheweek);
    }

这输出以下内容:

string '05-20-2013' (length=10)

string '05-21-2013' (length=10)

string '05-22-2013' (length=10)

string '05-23-2013' (length=10)

string '05-24-2013' (length=10)

string '05-25-2013' (length=10)

string '05-26-2013' (length=10)

我不明白如何将这两件事结合起来实现我追求的目标??!

1 个答案:

答案 0 :(得分:0)

这是解决这个问题的战斗计划:

使用$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

循环遍历这些并将日期从其字符串格式转换为unix时间戳,可能是使用explodemktime

foreach($rows as &$row) {
    $unix_airdate= mktime(...TODO);
    $row['unix_airdate']= $unix_airdate;
}
unset($row); //do not forget because $row was a reference

然后在你已编写的循环中,使用http://www.php.net/manual/en/function.array-filter.php $filtered_rows= array_filter($rows, 'my_filter_function..TODO');,以便my_filter_function检查unix_airdate是在weekbegin之间还是在下周开始之前。

现在查看已过滤的数组并输出所有节目。如果您希望它们按天聚合,则循环一周中的几天,然后按天_begin和day_begin_next_day过滤。