0

我试图让一个日历脚本连接到 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")?>
4

1 回答 1

0

您需要在 echo 之前检查单元格日和事件日$event//Get calendar events对和进行以下 2 处更改//Draw Calendar。更改位于//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>";
}                   
于 2012-09-17T06:05:26.900 回答