我正在尝试基于David Walsh的示例在PHP和SQL中构建事件日历。他的例子很老,并且使用了不推荐使用的mysql_query等函数,因此我尝试使用PDO进行现代化。到目前为止,我已经设法打印出日历,并且能够选择不同的月份和年份。我也成功创建了一个数据库连接。该表由id(自动递增),title(varchar)和event_date(date)组成。我已经用一些事件填充了表格,但我似乎无法成功检索它们。我已将代码中最相关的部分包含在顶部,但我已将整个代码复制到上下文中。
我对数据库的查询
$events = array();
try { $results = $db->query("SELECT title, DATE_FORMAT(event_date,'%Y-%m-%d') AS event_date FROM events WHERE event_date LIKE '$year-$month'");
echo "<pre>";
var_dump($results);
} catch (Exception $e) {
echo "cannot get results!";
exit;
}
$bookings = $results->fetchAll(PDO::FETCH_ASSOC);
while($row = $bookings) {
$events[$row['event_date']][] = $row;
}
echo '<pre>';
var_dump($bookings);
我的var_dump对$ results
的结果object(PDOStatement)#2 (1) {
["queryString"]=>
string(105) "SELECT title, DATE_FORMAT(event_date,'%Y-%m-%d') AS event_date FROM events WHERE event_date LIKE '2014-1'"
}
var_dump表示我的数组是空的
array(0) {
}
上下文的完整代码
<?php
require('database.php');
/* draws a calendar */
function draw_calendar($month,$year,$events = array()){
/* draw table */
$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';
/* table headings */
$headings = array('Sontag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag');
$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';
/* days and weeks vars now ... */
$running_day = date('w',mktime(0,0,0,$month,1,$year));
$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
$days_in_this_week = 1;
$day_counter = 0;
$dates_array = array();
/* row for week one */
$calendar.= '<tr class="calendar-row">';
/* print "blank" days until the first of the current week */
for($x = 0; $x < $running_day; $x++):
$calendar.= '<td class="calendar-day-np"> </td>';
$days_in_this_week++;
endfor;
/* keep going with days.... */
for($list_day = 1; $list_day <= $days_in_month; $list_day++):
$calendar.= '<td class="calendar-day"><div style="position:relative;height:100px;">';
/* add in the day number */
$calendar.= '<div class="day-number">'.$list_day.'</div>';
$event_day = $year.'-'.$month.'-'.$list_day;
if(isset($events[$event_day])) {
foreach($events[$event_day] as $event) {
$calendar.= '<div class="event">'.$event['title'].'</div>';
}
}
else {
$calendar.= str_repeat('<p> </p>',2);
}
$calendar.= '</div></td>';
if($running_day == 6):
$calendar.= '</tr>';
if(($day_counter+1) != $days_in_month):
$calendar.= '<tr class="calendar-row">';
endif;
$running_day = -1;
$days_in_this_week = 0;
endif;
$days_in_this_week++; $running_day++; $day_counter++;
endfor;
/* finish the rest of the days in the week */
if($days_in_this_week < 8):
for($x = 1; $x <= (8 - $days_in_this_week); $x++):
$calendar.= '<td class="calendar-day-np"> </td>';
endfor;
endif;
/* final row */
$calendar.= '</tr>';
/* end the table */
$calendar.= '</table>';
/** DEBUG **/
$calendar = str_replace('</td>','</td>'."\n",$calendar);
$calendar = str_replace('</tr>','</tr>'."\n",$calendar);
/* all done, return result */
return $calendar;
}
function random_number() {
srand(time());
return (rand() % 7);
}
/* date settings */
$month = (int) ($_GET['month'] ? $_GET['month'] : date('m'));
$year = (int) ($_GET['year'] ? $_GET['year'] : date('Y'));
/* select month control */
$select_month_control = '<select name="month" id="month">';
for($x = 1; $x <= 12; $x++) {
$select_month_control.= '<option value="'.$x.'"'.($x != $month ? '' : ' selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$year)).'</option>';
}
$select_month_control.= '</select>';
/* select year control */
$year_range = 7;
$select_year_control = '<select name="year" id="year">';
for($x = ($year-floor($year_range/2)); $x <= ($year+floor($year_range/2)); $x++) {
$select_year_control.= '<option value="'.$x.'"'.($x != $year ? '' : ' selected="selected"').'>'.$x.'</option>';
}
$select_year_control.= '</select>';
/* "next month" control */
$next_month_link = '<a href="?month='.($month != 12 ? $month + 1 : 1).'&year='.($month != 12 ? $year : $year + 1).'" class="control">Next Month >></a>';
/* "previous month" control */
$previous_month_link = '<a href="?month='.($month != 1 ? $month - 1 : 12).'&year='.($month != 1 ? $year : $year - 1).'" class="control"><< Previous Month</a>';
/* bringing the controls together */
$controls = '<form method="get">'.$select_month_control.$select_year_control.' <input type="submit" name="submit" class="btn btn-default" value="Go" /> '.$previous_month_link.' '.$next_month_link.' </form>';
/* get all events for the given month */
$events = array();
try { $results = $db->query("SELECT title, DATE_FORMAT(event_date,'%Y-%m-%d') AS event_date FROM events WHERE event_date LIKE '$year-$month'");
echo "<pre>";
var_dump($results);
} catch (Exception $e) {
echo "cannot get results!";
exit;
}
$bookings = $results->fetchAll(PDO::FETCH_ASSOC);
while($row = $bookings) {
$events[$row['event_date']][] = $row;
}
echo '<pre>';
var_dump($bookings);
echo '<h2 style="float:left; padding-right:30px;">'.date('F',mktime(0,0,0,$month,1,$year)).' '.$year.'</h2>';
echo '<div style="float:left;">'.$controls.'</div>';
echo '<div style="clear:both;"></div>';
echo draw_calendar($month,$year,$events);
echo '<br /><br />';
答案 0 :(得分:3)
除了LIKE问题
FetchAll
获得整套,所以虽然不会工作。使用foreach
foreach ($bookings as $row) {
$events[$row['event_date']][] = $row;
}
同样在您使用赋值运算符而不是比较==
答案 1 :(得分:1)
我认为你在sql查询字符串中忘记了一个通配符。
你可以试试这个:
WHERE event_date LIKE '$year-$month%'"
答案 2 :(得分:1)
如果您将event_date
列作为日期而不是文本字段处理,您会做得更好。
尝试此查询:
SELECT title,
DATE_FORMAT(event_date,'%Y-%m-%d') AS event_date
FROM events
WHERE event_date >= DATE(DATE_FORMAT(NOW(),'%Y-%m-01'))
AND event_date < DATE(DATE_FORMAT(NOW(),'%Y-%m-01')) + INTERVAL 1 MONTH
这将找到当月的所有event_date
值,同时将event_date
上的MySQL索引用于搜索是否有。{/ p>
如果你想要上个月的活动,你可以这样做。
WHERE event_date >= DATE(DATE_FORMAT(NOW(),'%Y-%m-01')) - INTERVAL 1 MONTH
AND event_date < DATE(DATE_FORMAT(NOW(),'%Y-%m-01'))
以下是对此主题的介绍。 http://www.plumislandmedia.net/mysql/sql-reporting-time-intervals/