代码:
<style>
div.day-number {
background:#999;
position:absolute;
z-index:2;
top:-5px;
right:-25px;
padding:5px;
color:#fff;
font-weight:bold;
width:20px;
text-align:center;
}
td.calendar-day, td.calendar-day-np {
width:120px;
padding:5px 25px 5px 5px;
border-bottom: 1px solid #999;
border-right: 1px solid #999;
border-left: 1px solid #999;
border-top: 1px solid #999;
}
</style>
<?php
include ('config.php');
function draw_calendar($month, $year, $events = array())
{
$calendar = '<center><table cellpadding="0" cellspacing="0" class="calendar">';
$headings = array(
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
);
$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">' . implode('</td><td class="calendar-day-head">', $headings) . '</td></tr>';
$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();
$calendar.= '<tr class="calendar-row">';
for ($x = 0; $x < $running_day; $x++):
$calendar.= '<td class="calendar-day-np"> </td>';
$days_in_this_week++;
endfor;
for ($list_day = 1; $list_day <= $days_in_month; $list_day++):
$calendar.= '<td class="calendar-day"><div style="position:relative;height:100px;">';
$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;
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;
$calendar.= '</tr>';
$calendar.= '</table></center>';
$calendar = str_replace('</td>', '</td>' . "\n", $calendar);
$calendar = str_replace('</tr>', '</tr>' . "\n", $calendar);
return $calendar;
}
function random_number()
{
srand(time());
return (rand() % 7);
}
$month = (int)($_GET['month'] ? $_GET['month'] : date('m'));
$year = (int)($_GET['year'] ? $_GET['year'] : date('Y'));
$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>';
$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_link = '<a href="?month=' . ($month != 12 ? $month + 1 : 1) . '&year=' . ($month != 12 ? $year : $year + 1) . '" class="control">Next Month >></a>';
$previous_month_link = '<a href="?month=' . ($month != 1 ? $month - 1 : 12) . '&year=' . ($month != 1 ? $year : $year - 1) . '" class="control"><< Previous Month</a>';
$controls = '<form method="get">' . $select_month_control . $select_year_control . ' <input type="submit" name="submit" value="Go" /> ' . $previous_month_link . ' ' . $next_month_link . ' </form>';
$events = array();
$query = "SELECT title, DATE_FORMAT(date,'%Y-%m-%D') AS event_date FROM events WHERE date LIKE '$year-$month%'";
$result = mysqli_query($db, $query);
while ($row = mysqli_fetch_assoc($result))
{
$events[$row['event_date']][] = $row;
}
echo '<h2 style="text-align: center;">' . date('F', mktime(0, 0, 0, $month, 1, $year)) . ' ' . $year . '</h2>';
echo '<div style="width: 30%;margin: 0 auto;">' . $controls . '</div>';
echo '<div style="clear:both;"></div>';
echo draw_calendar($month, $year, $events);
echo '<br /><br />';
?>
在这段代码中,我创建了一个简单的事件生成日历。在php代码内部,我编写了一个查询,即SELECT title, DATE_FORMAT(date,'%Y-%m-%D') AS event_date FROM events WHERE date LIKE '$year-$month%'
,该查询工作正常,但无法显示特定日期的事件。
下面我还提到了两个Notice
错误
Notice: Undefined index: month in C:\xampp\htdocs\event_calendra\new_cal.php on line 105
Notice: Undefined index: year in C:\xampp\htdocs\event_calendra\new_cal.php on line 106
那么,如何解决此问题并在特定日期显示事件?请帮助我。
谢谢