Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Creating Web Calendars With The PEAR Calendar Class
Need a quick-and-dirty Web calendar? Look no further!

| Holiday Hi-Jinks |

Calendar also comes with built-in capabilities to flag certain days within the month as "special". These special days can be treated differently when displaying the calendar. A good example of this is an appointment calendar, wherein days which have active appointments can be highlighted in a different colour.

Consider the following example, which demonstrates how this works:


<html>
<head></head>
<body>
<table>
<?php
// include classes
include("Calendar/Month/Weekdays.php");
include("Calendar/Day.php");
include("Calendar/Decorator/Textual.php");

// create array of "special" days
// 3 June 2005 and 17 June 2005
$specialDays = array(
    new Calendar_Day(2005, 6, 3),
    new Calendar_Day(2005, 6, 17)
    );

// create and build month data structure for June 2005
// containing day objects
// classified by week
$month =& new Calendar_Month_Weekdays(2005, 6);
$month->build($specialDays);

// create decorator
$text =& new Calendar_Decorator_Textual($month);

// print month and year
echo "<td colspan=7 align=center>" . $text->thisMonthName('short') . " " . $text->thisYear() . "</td>";

// print day names
$dayNames = $text->orderedWeekdays('two');
echo "<tr>";
foreach ($dayNames as $name) {
    echo "<td>$name</td>";
}
echo "</tr>";

// iterate over days
// display as weekly calendar
while ($day = $month->fetch()) {
    // if first day of week
    // start a new row
    if ($day->isFirst()) {
        echo "<tr>";
    }

    // if this day does not belong to this month
    // print empty spaces
    if ($day->isEmpty()) {
        echo "<td>&nbsp;</td>";
    // if this day is "special"
    // highlight with yellow background
    } else if ($day->isSelected()) {
        echo "<td align=right bgcolor='yellow'>" . $day->thisDay() . "</td>";
    // else print day number
    } else {
        echo "<td align=right>" . $day->thisDay() . "</td>";
    }

    // if last day
    // end row
    if ($day->isLast()) {
        echo "</tr>";
    }
}
?>
</table>
</body>
</html>


Here's what it might look like:

Output image

Here, the build() method is passed an array of Day() objects, which represent "special" days that are to be flagged. When iterating over the object collection, it now becomes possible to test each day with the isSelected() method to see if it is "special", and highlight it if so.

One application of this capability might be to visually mark public holidays in a calendar. To do this, first create a file containing the list of holidays, like this:


01-01-2005, New Year's Day
14-02-2005, Valentine's Day
17-09-2005, The Boss's Birthday


Then, write some code to read and parse this file, generate Day objects from it, and pass these Day objects to the build() method when rendering the calendar:

<html>
<head></head>
<body>
<table>
<?php
// include classes
include("Calendar/Month/Weekdays.php");
include("Calendar/Day.php");
include("Calendar/Decorator/Textual.php");
include("Calendar/Decorator/Uri.php");

// get month and year
// defaults to current month and year
// if no values available
$yy = (isset($_GET['yy']) && is_numeric($_GET['yy'])) ? $_GET['yy'] : date("Y");
$mm = (isset($_GET['mm']) && is_numeric($_GET['mm'])) ? $_GET['mm'] : date("m");

// retrieve holidays list
// get day/month/year components
// for each holiday
$lines = file("holidays.txt") or die("ERROR:Cannot find holiday database");
foreach ($lines as $line) {
    $fields = explode(",", $line);
    $date = explode("-", $fields[0]);
    $holidaysData[] = new Calendar_Day($date[2], $date[1], $date[0]);
}   

// create and build month data structure
// containing day objects
// classified by week
// take account of selected dates
$month =& new Calendar_Month_Weekdays($yy, $mm);
$month->build($holidaysData);

// create text decorator to get string names
$text =& new Calendar_Decorator_Textual($month);

// create URI helper for navigation
// define GET variables to use
$uri =& new Calendar_Decorator_Uri($month);
$uri->setFragments("yy", "mm");

// print prev page link
echo "<tr><td><a href=?" . $uri->prev('month') . "><<</a></td>";

// print month and year
echo "<td colspan=5 align=center>" . $text->thisMonthName('short') . " " . $text->thisYear() . "</td>";

// print next page link
echo "<td><a href=?" . $uri->next('month') . ">>></a></td></tr>";

// print day names
$dayNames = $text->orderedWeekdays('two');
echo "<tr>";
foreach ($dayNames as $name) {
    echo "<td>$name</td>";
}
echo "</tr>";

// reset URI helper for day links
// attach to dummy $day object
// set GET variables
$day =& new Calendar_Day(null, null, null);
$uri =& new Calendar_Decorator_Uri($day);
$uri->setFragments("yy", "mm", "dd");

// iterate over days
// display as weekly calendar
while ($day = $month->fetch()) {
    // if first day of week
    // start a new row
    if ($day->isFirst()) {
        echo "<tr>";
    }

    // if this day does not belong to this month
    // print empty spaces
    if ($day->isEmpty()) {
        echo "<td>&nbsp;</td>";
    // if this day is a holiday or a Sunday
    // print it without a hyperlink
    } else if ($day->isSelected() || $day->isLast()) {
        echo "<td align=right>" . $day->thisDay() . "</td>";
    // else print it
    // with a hyperlink
    } else {
        echo "<td align=right><a href=processor.php?" . $uri->this('day') . ">" . $day->thisDay() . "</a></td>";
    }

    // if last day
    // end row
    if ($day->isLast()) {
        echo "</tr>";
    }
}
?>
</table>
</body>
</html>
[/code]

Here, the calendar display code includes a test for special days (read: holidays) and Sundays. No hyperlinks are produced for these days in the calendar display. Here's what it might look like:

Output image


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Caching Web Sites With PEAR Cache
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback