繁体   English   中英

从mySQL获取事件。 PHP日历

[英]Get events from mySQL. PHP calendar

我试图获取日历脚本以连接到mySQL数据库。 脚本是一个简单的Calendarpicker女巫,我正在尝试连接到数据库,以便它可以显示事件。 我设法使数据库中显示的事件在当月的所有天都显示,但是我希望它仅在设置的那一天显示为链接。 我还想添加一个用于添加和删除事件的函数,但首先要注意的是。

我的数据库如下所示:

database: calendar

table: calendar_events
row1: event_title
row2: event_shortdesc
row3: event_start

这是我的脚本完整代码,calendarfunction工作正常。

<?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>";
}                   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM