{ throw new Exception"No event data was supplied."; } } } ?> Creating the Method to Store Event Objects in an Array Now that each event can be stored as an object, you can create the
Trang 1* The event ID
*
* @var int
*/
public $id;
/**
* The event title
*
* @var string
*/
public $title;
/**
* The event description
*
* @var string
*/
public $description;
/**
* The event start time
*
* @var string
*/
public $start;
/**
* The event end time
*
* @var string
*/
public $end;
/**
* Accepts an array of event data and stores it
*
* @param array $event Associative array of event data
* @return void
*/
public function construct($event)
{
if ( is_array($event) )
{
$this->id = $event['event_id'];
$this->title = $event['event_title'];
$this->description = $event['event_desc'];
$this->start = $event['event_start'];
$this->end = $event['event_end'];
}
else
Trang 2{
throw new Exception("No event data was supplied.");
}
}
}
?>
Creating the Method to Store Event Objects in an Array
Now that each event can be stored as an object, you can create the method that will loop through the available events and store them in an array corresponding to the dates on which they occur First, load
the event data from the database using _loadEventData() Next, extract the day of the month from each event’s start date and add a new value to the array at that day’s index In the Calendar class, create a new method called _createEventObj() and set it to private Load the events from the database, and create the
new array using the following bold code:
<?php
class Calendar extends DB_Connect
{
private $_useDate;
private $_m;
private $_y;
private $_daysInMonth;
private $_startDay;
public function construct($dbo=NULL, $useDate=NULL) { }
private function _loadEventData($id=NULL) { }
/**
* Loads all events for the month into an array
*
* @return array events info
*/
private function _createEventObj()
{
Trang 3* Create a new array, then organize the events
* by the day of the month
on which they occur
*/
$events = array();
foreach ( $arr as $event )
{
$day = date('j', strtotime($event['event_start']));
try
{
$events[$day][] = new Event($event);
}
catch ( Exception $e )
{
die ( $e->getMessage() );
}
}
return $events;
}
}
?>
Now the events can be loaded and organized in such a way that the method to output the actual
calendar, HTML can easily put dates in the proper place
Outputting HTML to Display the Calendar and Events
At this point, you have the database set up, test events stored, and methods in place to load and organize the event data into an easy-to-use array You’re ready to put the pieces together and build a calendar!
The calendar will be built by a public method called buildCalendar() This will generate a calendar
with the following attributes:
• A heading that will show the month and year being displayed
• Weekday abbreviations to make the calendar look like a calendar
• Numbered boxes that contain events if they exist for the given date
To start, declare the buildCalendar() method in the Calendar class, and create the heading in an H2
element Also, create an array of weekday abbreviations and loop through them to generate an
unordered list Add the following bold code to do so:
Trang 4<?php
class Calendar extends DB_Connect
{
private $_useDate;
private $_m;
private $_y;
private $_daysInMonth;
private $_startDay;
public function construct($dbo=NULL, $useDate=NULL) { } private function _loadEventData($id=NULL) { }
private function _createEventObj() { }
/**
* Returns HTML markup to display the calendar and events
*
* Using the information stored in class properties, the
* events for the given month are loaded, the calendar is
* generated, and the whole thing is returned as valid markup
*
* @return string the calendar HTML markup
*/
public function buildCalendar()
{
/*
* Determine the calendar month and create an array of
* weekday abbreviations to label the calendar columns */
$cal_month = date('F Y', strtotime($this->_useDate)); $weekdays = array('Sun', 'Mon', 'Tue',
'Wed', 'Thu', 'Fri', 'Sat');
/*
* Add a header to the calendar markup
*/
$html = "\n\t<h2>$cal_month</h2>";
Trang 5/*
* Return the markup for output
*/
return $html;
}
}
?>
Modifying the Index File
To see the output of the buildCalendar() method, you’ll need to modify index.php in the public folder to
call the method Update the file with the code shown in bold:
<?php
/*
* Include necessary files
*/
include_once ' /sys/core/init.inc.php';
/*
* Load the calendar for January
*/
$cal = new Calendar($dbo, "2010-01-01 12:00:00");
/*
* Display the calendar HTML
*/
echo $cal->buildCalendar();
?>
Pull up the file in your browser to see the results so far (see Figure 4-4)
Trang 6Figure 4-4 The heading and weekday abbreviations
Building the Calendar
The next step is to build the actual calendar days Several steps need to be completed for this to work out:
1 Create a new unordered list
2 Set up a loop (with an iteration counter, a calendar date counter, today’s date,
and the month and year stored as variables) that runs as long as the calendar date counter is less than the number of days in the month
3 Add a fill class to the days of the week that occur before the first
4 Add a today class if the current date is contained within the same month and
year and matches the date being generated
5 Create an opening and closing list item tag for each day
6 Check if the current calendar box falls within the current month, and add the
date if so
Trang 79 After the loop, run another loop to add filler days until the calendar week is
completed
10 Close the final unordered list and return the markup
To start, complete steps 1 and 2 by adding the following bold code to the buildCalendar() method: public function buildCalendar()
{
/*
* Determine the calendar month and create an array of
* weekday abbreviations to label the calendar columns
*/
$cal_month = date('F Y', strtotime($this->_useDate));
$weekdays = array('Sun', 'Mon', 'Tue',
'Wed', 'Thu', 'Fri', 'Sat');
/*
* Add a header to the calendar markup
*/
$html = "\n\t<h2>$cal_month</h2>";
for ( $d=0, $labels=NULL; $d<7; ++$d )
{
$labels = "\n\t\t<li>" $weekdays[$d] "</li>";
}
$html = "\n\t<ul class=\"weekdays\">"
$labels "\n\t</ul>";
/*
* Create the calendar markup
*/
$html = "\n\t<ul>"; // Start a new unordered list
for ( $i=1, $c=1, $t=date('j'), $m=date('m'), $y=date('Y');
$c<=$this->_daysInMonth; ++$i )
{
// More steps go here
}
/*
* Return the markup for output
*/
return $html;
}
Next, add the bold code below to complete steps 3–5:
public function buildCalendar()
{
/*
* Determine the calendar month and create an array of
* weekday abbreviations to label the calendar columns
*/
Trang 8$cal_month = date('F Y', strtotime($this->_useDate)); $weekdays = array('Sun', 'Mon', 'Tue',
'Wed', 'Thu', 'Fri', 'Sat');
/*
* Add a header to the calendar markup
*/
$html = "\n\t<h2>$cal_month</h2>";
for ( $d=0, $labels=NULL; $d<7; ++$d )
{
$labels = "\n\t\t<li>" $weekdays[$d] "</li>"; }
$html = "\n\t<ul class=\"weekdays\">"
$labels "\n\t</ul>";
/*
* Create the calendar markup
*/
$html = "\n\t<ul>"; // Start a new unordered list
for ( $i=1, $c=1, $t=date('j'), $m=date('m'), $y=date('Y'); $c<=$this->_daysInMonth; ++$i )
{
/*
* Apply a "fill" class to the boxes occurring before
* the first of the month
*/
$class = $i<=$this->_startDay ? "fill" : NULL;
/*
* Add a "today" class if the current date matches
* the current date
*/
if ( $c==$t && $m==$this->_m && $y==$this->_y )
{
$class = "today";
}
/*
* Build the opening and closing list item tags
*/
$ls = sprintf("\n\t\t<li class=\"%s\">", $class); $le = "\n\t\t</li>";
// More steps go here
}
Trang 9To complete steps 6-10—actually build the dates; check if the week needs to wrap; assemble the
date markup; finish the last week out with filler, and return the markup—add the following bold code:
public function buildCalendar()
{
/*
* Determine the calendar month and create an array of
* weekday abbreviations to label the calendar columns
*/
$cal_month = date('F Y', strtotime($this->_useDate));
$weekdays = array('Sun', 'Mon', 'Tue',
'Wed', 'Thu', 'Fri', 'Sat');
/*
* Add a header to the calendar markup
*/
$html = "\n\t<h2>$cal_month</h2>";
for ( $d=0, $labels=NULL; $d<7; ++$d )
{
$labels = "\n\t\t<li>" $weekdays[$d] "</li>";
}
$html = "\n\t<ul class=\"weekdays\">"
$labels "\n\t</ul>";
/*
* Create the calendar markup
*/
$html = "\n\t<ul>"; // Start a new unordered list
for ( $i=1, $c=1, $t=date('j'), $m=date('m'), $y=date('Y');
$c<=$this->_daysInMonth; ++$i )
{
/*
* Apply a "fill" class to the boxes occurring before
* the first of the month
*/
$class = $i<=$this->_startDay ? "fill" : NULL;
/*
* Add a "today" class if the current date matches
* the current date
*/
if ( $c+1==$t && $m==$this->_m && $y==$this->_y )
{
$class = "today";
}
/*
* Build the opening and closing list item tags
*/
$ls = sprintf("\n\t\t<li class=\"%s\">", $class);
Trang 10$le = "\n\t\t</li>";
/*
* Add the day of the month to identify the calendar box
*/
if ( $this->_startDay<$i && $this->_daysInMonth>=$c)
{
$date = sprintf("\n\t\t\t<strong>%02d</strong>",$c++);
}
else { $date=" "; }
/*
* If the current day is a Saturday, wrap to the next row
*/
$wrap = $i!=0 && $i%7==0 ? "\n\t</ul>\n\t<ul>" : NULL;
/*
* Assemble the pieces into a finished item
*/
$html = $ls $date $le $wrap;
}
/*
* Add filler to finish out the last week
*/
while ( $i%7!=1 )
{
$html = "\n\t\t<li class=\"fill\"> </li>";
++$i;
}
/*
* Close the final unordered list
*/
$html = "\n\t</ul>\n\n";
/*
* Return the markup for output
*/
return $html;
}
Test the function as it stands now, and you’ll see the unordered lists in your browser (see Figure 4-5)