我试图获取一个日历脚本来连接到mySQL数据库。该脚本是一个简单的日历选择器女巫,我正在尝试连接到数据库,以便它可以显示事件。我设法使数据库中的事件显示该月的所有日子,但我希望它仅在设置的日期显示为链接。我还想添加一个用于添加和删除事件的函数,但首先要做的是。
我的数据库如下所示:
database: calendar
table: calendar_events
row1: event_title
row2: event_shortdesc
row3: event_start
这是我的整个脚本代码,日历函数工作正常。
<?php include("includes/header.php")?>
<?php require_once("includes/db_connection.php")?>
<?php include("includes/functions.php")?>
<?php
if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("m");
if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");
$cMonth = $_REQUEST["month"];
$cYear = $_REQUEST["year"];
$prev_year = $cYear;
$next_year = $cYear;
$prev_month = $cMonth-1;
$next_month = $cMonth+1;
if ($prev_month == 0) {
$prev_month = 12;
$prev_year = $cYear-1;
}
if ($next_month == 13) {
$next_month = 1;
$next_year = $cYear+1;
}
$monthNames = Array("Januar", "Februar", "Mars", "April", "Mai", "Juni", "Juli",
"August", "September", "Oktober", "November", "Desember");
?>
<div id="kalendercontainer">
<a id="prev" href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>">Previous</a>
<span id="monthname"><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></span>
<a id="next" href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>">Next</a>
<table id="kalender">
<tr>
<td><strong>Man</strong></td>
<td><strong>Tir</strong></td>
<td><strong>Ons</strong></td>
<td><strong>Tor</strong></td>
<td><strong>Fre</strong></td>
<td><strong>Lør</strong></td>
<td><strong>Søn</strong></td>
</tr>
<?php
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday']-1;
//Get calendar events
$get_cal_events = mysql_query("SELECT * FROM calendar_events WHERE event_start = '{$startday}'");
$rows = mysql_fetch_array($get_cal_events);
$event = $rows['event_title'] . $rows['event_shortdesc'];
//Draw calendar
for ($i=0; $i<($maxday+$startday); $i++) {
if(($i % 7) == 0 ) echo "<tr>";
if($i < $startday) echo "<td></td>";
else
echo "<td>" . ($event) . ($i - $startday + 1) . "</td>";
if(($i % 7) == 6 ) echo "</tr>";
}
?>
</table>
<p></p>
</div>
<?php require("includes/footer.php")?>
在回显$event
之前,您需要根据事件日检查单元格日。对//Get calendar events
和//Draw Calendar
进行以下 2 项更改。更改在 //ADD THIS CODE
.
这假设event_start
是一个日期(即 2012-09-17)。
//Get calendar events
$get_cal_events = mysql_query("SELECT * FROM calendar_events WHERE event_start = '{$startday}'");
$rows = mysql_fetch_array($get_cal_events);
$event = $rows['event_title'] . $rows['event_shortdesc'];
//ADD THIS CODE
// Gets the event day. Assuming the event_start is a mysql date (ie. 2012-09-17)
$event_day = date("j", strtotime($rows['event_start']));
//Draw calendar
for ($i=0; $i<($maxday+$startday); $i++) {
if(($i % 7) == 0 ) echo "<tr>";
if($i < $startday) echo "<td></td>";
else
echo "<td>";
//ADD THIS CODE
// Checks to see if the event day is the same as the cell day
if ($event_day == ($i - $startday + 1)) echo $event;
echo ($i - $startday + 1) . "</td>";
if(($i % 7) == 6 ) echo "</tr>";
}