Drop Shadow

Automatic Calendar

If you have a database of dates, you will often want to display them as a calendar. This calendar include file will automatically create a weekly calendar or a monthly calendar of events.

For example, if you always want to display the current month and the next month from your MySQL database, use:

<? include("/web/includes/calendar.php"); $myCalendar = new autocalendar(); $myCalendar->open("database name", "username", "password"); $myCalendar->events("table name", "date field", "title field"); $myCalendar->monthly(2); $myCalendar->close(); ?> <link rel="stylesheet" href="/css/calendar.css" type="text/css" media="all" />

That’s all you need to do. If you want one month at a time, replace “monthly(2)” with “monthly(1)”. If you want four months, replace it with “monthly(4)”.

Wherever you put “$myCalendar->monthly()” on your page, a calendar will be displayed.

If you’re using SQLite, replace the new line and the open line with:

$myCalendar = new sqliteautocalendar(); $myCalendar->open("database file");

The other lines all remain the same.

More detailed instructions follow. If you want to provide RSS and icalendar feeds, for example, read on.

Create a PHP web page

Your web page must be a PHP-based web page. Currently, all web pages based on the official USD templates are PHP pages. You can usually tell a PHP page because its filename will end in “.php”.

Add the style sheet

In order to correctly display different kinds of calendars, you will need to include a special calendar style sheet.

<link rel="stylesheet" href="/css/calendar.css" type="text/css" media="all" />

The style sheet should be linked inside the “head” area of your web page. This is HTML, not PHP, so it should not be between <? and ?>.

Include the calendar file

Somewhere before you need the calendar, add the following inside a PHP section of your page (that is, between <? and ?>):

include('/web/includes/calendar.php');

This pulls in the PHP code necessary to create calendars.

Create your calendar

You will need to create a new calendar that knows where your database is and what table to get its information from. There are several ways to do this. The first step is to make the new calendar. There are different “autocalendars” for different data sources.

//create a calendar from MySQL data $myCalendar = new autocalendar(); //create a calendar from SQLite data $myCalendar = new sqliteautocalendar();

Make a connection to your data source

If you don’t already have a connection to your data source, you’ll need to tell your calendar how to make the connection. This will also tend to vary from data source to data source, because different data sources require different information.

//open a MySQL database $myCalendar->open("database name", "username", "password", "host name"); //open a SQLite database $myCalendar->open("/path/to/database/file");

The host name for the MySQL version is optional. If left out, autocalendar will assume the main USD MySQL database server.

If you already have a connection open, you don’t need to do anything: autocalendar (or, more precisely, PHP) will use that existing connection automatically. If you have more than one connection open and you need to use a specific one, call:

$myCalendar->connection(connection);

When you’re done with the calendar and don’t need it any more, you can:

$myCalendar->close();

Pass the table and field information

You’ll also need to tell autocalendar where your events are and how they are stored. Autocalendar needs to know the table the information is stored in, the field that contains the event date, the field that contains the title, and, for some calendars, the field that contains the description.

$myCalendar->events("table name", "date field", "title field", "description field");

The description field is optional. If left out, iCal and RSS calendars will not display a description.

Link to detail

Often, you will want your calendar to link to a more detailed page that you’ve created using some other software. You’ll need to tell autocalendar where that detail page is and which field in your table to use as a key for linking to the detail page.

$calendar->link("link URL", "key field");

If you leave the key field off, autocalendar will assume that the key is stored in a field called “ID”. (Note that PHP is case sensitive, so if your field is really called “id”—all lower case—you’ll need to specify that.)

There are two forms of detail pages. If the URL ends in an “=”, autocalendar will append the key field to the end of the URL. Usually, this is what you want:

$calendar->link("details.php?event=", "eventKey");

Otherwise, autocalendar will replace all instances of “__KEY__” in the URL with the key field.

$calendar->link("mailto:__KEY__@sandiego.edu", "username");

That will link each vacationers username (assuming the same connection info as used in the examples above) as a mailto: link. (Obviously, we would want to put this calendar behind a password so that spam harvesters can’t get at it.)

Request a calendar

There are several types of calendars, including weekly calendars, monthly calendars, iCalendar calendars, and RSS calendars.

Monthly calendars

Usually you’ll probably want a monthly calendar, or perhaps several months starting from the current month. Get a monthly calendar of the current month using:

$myCalendar->monthly();

Get a monthly calendar of a specific number of months, starting with the current month, using:

$myCalendar->monthly(number-of-months);

Get a monthly calendar from a month a specific number of months away from the current month using:

$myCalendar->monthly(month-offset, number-of-months);

For example:

//this month $myCalendar->monthly(); //this month and next month $myCalendar->monthly(2); //last month and this month $myCalendar->monthly(-1, 2); //next month and the month after $myCalendar->monthly(1, 2); //last month $myCalendar->monthly(-1);

Weekly calendars

You can also provide a summary of only the current week.

$myCalendar->weekly();

Get a weekly calendar of a specific number of weeks, starting with the current week, using:

$myCalendar->weekly(number-of-weeks);

Get a weekly calendar from a week a specific number of weeks away from the current week using:

$myCalendar->weekly(week-offset, number-of-weeks);

For example:

//this week $myCalendar->weekly(); //this week and next week $myCalendar->weekly(2); //last week and this week $myCalendar->weekly(-1, 2); //next week and the week after $myCalendar->weekly(1, 2); //last week $myCalendar->weekly(-1);

Style sheet

You can create your own styles to override the built-in style sheet.

div.autocalendarAll HTML created by the auto-calendar will be contained in a “div” with the class “autocalendar”.
table.calendarMonthly and weekly calendars are created using tables of the class “calendar”.
table.monthMonthly calendars are created using tables of the class “month”.
table.weekWeekly calendars are created using tables of the class “week”.
td.todayThe table cell that contains the current date will have the class “today”.
td.pastDates that have already past are in a cell with the class “past”.
td.weekendSaturday and Sunday cells have the class “weekend”.
td.offcalendarcells that are in the previous or next month have the class “offcalendar”.
p.dayThe number of the current day of the month is contained in a paragraph with the class “day”.
ul.eventsThe list of events is an unordered list with the class “events”.
ul.feedsThe list of available feeds is an unordered list with the class “feeds”.