PHP
  Home arrow PHP arrow Page 4 - A Useful Event Calendar Written In PHP
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

A Useful Event Calendar Written In PHP
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 124
    2002-06-10

    Table of Contents:
  • A Useful Event Calendar Written In PHP
  • The event calendars logic
  • Creating the calendar
  • Highlighting days with events
  • Adding an event
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    A Useful Event Calendar Written In PHP - Highlighting days with events


    (Page 4 of 6 )

    As I mentioned earlier, days that have events attached to them are flagged with a blue background thanks to the use of some simple CSS styling. We have a function called ReadEvents that opens the events text file and reads each event into a multi-dimensional associative array. This array is then dissected and checked for events that match each particular day of the month.

    ReadEvents accepts one parameter, which is the month to retrieve all events for:

    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>");


    We make sure that the events file exists with the use of file_exists. If you receive errors here then make sure that the directory where the event file exists is CHMOD'ed with write attributes.

    In the code above we use PHP's DOCUMENT_ROOT server variable to make sure that we have the correct path to the events file. The location of the events file is defined at the top of cal.php as the EVENT_FILE constant and looks like this:

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

    You can of course change this to whatever you like. Once we've opened the events file, we use fread to read the contents of the file 1,024 bytes at a time into the $events variable:

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

    @fclose($fp);


    Remember that each event has three lines: the date it was posted, a name, and the actual description of the event. We use the explode function to grab all lines separated by the newline character into an array, like so:

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


    Next up we have a for loop. This for loop increments through the $arrEvents array three lines at a time, which means that it only checks the date of each event. It uses the explode function to get the month, day and year of each event into an array called $arrEventDate.

    With this array it compares the month that the event was posted to the current month selected on the calendar. If they are the same then that event is added to the $theEvents variable as an associative array, like this:

    // 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;

    We now have an associative array that contains all events posted for the selected month. We use PHP's foreach construct as well as several if statements to loop through each event and determine which day is was posted for. We then setup the appropriate style for that day's table cell in the calendar table:

    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'";


    Browsing by day
    OK, we can now show each month on the calendar, but we also need to show each day on the calendar as well as the events attached to each day. To do this, we setup each day of the calendar as a hyperlink. Something like this:

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

    The key here is the sel=1 query string variable. We use this to flag that a day is selected and display the events for that day, like so:

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


    After the for loop, we have a variable called $todaysEvents that contains the HTML code for all events on the selected day. We then use an if statement to show the events under the calendar:

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


    Here's how a day looks with some events attached to it:

    Events attached to a particular day

    More PHP Articles
    More By Mitchell Harper


       · Good Tutorial.The full source code could be nice, as I am having problems with the...
       · I cannot seem to locate the mentioned support files link! Any help would be...
       · can we have the source code please.thank you
       · Please send me the complete source code, somethings wrong withmy coding, i can't...
     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP


     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT