Click to See Complete Forum and Search --> : Event Calendar


kproc
02-27-2007, 07:23 PM
Hi

I'm looking for a tutorial or some script to get me started in creating an event calendar. I have done some search and found some great calendars but nothing that I can get to do what i want

What I want to do

Input

Users can enter events into a mysql database

Output

Events displayed on the calendar on the date in which they occur with the option to click a link to display the complete details

Thanks for any help

aj_nsc
02-27-2007, 07:32 PM
http://www.phpfreaks.com/tutorials/83/0.php

Best thing you can get. I added a function to this code to pull events out of a mysql database. I can't remember if there are bugs in this code or not, but I know I modified mine because it wasn't working right, but maybe I messed it up myself before I 'fixed' it :D. If you need any assistance, let me know, I've got this one working perfectly for me.

kproc
02-27-2007, 07:34 PM
I came accross that one but it does not add the events to the specific dates.

aj_nsc
02-27-2007, 07:37 PM
It can, quite easily. You just have to modify the code a little. If you know enough about mysql to put specific events into a database, you should learn enough about php to pull them out. I'd be more than happy to show you the function I wrote if you want.

kproc
02-27-2007, 08:07 PM
that would be greatly appreciated, I just got the calendar to display, know to show the event titles in the correct dates:confused:

aj_nsc
02-27-2007, 09:04 PM
I was going to type in how to do this, but instead I think I'll post my entire calendar script. I'll just post a couple of comments in it to show you were to look and what I did.

As I have in my comments, don't get caught up in the specifics of the exact code I used, just use it to see how you can pull events from a database and get them to display on a specific day for your calendar. Hope this helps.

kproc
02-27-2007, 09:59 PM
Thank you for all your help, I'm having hard time piecing this one together and following your code, It looks nothing like the tutorial from PHP freaks.

NightShift58
02-27-2007, 10:05 PM
//CREATE AN OFFSET TO CALCULATE WHICH COLUMN (Sun-Sat) THE FIRST DAY GOES IN
switch($month_start_day) {
case "Sun"; $offset = 0; break;
case "Mon"; $offset = 1; break;
case "Tue"; $offset = 3; break;
case "Wed"; $offset = 4; break;
case "Thu"; $offset = 5; break;
case "Fri"; $offset = 6; break;
case "Sat"; $offset = 7; break;
}Is this correct?

kproc
02-27-2007, 10:09 PM
This code is from phpfreaks. I did some editing.

what it does this far,

displays the calendar and when there is an event on a given date the date value shows has a hyperlink,

what is does not do
when I click the link nothing happens

What I want it to do
Display the event time under the date value.

I'm having a hard time thinking through this one.

<?
include ("../Connections/db.php");
// Database connection variables

function getEventDays($month, $year) {
$days = array();
$sql = mysql_query("SELECT DAY(event_date) AS day, COUNT(event_id) FROM calendar_events WHERE MONTH(event_date) = '$month' AND YEAR(event_date) = '$year' GROUP BY day");

if (mysql_num_rows($sql) > 0) {
while ($row = mysql_fetch_array($sql)) $days[] = $row['day'];
}

return $days;
}

function drawCalendar($month, $year) {
// set variables we will need to help with layouts
$first = mktime(0,0,0,$month,1,$year); // timestamp for first of the month
$offset = date('w', $first); // what day of the week we start counting on
$daysInMonth = date('t', $first);
$monthName = date('F', $first);
$weekDays = array('Su', 'M', 'Tu', 'W', 'Th', 'F', 'Sa');
$eventDays = getEventDays($month, $year);

// Start drawing calendar
$out = "<table id=\"myCalendar\">\n";
$out .= "<tr><th colspan=\"7\">$monthName $year</th></tr>\n";
$out .= "<tr>\n";
foreach ($weekDays as $wd) $out .= "<td class=\"weekDays\">$wd</td>\n";

$i = 0;
for ($d = (1 - $offset); $d <= $daysInMonth; $d++) {
if ($i % 7 == 0) $out .= "<tr>\n"; // Start new row
if ($d < 1) $out .= "<td class=\"nonMonthDay\">&nbsp;</td>\n";
else {
if (in_array($d, $eventDays)) {
$out .= "<td class=\"monthDay\">\n";
$out .= "<a href=\"?year=$year&amp;month=$month&amp;day=$d\">$d</a>\n";
$out .= "</td>\n";
} else $out .= "<td class=\"monthDay\">$d</td>\n";
}
++$i; // Increment position counter
if ($i % 7 == 0) $out .= "</tr>\n"; // End row on the 7th day
}

// Round out last row if we don't have a full week
if ($i % 7 != 0) {
for ($j = 0; $j < (7 - ($i % 7)); $j++) {
$out .= "<td class=\"nonMonthDay\">&nbsp;</td>\n";
}
$out .= "</tr>\n";
}

$out .= "</table>\n";

return $out;
}

