[英]Associating two values in PHP
I'm having a trouble with a calendar I'm doing with TV shows. 我在看电视节目时遇到了麻烦。
Basically, I want to output my days of the week and the shows aired for each day. 基本上,我想输出一周中的每一天以及每天播出的节目。 (without doing 7 different queries obviously)
(显然,无需执行7个不同的查询)
The following outputs the shows with an airdate between the beginning of the week and the end 以下输出的节目带有本周开始和结束之间的播出时间
$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()) {
}
This outputs the seven dates of the week. 这将输出一周中的七个日期。
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);
}
This outputs the following: 输出以下内容:
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)
I don't understand how to combine those two things to achieve what I'm after ??! 我不明白如何结合这两件事来实现我的追求?
Here would be my battle plan to solve this: 这是我解决这个问题的战斗计划:
Use $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
使用
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
Loop over these and convert the dates from their string format into unix timestamps, probably by using explode
and mktime
: 循环遍历这些并将日期从其字符串格式转换为unix时间戳,可能是通过使用
explode
和mktime
:
foreach($rows as &$row) {
$unix_airdate= mktime(...TODO);
$row['unix_airdate']= $unix_airdate;
}
unset($row); //do not forget because $row was a reference
Then in the loop you already wrote, use http://www.php.net/manual/en/function.array-filter.php $filtered_rows= array_filter($rows, 'my_filter_function..TODO');
然后在您已经编写的循环中,使用http://www.php.net/manual/zh/function.array-filter.php
$filtered_rows= array_filter($rows, 'my_filter_function..TODO');
so that my_filter_function checks that the unix_airdate is between weekbegin and before the begin of next week. 这样my_filter_function会检查unix_airdate是否在weekbegin和下周开始之前之间。
Now go over the filtered array and output all shows. 现在遍历过滤后的数组并输出所有显示。 If you want them aggregated by day, loop over the days of the week and filter by day_begin and day_begin_next_day instead.
如果希望它们按天汇总,请在一周的各天中循环,然后按day_begin和day_begin_next_day进行过滤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.