I have a calendar script that uses a database – I am needing it so it can cross dates (Floating Events) e.g an event starts on a Friday and ends on Sunday and will highlight the dates it covers. I have searched high and low to no avail.
Here is my datebase structure:
id
event
day
month
year
category
start
finish
location
Here is a row from the database (in same order as above):
1
British Red Cross Practical First Aid
10
3
2012
3
09:00
16:00
Dundee, venue to be confirmed
Here is my current 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) You need someway of specifying that an event spans more than 1 date.
eg: spans: int
2) change your search so that you include span date (ie where day between startday and startday+span-1). You could iterate through the query and put each row into an array of arrays, one for each day of the month; duplicating the ones that span more than one day.
3) another way to produce your calendar is to search the whole month at once, and use PHP to chop it up, instead of SQL:
(notice, I cleaned up your PHP/SQL. you don’t need to drop out of strings, and if the columns day, month and year are ints, you don’t need to quote them)
4) in the above, if you want to consider the span, you could, for instance :
have fun.