PHP事件日历-浮动事件


PHP Event Calendar - Floating Events

我有一个使用数据库的日历脚本-我需要它,以便它可以跨日期(浮动事件)例如,一个事件从周五开始,到周日结束,并将突出显示它所涵盖的日期。我到处找都找不到。

这是我的数据库结构:

    id
    event 
    day
    month
    year
    category
    start
    finish
    location

这是数据库中的一行(按与上面相同的顺序):

    1
    British Red Cross Practical First Aid
    10
    3
    2012
    3
    09:00
    16:00
    Dundee, venue to be confirmed

这是我目前的PHP:

<?php 
    $timestamp = mktime(0,0,0,$cMonth,1,$cYear);
    $maxday = date("t",$timestamp);
    $thismonth = getdate($timestamp);
    $startday = $thismonth['wday'] + 6;
    for ($i=0; $i<($maxday+$startday); $i++) {
        if(($i % 7) == 0 ) echo "<tr>'n";
        if($i < $startday) echo "<td></td>'n";
        else{
            $sql = "SELECT * FROM events WHERE day='".($i - $startday + 1)."' AND month='".$cMonth."' AND year='".$cYear."'";
            $query = mysql_query($sql);
            if (mysql_num_rows($query) > 0){
                echo "<td valign='top' height='80px'><div class='date' align='right'>".($i - $startday + 1)."</div>";
                while ($row = mysql_fetch_assoc($query)){
                    if ($row['category'] == "1"){
                        echo "<div style='background:#7F9AA4; margin-bottom: 2px; color: white; width: 98px;'>".$row['event']."</div>";
                    }elseif ($row['category'] == "2"){
                        echo "<div style='background:#9C8CAB; margin-bottom: 2px; color: white; width: 98px;'>".$row['event']."</div>";
                    }elseif ($row['category'] == "3"){
                        echo "<div style='background:#CABB16; margin-bottom: 2px; color: white; width: 98px;'>".$row['event']."</div>";
                    }elseif ($row['category'] == "4"){
                        echo "<div style='background:#86A20B; margin-bottom: 2px; color: white; width: 98px;'>".$row['event']."</div>";
                    }elseif ($row['category'] == "5"){
                        echo "<div style='background:#6E4C8C; margin-bottom: 2px; color: white; width: 98px;'>".$row['event']."</div>";
                    }
                }
                echo "</td>'n";
            }else echo "<td valign='top' height='80px'><div class='date' align='right'>". ($i - $startday + 1) ."</div></td>'n";
        }
        if(($i % 7) == 6 ) echo "</tr>'n";
    }
?>

1)您需要某种方式来指定一个事件跨越1个以上的日期。例如:跨度:int

2) 更改您的搜索,使您包括跨度日期(即起始日期和起始日期之间的日期+span-1)。您可以迭代查询,并将每一行放入一个数组中,一个数组对应一个月的每一天;复制那些跨越一天以上的。

3) 生成日历的另一种方法是一次搜索整个月,并使用PHP将其分解,而不是SQL:(注意,我清理了你的PHP/SQL。你不需要去掉字符串,如果日期、月份和年份是int,你就不需要引用它们)

$sql = "SELECT * FROM events WHERE month=$cMonth AND year=$cYear ORDER BY day";
$query = mysql_query($sql);
$cday = 90;
while ($row = mysql_fetch_assoc($query))
{
    $day = $row['day'];
    if ($day != $cday)
    {
        // I'll leave as an exercise how figure out how to output day in particular slot
        $day = $cday;
    }
}

4) 在上面,如果你想考虑跨度,你可以,例如:

$prev_month = ???; $prev_year= ???; $prev_month_days= ???;
 $sql = "SELECT * FROM events 
           WHERE (month=$cMonth AND year=$cYear) 
               OR ( month= $prev_month and year=$prev_year and day>($prev_month_days - span) )
           ORDER BY day";

玩得开心。