Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Events Calendar 1

Status
Not open for further replies.

TrueJoker

Technical User
Jun 14, 2006
115
GB
Hi,

I am attempting to code my own php events calendar myself and i emphasise the attempting. I have the following code so far!

Code:
<style>
.normal { 
font-family: Verdana; 
font-size: 8pt; 
color: #000000; 
height: 20; 
padding-left: 5pt; 
} 
.today { 
font-family: Verdana; 
font-size: 8pt; 
color: #FFFFFF; 
background-color: #CACACA; 
height: 20; 
padding-left: 5pt; 
} 
.selected { 
font-family: Verdana; 
font-size: 8pt; 
color: #FFFFFF; 
background-color: #C00000; 
height: 20; 
padding-left: 5pt; 
} 
.event { 
font-family: Verdana; 
font-size: 8pt; 
color: #000000; 
background-color: #C6D1DC; 
height: 20; 
padding-left: 5pt; 
} 
</style>
<?php

// Get values from query string 
$day = $_GET["day"]; 
$month = $_GET["month"]; 
$year = $_GET["year"]; 
$sel = $_GET["sel"]; 
$what = $_GET["what"]; 

if($day == "") 
$day = date("j"); 

if($month == "") 
$month = date("m"); 

if($year == "") 
$year = date("Y");

$currentTimeStamp = strtotime("$year-$month-$day"); 
$monthName = date("F", $currentTimeStamp); 
$numDays = date("t", $currentTimeStamp); 
$counter = 0; 
$numEventsThisMonth = 0; 
$hasEvent = false; 
$todaysEvents = "";

<table width='350' border='0' cellspacing='0' cellpadding='0'> 
<tr> 
<td class='head' width='50'>S</td> 
<td class='head' width='50'>M</td> 
<td class='head' width='50'>T</td> 
<td class='head' width='50'>W</td> 
<td class='head' width='50'>T</td> 
<td class='head' width='50'>F</td> 
<td class='head' width='50'>S</td> 
</tr> 

$numDays = date("t", $currentTimeStamp);