$year = isset($_GET['year']) ? $_GET['year'] : date('Y');
$month = isset($_GET['month']) ? $_GET['month'] : date('m');

echo drawCalendar($month, $year);

// Previous month link
$prevTS = strtotime("$year-$month-01 -1 month"); // timestamp of the first of last month
$pMax = date('t', $prevTS);
$pDay = ($day > $pMax) ? $pMax : $day;
list($y, $m) = explode('-', date('Y-m', $prevTS));
echo "<p>\n";
echo "<a href=\"?year=$year&amp;month=$m&amp;day=$pDay\">&laquo; Prev</a> |\n";

// Next month link
$nextTS = strtotime("$year-$month-01 +1 month");
$nMax = date('t', $nextTS);
$nDay = ($day > $nMax) ? $nMax : $day;
list($y, $m) = explode('-', date('Y-m', $nextTS));
echo "<a href=\"?year=$y&amp;month=$m&amp;day=$nDay\">Next &raquo;</a>\n";
echo "</p>\n";

// Calendar is done, let's list events for selected day
$sql = mysql_query("SELECT * FROM calendar_events WHERE event_date = '$year-$month-$day'");
if (mysql_num_rows($sql) > 0) {
while ($e = mysql_fetch_array($sql)) {
echo "<p>$e[event_title]</p>\n";
}
} else {
echo "<p>No events today</p>\n";
}
?>

NightShift58
02-27-2007, 10:26 PM
$out .= "<a href=\"?year=$year&amp;month=$month&amp;day=$d\">$d</a>\n";How about adding the page name and not just the URL parameters?

kproc
02-27-2007, 10:41 PM
doh? should have looked harder. any thoughts how to get the events to display with there corresponding dates

kproc
02-27-2007, 10:52 PM
Below is what I have this far. The function is not working. not sure why

<?
include ("../Connections/db.php");
// Database connection variables

function getEventDays($month, $year) {
$days = array();
$sql = mysql_query("SELECT DAY(event_date) AS day, COUNT(event_id) FROM calendar_events WHERE MONTH(event_date) = '$month' AND YEAR(event_date) = '$year' GROUP BY day");

if (mysql_num_rows($sql) > 0) {
while ($row = mysql_fetch_array($sql)) $days[] = $row['day'];
}

return $days;
}

