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!

| Changing The Decor |

The Calendar package also includes what it calls "decorators". No, these aren't the guys who attempt to re-arrange your living room, but a set of tools to help you create more user-friendly and usable calendars. The one you're likely to use fairly often is called the Textual decorator, which lets you print names instead of numbers for months and weekdays. Here's an example:


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

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

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

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

// print day names
$dayNames = $deco->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>";
    // else print the day number
    } else {
        echo "<td align=right>" . $day->thisDay() . "</td>";
    }

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


In this case, an instance of the Calendar_Decorator_Textual class is initialized with a Month() object. This decorator object is then used to print month names and weekday names, via its thisMonthName() and orderedWeekdays() methods respectively. Here's what the result looks like:

Output image

Notice that this script differs from previous ones, in that it generates the calendar using HTML table cells. I've done this mostly because it provides me with the opportunity to showcase another method of the Day() object, the isLast() method. This method does the opposite of the isFirst() method discussed earlier, indicating whether the particular day falls at the end of the week. If isLast() returns true, it means it's time to end the current table row with a </tr>.


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