for($i = 1; $i < $numDays+1; $i++, $counter++) 
{ 
$timeStamp = strtotime("$year-$month-$i"); 

if($i == 1) 
{ 
// Workout when the first day of the month is 
$firstDay = date("w", $timeStamp); 

for($j = 0; $j < $firstDay; $j++, $counter++) 
echo "<td>&nbsp;</td>"; 
} 

if($counter % 7 == 0) 
echo "</tr><tr>"; 

echo "<td width='50'>$I</td>"; 

if(date("w", $timeStamp) == 0 || date("w", $timeStamp) == 6) 
echo "class='weekend'"; 
else 
if($i == date("d") && $month == date("m") && $year == date("Y")) 
echo "class='today'"; 
else 
echo "class='normal'";

$monthName = date("F", $currentTimeStamp);

?>

<tr> 
<td width='50' colspan='1'> 
<input type='button' value=' < ' onClick='goLastMonth(<?php echo $month . ", " . $year; ?>)'> 
</td> 
<td width='250' colspan='5'> 
<span class='title'><?php echo $monthName . " " . $year; ?></span><br> 
</td> 
<td width='50' colspan='1' align='right'> 
<input type='button' value=' > ' onClick='goNextMonth(<?php echo $month . ", " . $year; ?>)'> 
</td> 
</tr>

<?php
function goLastMonth(month, year) 
{ 
// If the month is Januaru, decrement the year 
if(month == 1) 
{ 
--year; 
month = 13; 
} 

document.location.href = 'cal.php?month='+(month-1)+'&year='+year; 
} 
function goNextMonth(month, year) 
{ 
// If the month is December, increment the year 
if(month == 12) 
{ 
++year; 
month = 0; 
} 

document.location.href = 'cal.php?month='+(month+1)+'&year='+year; 
} 
function ReadEvents($Month) 
{ 
// We will get all of the events for this month into an associative 
// array and that array will then be returned 

$theEvents = array(); 
$eventCounter = 0; 

// Make sure that the file exists 
if(!file_exists($_SERVER["DOCUMENT_ROOT"] . "/" . EVENT_FILE)) 
{ 
$fp = @fopen($_SERVER["DOCUMENT_ROOT"] . "/" . EVENT_FILE, "w") 
or die("<span class='error'>ERROR: Couldn't create events file.</span>"); 
@fclose($fp); 
} 

$fp = @fopen($_SERVER["DOCUMENT_ROOT"] . "/" . EVENT_FILE, "rb") 
or die("<span class='error'>ERROR: Couldn't open events file to read events.</span>");

define("EVENT_FILE", "cal_events.text");

while($data = fread($fp, 1024)) 
{ 
$events .= $data; 
} 

@fclose($fp);

// Seperate the data into line-seperated arrays 
$arrEvents = explode("\r\n", $events);

// Loop through the results and pick the arrays 
// that match the selected month 
for($i = 0; $i < sizeof($arrEvents); $i+=3) 
{ 
// Get each part of the events date as an index 
// of an array 
$arrEventDate = explode(" ", $arrEvents[$i]); 

// If the month is the selected month the grab 
// the details of this event 
if((int)$arrEventDate[0] == (int)$Month) 
{ 
$theEvents[$eventCounter++] = array("day" => $arrEventDate[1], "name" => $arrEvents[$i+1], "desc" => $arrEvents[$i+2]); 
} 
} 

Lastly, we return the array: 

return $theEvents; 

foreach($arrEvents as $eventEntry) 
{ 
if($eventEntry["day"] == $i) 
{ 
// We have at least one event for the day 
$hasEvent = true; 
$numEventsThisMonth++; 
… 
} 
} 
} 

// Is it a weekend, does it have events, etc 
if($i == $day && $month == date("m") && $year == date("Y") && $sel == 1) 
echo "class='selected'"; 
else if($i == $day && $sel == 1) 
echo "class='selected'"; 
else if($hasEvent == true) 
echo "class='event'"; 
else 
if(date("w", $timeStamp) == 0 || date("w", $timeStamp) == 6) 
echo "class='weekend'"; 
else 
if($i == date("d") && $month == date("m") && $year == date("Y")) 
echo "class='today'"; 
else 
echo "class='normal'";

cal.php?sel=1&day=11&month=6&year=2002 

$arrEvents = ReadEvents($month); 

foreach($arrEvents as $eventEntry) 
{ 
if($eventEntry["day"] == $i) 
{ 
// We have at least one event for the day 
$hasEvent = true; 
$numEventsThisMonth++; 

if($eventEntry["day"] == $day) 
{ 
// Add the event to the $todaysEvents variable 
$todaysEvents .= "<span class='eventTitle'>" . stripslashes($eventEntry["name"]) . "</span>"; 

$todaysEvents .= "<span class='eventDesc'><br>" . stripslashes($eventEntry["desc"]) . "</span><br>"; 

$todaysEvents .= " <a href='cal.php?sel=1&what=delPost&day=$day&month=$month&year=$year&eName=" . urlencode($eventEntry["name"]) . "&eDesc=" . urlencode($eventEntry["desc"]) . "'>[Remove]</a><br><br>"; 
} 
} 
} 

if($sel == 1) 
{ 
echo "<tr>"; 
echo " <td width='350' colspan='7'>"; 
echo " <hr size='1' color='#CACACA' noshade>"; 
echo " <span class='Title'>Today's Events</span><br><br>"; 
echo $todaysEvents; 
echo " </td>"; 
echo "</tr>"; 
} 
?>

now what i am hoping to achieve is to have a small calendar displayed with any events for the elected day shown underneath which i think is possible although i ahve not achieved it yet! if anyone can help would be grateful :)
 
what you are asking is possible.

you are not doing yourself any favours using a flatfile storage mechanism. a db would be better.

what is the format of your flatfile? is it one file for all events or one file for each day?

and what is the internal format of each file (csv fields)?

 
hmmm. I was going to use one file for each day, but im not sure whether this is the best way to go cause i can imagen the whole thing becoming huge with events files coming out of my ears!

I have a working calendar but it doesnt seem to link each day to events or i have been unsuccessful in doing so thus far so ahve loked for alternatives!

Code:
<html> 
<head> 
<title>PHP Calendar</title> 
<style type="text/css"> 
<!-- 
.table.calendar {border: 1px solid #000000; border-collapse: collapse; color: #000000; background: #FFFFFF; } 
.td.today { border: 1px solid white; color: #000000; background: #EFEFEF; font-weight: bold;} 
.td.monthdays {border: 1px solid #434470; color: #000000; background: #FFFFFFF; } 
.td.nonmonthdays { border: 1px solid white; color: #000000; background: #EFEFEF;} 
--> 
</style> 
<body> 

<?php 
error_reporting('0'); 
ini_set('display_errors', '0'); 
// Gather variables from 
// user input and break them 
// down for usage in our script 

if(!isset($_REQUEST['date'])){ 
   $date = mktime(0,0,0,date('m'), date('d'), date('Y')); 
} else { 
   $date = $_REQUEST['date']; 
} 

$day = date('d', $date); 
$month = date('m', $date); 
$year = date('Y', $date); 

// Get the first day of the month 
$month_start = mktime(0,0,0,$month, 1, $year); 

// Get friendly month name 
$month_name = date('M', $month_start); 

// Figure out which day of the week 
// the month starts on. 
$month_start_day = date('D', $month_start); 

switch($month_start_day){ 
    case "Sun": $offset = 0; break; 
    case "Mon": $offset = 1; break; 
    case "Tue": $offset = 2; break; 
    case "Wed": $offset = 3; break; 
    case "Thu": $offset = 4; break; 
    case "Fri": $offset = 5; break; 
    case "Sat": $offset = 6; break; 
} 

// determine how many days are in the last month. 
if($month == 1){ 
   $num_days_last = cal_days_in_month(0, 12, ($year -1)); 
} else { 
   $num_days_last = cal_days_in_month(0, ($month -1), $year); 
} 
// determine how many days are in the current month. 
$num_days_current = cal_days_in_month(0, $month, $year); 

// Build an array for the current days 
// in the month 
for($i = 1; $i <= $num_days_current; $i++){ 
    $num_days_array[] = $i; 
} 

// Build an array for the number of days 
// in last month 
for($i = 1; $i <= $num_days_last; $i++){ 
    $num_days_last_array[] = $i; 
} 

// If the $offset from the starting day of the 
// week happens to be Sunday, $offset would be 0, 
// so don't need an offset correction. 

if($offset > 0){ 
    $offset_correction = array_slice($num_days_last_array, -$offset, $offset); 
    $new_count = array_merge($offset_correction, $num_days_array); 
    $offset_count = count($offset_correction); 
} 

// The else statement is to prevent building the $offset array. 
else { 
    $offset_count = 0; 
    $new_count = $num_days_array; 
} 

// count how many days we have with the two 
// previous arrays merged together 
$current_num = count($new_count); 

// Since we will have 5 HTML table rows (TR) 
// with 7 table data entries (TD) 
// we need to fill in 35 TDs 
// so, we will have to figure out 
// how many days to appened to the end 
// of the final array to make it 35 days. 


if($current_num > 35){ 
   $num_weeks = 6; 
   $outset = (42 - $current_num); 
} elseif($current_num < 35){ 
   $num_weeks = 5; 
   $outset = (35 - $current_num); 
} 
if($current_num == 35){ 
   $num_weeks = 5; 
   $outset = 0; 
} 
// Outset Correction 
for($i = 1; $i <= $outset; $i++){ 
   $new_count[] = $i; 
} 

// Now let's "chunk" the $all_days array 
// into weeks. Each week has 7 days 
// so we will array_chunk it into 7 days. 
$weeks = array_chunk($new_count, 7); 


// Build Previous and Next Links 
$previous_link = "<a href=\"".$_SERVER['PHP_SELF']."?date="; 
if($month == 1){ 
   $previous_link .= mktime(0,0,0,12,$day,($year -1)); 
} else { 
   $previous_link .= mktime(0,0,0,($month -1),$day,$year); 
} 
$previous_link .= "\"><< Prev</a>"; 

$next_link = "<a href=\"".$_SERVER['PHP_SELF']."?date="; 
if($month == 12){ 
   $next_link .= mktime(0,0,0,1,$day,($year + 1)); 
} else { 
   $next_link .= mktime(0,0,0,($month +1),$day,$year); 
} 
$next_link .= "\">Next >></a>"; 

// Build the heading portion of the calendar table 
echo "<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\" width=\"300\" class=\"calendar\">\n". 
     "<tr>\n". 
     "<td colspan=\"7\">\n". 
     "<table align=\"center\">\n". 
     "<tr>\n". 
     "<td colspan=\"2\" width=\"75\" align=\"left\">$previous_link</td>\n". 
     "<td colspan=\"3\" width=\"150\" align=\"center\">$month_name $year</td>\n". 
     "<td colspan=\"2\" width=\"75\" align=\"right\">$next_link</td>\n". 
     "</tr>\n". 
     "</table>\n". 
     "</td>\n". 
     "<tr>\n". 
     "<td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td>\n". 
     "</tr>\n"; 

// Now we break each key of the array  
// into a week and create a new table row for each 
// week with the days of that week in the table data 

$i = 0; 
foreach($weeks AS $week){ 
       echo "<tr>\n"; 
       foreach($week as $d){ 
         if($i < $offset_count){ 
             $day_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,$month -1,$d,$year)."\">$d</a>"; 
             echo "<td class=\"nonmonthdays\">$day_link</td>\n"; 
         } 
         if(($i >= $offset_count) && ($i < ($num_weeks * 7) - $outset)){ 
            $day_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,$month,$d,$year)."\">$d</a>"; 
           if($date == mktime(0,0,0,$month,$d,$year)){ 
               echo "<td class=\"today\">$d</td>\n"; 
           } else { 
               echo "<td class=\"days\">$day_link</td>\n"; 
           } 
        } elseif(($outset > 0)) { 
            if(($i >= ($num_weeks * 7) - $outset)){ 
               $day_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,$month +1,$d,$year)."\">$d</a>"; 
               echo "<td class=\"nonmonthdays\">$day_link</td>\n"; 
           } 
        } 
        $i++; 
      } 
      echo "</tr>\n";    
} 

// Close out your table and that's it! 
echo '<tr><td colspan="7" class="days"> </td></tr>'; 
echo '</table>'; 
?> 
</body> 
</html>

Is what i want possible with this code? if so how do i achieve it please!
 
using one for each day is fine. not too many files really. store them in the format yyyy-mm-dd.txt and store each event as a text line separated by \r\n

replace the foreach block of your second post with the following code:

Code:
foreach($weeks AS $week){
       echo "<tr>\n";
       foreach($week as $d){
         if($i < $offset_count){
             $day_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,$month -1,$d,$year)."\">$d</a>";
			 $m = $month-1;
             echo "<td class=\"nonmonthdays\">$day_link</td>\n";
         }
         if(($i >= $offset_count) && ($i < ($num_weeks * 7) - $outset)){
            $day_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,$month,$d,$year)."\">$d</a>";
			$m = $month;
           if($date == mktime(0,0,0,$month,$d,$year)){
               echo "<td class=\"today\">$d</td>\n";
           } else {
               echo "<td class=\"days\">$day_link</td>\n";
           }
        } elseif(($outset > 0)) {
            if(($i >= ($num_weeks * 7) - $outset)){
               $day_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,$month +1,$d,$year)."\">$d</a>";
               echo "<td class=\"nonmonthdays\">$day_link</td>\n";
			   $m = $month+1;
           }
        }
		//added code
		$filename = date ("Y-m-d", strtotime("$year-$m-$d"));
		$relativepathtoeventfile = "eventsdir/$filename.txt";
		if (file_exists($reltivepathtoeventfile)):
			echo "<br/><span class=\"eventdata\">" . nl2br(file_get_contents("eventsdir/$filename.txt")). "</span>";
		endif;
		//end added code
        $i++;
      }
      echo "</tr>\n";    
}

it goes without saying that this is going to bust your layou t completely. i'd be looking to move to a full page layout here and set a smallish font on the eventdata class.
 
thank you for that bit of code! and strangely enough hasnt bust my layout! at least it doesnt look like it.

So to store an event i would call it say 2006-07-15.txt

and the events would have to be stored as follows:

Code:
<?php

echo "10:30 - 12:30 Soccer Practice" \r\n;

echo "12:30 - Onwards Relax";

?>

at least i think thats how i would do the txt file.
 
it will bust your layout when you start getting events showing up!

for the events file just store them as plain text. no need for php tags. store each one on different line like so (no need for quotes or anything)

Code:
10:30 - 12:30 Soccer Practice
12:30 - Onwards Relax
 
ah i see!

thats all done! saved a file as 2006-07-15.txt

This should show up when i click the 15th july 2006 then!

but it doesnt show up when i click on the 15 july 2006

or is there something im missing [ponder]
 
it must be saved in the eventsdir subdirectory of the script. it won't show on a click but actually inside the box. i may have misunderstood.
 
hmmm

ok i have now structured it so the event file is in a folder named eventsdir (eventsdir/2007-07-15.txt) is the file directory but still nothing happens! now just to check cause i usually do get things wrong lol it was the code in the second post that was supposed to be altered?

just to clarify

Code:
<html> 
<head> 
<title>PHP Calendar</title> 
<style type="text/css"> 
<!-- 
.table.calendar {border: 1px solid #000000; border-collapse: collapse; color: #000000; background: #FFFFFF; } 
.td.today { border: 1px solid white; color: #000000; background: #EFEFEF; font-weight: bold;} 
.td.monthdays {border: 1px solid #434470; color: #000000; background: #FFFFFFF; } 
.td.nonmonthdays { border: 1px solid white; color: #000000; background: #EFEFEF;} 
--> 
</style> 
<body> 

<?php 
error_reporting('0'); 
ini_set('display_errors', '0'); 
// Gather variables from 
// user input and break them 
// down for usage in our script 

if(!isset($_REQUEST['date'])){ 
   $date = mktime(0,0,0,date('m'), date('d'), date('Y')); 
} else { 
   $date = $_REQUEST['date']; 
} 

$day = date('d', $date); 
$month = date('m', $date); 
$year = date('Y', $date); 

// Get the first day of the month 
$month_start = mktime(0,0,0,$month, 1, $year); 

// Get friendly month name 
$month_name = date('M', $month_start); 

// Figure out which day of the week 
// the month starts on. 
$month_start_day = date('D', $month_start); 

switch($month_start_day){ 
    case "Sun": $offset = 0; break; 
    case "Mon": $offset = 1; break; 
    case "Tue": $offset = 2; break; 
    case "Wed": $offset = 3; break; 
    case "Thu": $offset = 4; break; 
    case "Fri": $offset = 5; break; 
    case "Sat": $offset = 6; break; 
} 

// determine how many days are in the last month. 
if($month == 1){ 
   $num_days_last = cal_days_in_month(0, 12, ($year -1)); 
} else { 
   $num_days_last = cal_days_in_month(0, ($month -1), $year); 
} 
// determine how many days are in the current month. 
$num_days_current = cal_days_in_month(0, $month, $year); 

// Build an array for the current days 
// in the month 
for($i = 1; $i <= $num_days_current; $i++){ 
    $num_days_array[] = $i; 
} 

// Build an array for the number of days 
// in last month 
for($i = 1; $i <= $num_days_last; $i++){ 
    $num_days_last_array[] = $i; 
} 

// If the $offset from the starting day of the 
// week happens to be Sunday, $offset would be 0, 
// so don't need an offset correction. 

if($offset > 0){ 
    $offset_correction = array_slice($num_days_last_array, -$offset, $offset); 
    $new_count = array_merge($offset_correction, $num_days_array); 
    $offset_count = count($offset_correction); 
} 

// The else statement is to prevent building the $offset array. 
else { 
    $offset_count = 0; 
    $new_count = $num_days_array; 
} 

// count how many days we have with the two 
// previous arrays merged together 
$current_num = count($new_count); 

// Since we will have 5 HTML table rows (TR) 
// with 7 table data entries (TD) 
// we need to fill in 35 TDs 
// so, we will have to figure out 
// how many days to appened to the end 
// of the final array to make it 35 days. 


if($current_num > 35){ 
   $num_weeks = 6; 
   $outset = (42 - $current_num); 
} elseif($current_num < 35){ 
   $num_weeks = 5; 
   $outset = (35 - $current_num); 
} 
if($current_num == 35){ 
   $num_weeks = 5; 
   $outset = 0; 
} 
// Outset Correction 
for($i = 1; $i <= $outset; $i++){ 
   $new_count[] = $i; 
} 

// Now let's "chunk" the $all_days array 
// into weeks. Each week has 7 days 
// so we will array_chunk it into 7 days. 
$weeks = array_chunk($new_count, 7); 


// Build Previous and Next Links 
$previous_link = "<a href=\"".$_SERVER['PHP_SELF']."?date="; 
if($month == 1){ 
   $previous_link .= mktime(0,0,0,12,$day,($year -1)); 
} else { 
   $previous_link .= mktime(0,0,0,($month -1),$day,$year); 
} 
$previous_link .= "\"><< Prev</a>"; 

$next_link = "<a href=\"".$_SERVER['PHP_SELF']."?date="; 
if($month == 12){ 
   $next_link .= mktime(0,0,0,1,$day,($year + 1)); 
} else { 
   $next_link .= mktime(0,0,0,($month +1),$day,$year); 
} 
$next_link .= "\">Next >></a>"; 

// Build the heading portion of the calendar table 
echo "<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\" width=\"300\" class=\"calendar\">\n". 
     "<tr>\n". 
     "<td colspan=\"7\">\n". 
     "<table align=\"center\">\n". 
     "<tr>\n". 
     "<td colspan=\"2\" width=\"75\" align=\"left\">$previous_link</td>\n". 
     "<td colspan=\"3\" width=\"150\" align=\"center\">$month_name $year</td>\n". 
     "<td colspan=\"2\" width=\"75\" align=\"right\">$next_link</td>\n". 
     "</tr>\n". 
     "</table>\n". 
     "</td>\n". 
     "<tr>\n". 
     "<td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td>\n". 
     "</tr>\n"; 

// Now we break each key of the array  
// into a week and create a new table row for each 
// week with the days of that week in the table data 

$i = 0; 
foreach($weeks AS $week){
       echo "<tr>\n";
       foreach($week as $d){
         if($i < $offset_count){
             $day_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,$month -1,$d,$year)."\">$d</a>";
             $m = $month-1;
             echo "<td class=\"nonmonthdays\">$day_link</td>\n";
         }
         if(($i >= $offset_count) && ($i < ($num_weeks * 7) - $outset)){
            $day_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,$month,$d,$year)."\">$d</a>";
            $m = $month;
           if($date == mktime(0,0,0,$month,$d,$year)){
               echo "<td class=\"today\">$d</td>\n";
           } else {
               echo "<td class=\"days\">$day_link</td>\n";
           }
        } elseif(($outset > 0)) {
            if(($i >= ($num_weeks * 7) - $outset)){
               $day_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=".mktime(0,0,0,$month +1,$d,$year)."\">$d</a>";
               echo "<td class=\"nonmonthdays\">$day_link</td>\n";
               $m = $month+1;
           }
        }
        //added code
        $filename = date ("Y-m-d", strtotime("$year-$m-$d"));
        $relativepathtoeventfile = "eventsdir/$filename.txt";
        if (file_exists($reltivepathtoeventfile)):
            echo "<br/><span class=\"eventdata\">" . nl2br(file_get_contents("eventsdir/$filename.txt")). "</span>";
        endif;
        //end added code
        $i++;
      }
      echo "</tr>\n";    
}

// Close out your table and that's it! 
echo '<tr><td colspan="7" class="days"> </td></tr>'; 
echo '</table>'; 
?> 
</body> 
</html>

what i was hoping was for the events info to be shown under the calendar but to be honest id just be happy to get it all working first lol
 
sorry. spelling mistake. try this instead

Code:
//added code
        $filename = date ("Y-m-d", strtotime("$year-$m-$d"));
        $relativepathtoeventfile = "eventsdir/$filename.txt";
        if (file_exists($relativepathtoeventfile)):
            echo "<br/><span class=\"eventdata\">" . nl2br(file_get_contents($relativepathtoeventfile)). "</span>";
        endif;
 
right.
i've redone the code a bit so that the events show on the same page below the calendar.
Code:
<html>
<head>
<title>PHP Calendar</title>
<style type="text/css">
<!--
.table.calendar {border: 1px solid #000000; border-collapse: collapse; color: #000000; background: #FFFFFF; }
.td.today { border: 1px solid white; color: #000000; background: #EFEFEF; font-weight: bold;}
.td.monthdays {border: 1px solid #434470; color: #000000; background: #FFFFFFF; }
.td.nonmonthdays { border: 1px solid white; color: #000000; background: #EFEFEF;}
-->
</style>
<body>

<?php
error_reporting('0');
ini_set('display_errors', '0');
// Gather variables from
// user input and break them
// down for usage in our script

if(!isset($_REQUEST['date'])){
   $date = mktime(0,0,0,date('m'), date('d'), date('Y'));
} else {
   $date = $_REQUEST['date'];
}

$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);

// Get the first day of the month
$month_start = mktime(0,0,0,$month, 1, $year);

// Get friendly month name
$month_name = date('M', $month_start);

// Figure out which day of the week
// the month starts on.
$month_start_day = date('D', $month_start);

switch($month_start_day){
    case "Sun": $offset = 0; break;
    case "Mon": $offset = 1; break;
    case "Tue": $offset = 2; break;
    case "Wed": $offset = 3; break;
    case "Thu": $offset = 4; break;
    case "Fri": $offset = 5; break;
    case "Sat": $offset = 6; break;
}

// determine how many days are in the last month.
if($month == 1){
   $num_days_last = cal_days_in_month(0, 12, ($year -1));
} else {
   $num_days_last = cal_days_in_month(0, ($month -1), $year);
}
// determine how many days are in the current month.
$num_days_current = cal_days_in_month(0, $month, $year);

// Build an array for the current days
// in the month
for($i = 1; $i <= $num_days_current; $i++){
    $num_days_array[] = $i;
}

// Build an array for the number of days
// in last month
for($i = 1; $i <= $num_days_last; $i++){
    $num_days_last_array[] = $i;
}

// If the $offset from the starting day of the
// week happens to be Sunday, $offset would be 0,
// so don't need an offset correction.

if($offset > 0){
    $offset_correction = array_slice($num_days_last_array, -$offset, $offset);
    $new_count = array_merge($offset_correction, $num_days_array);
    $offset_count = count($offset_correction);
}

// The else statement is to prevent building the $offset array.
else {
    $offset_count = 0;
    $new_count = $num_days_array;
}

// count how many days we have with the two
// previous arrays merged together
$current_num = count($new_count);

// Since we will have 5 HTML table rows (TR)
// with 7 table data entries (TD)
// we need to fill in 35 TDs
// so, we will have to figure out
// how many days to appened to the end
// of the final array to make it 35 days.


if($current_num > 35){
   $num_weeks = 6;
   $outset = (42 - $current_num);
} elseif($current_num < 35){
   $num_weeks = 5;
   $outset = (35 - $current_num);
}
if($current_num == 35){
   $num_weeks = 5;
   $outset = 0;
}
// Outset Correction
for($i = 1; $i <= $outset; $i++){
   $new_count[] = $i;
}

// Now let's "chunk" the $all_days array
// into weeks. Each week has 7 days
// so we will array_chunk it into 7 days.
$weeks = array_chunk($new_count, 7);


// Build Previous and Next Links
$previous_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=";
if($month == 1){
   $previous_link .= mktime(0,0,0,12,$day,($year -1));
} else {
   $previous_link .= mktime(0,0,0,($month -1),$day,$year);
}
$previous_link .= "\"><< Prev</a>";

$next_link = "<a href=\"".$_SERVER['PHP_SELF']."?date=";
if($month == 12){
   $next_link .= mktime(0,0,0,1,$day,($year + 1));
} else {
   $next_link .= mktime(0,0,0,($month +1),$day,$year);
}
$next_link .= "\">Next >></a>";

// Build the heading portion of the calendar table
echo "<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\" width=\"300\" class=\"calendar\">\n".
     "<tr>\n".
     "<td colspan=\"7\">\n".
     "<table align=\"center\">\n".
     "<tr>\n".
     "<td colspan=\"2\" width=\"75\" align=\"left\">$previous_link</td>\n".
     "<td colspan=\"3\" width=\"150\" align=\"center\">$month_name $year</td>\n".
     "<td colspan=\"2\" width=\"75\" align=\"right\">$next_link</td>\n".
     "</tr>\n".
     "</table>\n".
     "</td>\n".
     "<tr>\n".
     "<td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td>\n".
     "</tr>\n";

// Now we break each key of the array  
// into a week and create a new table row for each
// week with the days of that week in the table data

$i = 0;
foreach($weeks AS $week){
       echo "<tr>\n";
       foreach($week as $d){
         if($i < $offset_count){
		 	$m = $month-1;
             $day_link = "<a href=\"".$_SERVER['PHP_SELF']."?action=showevent&ryear=$year&rmonth=$m&rday=$d\">$d</a>";
			 
             echo "<td class=\"nonmonthdays\">$day_link</td>\n";
         }
         if(($i >= $offset_count) && ($i < ($num_weeks * 7) - $outset)){
             $m = $month;
			 $day_link = "<a  href=\"".$_SERVER['PHP_SELF']."?action=showevent&ryear=$year&rmonth=$m&rday=$d\">$d</a>";
			
           if($date == mktime(0,0,0,$month,$d,$year)){
               echo "<td class=\"today\">$day_link</td>\n";
           } else {
               echo "<td class=\"days\">$day_link</td>\n";
           }
        } elseif(($outset > 0)) {
            if(($i >= ($num_weeks * 7) - $outset)){
				$m = $month+1;
				$day_link = "<a  href=\"".$_SERVER['PHP_SELF']."?action=showevent&ryear=$year&rmonth=$m&rday=$d\">$d</a>";
               echo "<td class=\"nonmonthdays\">$day_link</td>\n";
			   
           }
        }
        $i++;
      }
      echo "</tr>\n";    
}

// Close out your table and that's it!
echo '<tr><td colspan="7" class="days"> </td></tr>';
echo '</table>';
?>

<br /><br /><hr/>
Event entries for <? echo date("l j, F Y", strtotime($_GET['ryear']."-".$_GET['rmonth']."-".$_GET['rday']));?>
<br/>
<? 
	$relativepathtoeventfile = "eventsdir/".$_GET['ryear']."-".$_GET['rmonth']."-".$_GET['rday'].".txt";
	if (file_exists($relativepathtoeventfile)):
			echo "<div class=\"eventdata\">" . nl2br(file_get_contents($relativepathtoeventfile)). "</div>";
	endif;
?>
</body>
</html>
 
Wow thank you that works exactly how i wanted it to work! Thanx a million! :)
 
Just a quick question! is it possible to highlight the days that have events on them in a different colour so it makes it obvious which days events are scheduled for?
 
Just a quick question! is it possible to highlight the days that have events on them in a different colour so it makes it obvious which days events are scheduled for?

yes. here is a kludge.

Code:
foreach($weeks AS $week){
       echo "<tr>\n";
       foreach($week as $d){
     	 $style="";
         if($i < $offset_count){
		 	 $m = $month-1;
			 if (strlen($m) === 1) {$m = "0".$m;}
         if (file_exists("eventsdir/$year-$m-$d.txt")) {$style="style=\"border: 1px red dotted;\""; }
			 $day_link = "<a $style href=\"".$_SERVER['PHP_SELF']."?action=showevent&ryear=$year&rmonth=$m&rday=$d\">$d</a>";
			 
             echo "<td class=\"nonmonthdays\">$day_link</td>\n";
         }
         if(($i >= $offset_count) && ($i < ($num_weeks * 7) - $outset)){
             $m = $month;
			 
			 if (file_exists("eventsdir/$year-$m-$d.txt")) {$style="style=\"border: 1px red dotted;\"";}
			 $day_link = "<a  $style href=\"".$_SERVER['PHP_SELF']."?action=showevent&ryear=$year&rmonth=$m&rday=$d\">$d</a>";
			
           if($date == mktime(0,0,0,$month,$d,$year)){
               echo "<td class=\"today\">$day_link</td>\n";
           } else {
               echo "<td class=\"days\">$day_link</td>\n";
           }
        } elseif(($outset > 0)) {
            if(($i >= ($num_weeks * 7) - $outset)){
				$m = $month+1;
   			 if (strlen($m) === 1) {$m = "0".$m;}
			    if (file_exists("eventsdir/$year-$m-$d.txt")) {$style="style=\"border: 1px red dotted;\"";}
				$day_link = "<a  $style href=\"".$_SERVER['PHP_SELF']."?action=showevent&ryear=$year&rmonth=$m&rday=$d\">$d</a>";
               echo "<td class=\"nonmonthdays\">$day_link</td>\n";
			   
           }
        }
        $i++;
      }
      echo "</tr>\n";    
}

what would be better is if you replaced the link with a number for days with no events.

 
Thank you, for all your help! Really appreciate it! Hope this didnt give you too many grey hairs :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top