function entryInfoQuery($date,$month,$monthname,$year) {

$query_result = mysql_query("SELECT * FROM calendar_events WHERE DAY(event_date)='$date' AND MONTH(event_date)='$month' AND YEAR(event_date)='$year'");

$num = mysql_numrows($query_result);

if ($num > 1) {
$i=0;
while ($i < $num) {
$event = mysql_result($query_result,$i,"event_title");
$time = mysql_result($query_result,$i,"event_time");
$body[$i] = "$event @ $time";
$i++;
}
$body_var = join("<br />",$body);
$title = "title=\"header=[$date $monthname $year] body=[$body_var]\"";
unset ($event_array); //CLEAR THE ARRAY FOR THE NEXT TIME THE FUNCTION IS CALLED
return $title;
}

elseif ($num == 1) {
$event = mysql_result($query_result,0,"title");
$time = mysql_result($query_result,0,"time");
return "title=\"header=[$date $monthname $year] body=[$event @ $time]\"";
}

else {
return "";
}
}
function drawCalendar($month, $year) {
// set variables we will need to help with layouts
$first = mktime(0,0,0,$month,1,$year); // timestamp for first of the month
$offset = date('w', $first); // what day of the week we start counting on
$daysInMonth = date('t', $first);
$monthName = date('F', $first);
$weekDays = array('Su', 'M', 'Tu', 'W', 'Th', 'F', 'Sa');
$eventDays = getEventDays($month, $year);

// Start drawing calendar
$out = "<table id=\"myCalendar\">\n";
$out .= "<tr><th colspan=\"7\">$monthName $year</th></tr>\n";
$out .= "<tr>\n";
foreach ($weekDays as $wd) $out .= "<td class=\"weekDays\">$wd</td>\n";

$i = 0;
for ($d = (1 - $offset); $d <= $daysInMonth; $d++) {
if ($i % 7 == 0) $out .= "<tr>\n"; // Start new row
if ($d < 1) $out .= "<td class=\"nonMonthDay\">&nbsp;</td>\n";
else {
if (in_array($d, $eventDays)) {
$title = "";entryInfoQuery($d,($month-1),$prevMonth_name,$prevMonthYear);
$out .= "<td class=\"monthDay\">\n";
$out .= "<a href=\"?year=$year&amp;month=$month&amp;day=$d\">$d</a>\n<br />";
$out .= "$title";
$out .= "</td>\n";
} else $out .= "<td class=\"monthDay\">$d</td>\n";
}
++$i; // Increment position counter
if ($i % 7 == 0) $out .= "</tr>\n"; // End row on the 7th day
}

// Round out last row if we don't have a full week
if ($i % 7 != 0) {
for ($j = 0; $j < (7 - ($i % 7)); $j++) {
$out .= "<td class=\"nonMonthDay\">&nbsp;</td>\n";
}
$out .= "</tr>\n";
}

$out .= "</table>\n";

return $out;
}

$year = isset($_GET['year']) ? $_GET['year'] : date('Y');
$month = isset($_GET['month']) ? $_GET['month'] : date('m');

echo drawCalendar($month, $year);

// Previous month link
$prevTS = strtotime("$year-$month-01 -1 month"); // timestamp of the first of last month
$pMax = date('t', $prevTS);
$pDay = ($day > $pMax) ? $pMax : $day;
list($y, $m) = explode('-', date('Y-m', $prevTS));
echo "<p>\n";
echo "<a href=\"?year=$year&amp;month=$m&amp;day=$pDay\">&laquo; Prev</a> |\n";

// Next month link
$nextTS = strtotime("$year-$month-01 +1 month");
$nMax = date('t', $nextTS);
$nDay = ($day > $nMax) ? $nMax : $day;
list($y, $m) = explode('-', date('Y-m', $nextTS));
echo "<a href=\"?year=$y&amp;month=$m&amp;day=$nDay\">Next &raquo;</a>\n";
echo "</p>\n";

// Calendar is done, let's list events for selected day
$sql = mysql_query("SELECT * FROM calendar_events WHERE event_date = '$year-$month-$day'");
if (mysql_num_rows($sql) > 0) {
while ($e = mysql_fetch_array($sql)) {
echo "<p>$e[event_title]</p>\n";
}
} else {
echo "<p>No events today</p>\n";
}
?>

kproc
02-28-2007, 07:22 PM
I'm not sure how to incorporate my variables into the below function.


function entryInfoQuery($date,$month,$monthname,$year)

NightShift58
02-28-2007, 10:36 PM
<style>
body {font-family: Arial;}
table {font-size: 11pt; width: 180px;}
td {padding-left:0px; padding-right:4px; padding-top:0px; padding-bottom:1px;}
a {color:navy; text-decoration: none; }
.smCal {border:1px solid navy;}
.month {color: white; background-color: navy; font-weight:bold; font-size:10pt; text-align:center; font-variant: small-caps;}
.nav a {color: white; background-color: navy; font-weight:bold; font-size:12pt; vertical-align:middle}
.header {color: DarkRed; background-color: e0e0e0; font-weight:bold; text-align=right; font-size: 9pt;}
.active {color: navy; background-color: white; font-weight:bold; text-align:right;}
.inactive {color: silver; background-color: white; font-weight:normal; text-align:right;}
.nav {color: white; background-color: navy; font-weight:bold; font-size:12pt; vertical-align:middle}
.active a:hover {color: red;}
.active a:focus {color: red;}
.month a {color: yellow; background-color: navy; font-weight:bold; font-size:11pt;}
</style>

