Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 101

Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 107

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 109

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 124

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 125

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 127

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174
The Melonfire Community - Trog
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!

| Time Travel |

Now, you'll have noticed that most Web-based calendars include the ability to scroll forward and backward between months. If you've ever tried coding this manually, you already know just how difficult this seemingly-innocuous feature can be: you've got to worry about invalid month and year values, as well as rolling over to the following or previous year when year boundaries are breached.

With the Calendar package's Uri decorator, all this worry is a thing of the past. This decorator takes care of building navigation links to move from one month to another. Here's how:


<html>
<head></head>
<body>
<table>
<?php
// include classes
include("Calendar/Month/Weekdays.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");

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

// create decorator
$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>";

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


Here's what it looks like:

Output image

Here, each time a user clicks the next or previous hyperlink, the PHP script calls itself with a new set of month and year parameters. These parameters are then used to generate an appropriate calendar. The URL code for the hyperlinks is generated by calling the Calendar_Decorator_Uri object's next() and prev() methods.

If you'd like to hyperlink every single day in the rendered calendar - useful, for example, if you're building an online appointment calendar - you can use the Calendar_Decorator_Uri to do this too. Simply wrap each day number in a hyperlink created by calling the decorator's this() method, as in the next example:


<html>
<head></head>
<body>
<table>
<?php
// include classes
include("Calendar/Month/Weekdays.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");

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

// 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>";
    // else print the day number
    // 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>


And here's what this looks like:

Output image

Notice that each day number is linked to the example script processor.php, and passes it the current month, day and year as GET variables. The hard work of creating the hyperlink is done by the decorator's this() method.


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-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 731
2018 Melonfire. All rights reserved
Terms and Conditions | Feedback