我试图获得一个连接到mySQL数据库的日历。该脚本是一个简单的calendarpicker女巫我试图连接到数据库,以便它可以显示事件。 我设法在我的数据库中显示我在该月的所有日子中显示的事件,但我希望它仅显示为设置日期的链接。我还想添加一个添加和删除事件的功能,但首先要做的事情。
我的数据库如下所示:
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")?>
答案 0 :(得分:0)
您需要在回复$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>";
}