<?
include "DBconnect2.inc.php";

$sql = "SELECT event_date FROM calendar_events GROUP BY event_date ORDER BY event_date";
$qry = mysql_query($sql) or die("SQL Error: $sql<br" . mysql_error());

$arrEVENTS = array();
WHILE ($r = mysql_fetch_array($qry)) :
$arrEVENTS[$r['event_date']] = 1;
ENDWHILE;

$pMON = (isset($_GET['m']) ? $_GET['m'] : gmdate("Ym"));

$thisMONyy = substr($pMON,0,4);
$thisMONmm = substr($pMON,4,2);

$BOM = gmmktime(0,0,0,$thisMONmm,1,$thisMONyy);
$EOM = gmmktime(0,0,0,$thisMONmm+1,0,$thisMONyy);
$BOC = gmmktime(0,0,0,$thisMONmm,1-gmdate("w", $BOM),$thisMONyy);
$EOC = gmmktime(0,0,0,$thisMONmm+1,0+(6-gmdate("w",$EOM)),$thisMONyy);

$PREVmon = gmdate("Ym",gmmktime(0,0,0,$thisMONmm-1,1,$thisMONyy));
$NEXTmon = gmdate("Ym",gmmktime(0,0,0,$thisMONmm+1,1,$thisMONyy));

$PREVyear = gmdate("Ym",gmmktime(0,0,0,$thisMONmm,1,$thisMONyy-1));
$NEXTyear = gmdate("Ym",gmmktime(0,0,0,$thisMONmm,1,$thisMONyy+1));

print "<table style='smCal' cellspacing=0 cellpadding=0 bgcolor=c0c0c0>";
print "<tr bgcolor=navy>";
print "<td class='month' width='100%' colspan='7'>";
print "<table width='100%'><tr>";
print "<td align='left'>";
print "<a href='{$_SERVER['PHP_SELF']}?m=$PREVyear'>&laquo;</a>&nbsp;";
print "<a href='{$_SERVER['PHP_SELF']}?m=$PREVmon'>&lt;</a>";
print "</td>";
print "<td class='month'>" . gmdate("F Y", $BOM) . "</td>";
print "<td align='right'>";
print "<a href='{$_SERVER['PHP_SELF']}?m=$NEXTmon'>&gt;</a>";
print "<a href='{$_SERVER['PHP_SELF']}?m=$NEXTyear'>&raquo;</a>";
print "</td>";
print "</tr></table>";
print "</td>";
print "<tr>";

print "<tr class='header'>";
$DOWheader = array("Su","Mo","Tu","We","Th","Fr","Sa");
foreach($DOWheader as $dow => $thisHEADER) :
print "<td align='right'><b>$thisHEADER</b></td>";
endforeach;
print "</tr>\n";

$rows = 0;
$x = $BOC;
DO {
$dow = gmdate("w", $x);
if ($dow == 0) : print "<tr>\n"; endif;
$thisSTYLE = "";
IF (isset($arrEVENTS[gmdate("Y-m-d",$x)])) :
$thisSTYLE = "style='color:DarkGreen;'";
ENDIF;
print "<td class='" . (gmdate("Ym",$x) == $pMON ? "active" : "inactive") . "'>";
IF (isset($arrEVENTS[gmdate("Y-m-d",$x)])) :
print "<a href='details_page.php?dt=" . gmdate("Y-m-d", $x) . "' $thisSTYLE>";
ENDIF;
print gmdate("j",$x);
IF (isset($arrEVENTS[gmdate("Y-m-d",$x)])) :
print "</a>";
ENDIF;
print "</td>\n";
if ($dow == 6) : print "</tr>\n"; $rows++; endif;
$x+=86400;
} WHILE ($x <= $EOC and $rows <= 5);

print "<table>";
?>

kproc
02-28-2007, 10:41 PM
Thank you for the help, for some reason it not including the title of an event in my database that should show under the date 02/27/07

kproc
02-28-2007, 10:44 PM
when you advance to the next month it advances to the next year for some reason

kproc
02-28-2007, 10:46 PM
for get the last post, there was an extra level added for advancing by year, excellent feature