02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

* Create a new array, then organize the events<br />

* <strong>by</strong> the day of the month<br />

on which they occur<br />

*/<br />

$events = array();<br />

foreach ( $arr as $event )<br />

{<br />

$day = date('j', strtotime($event['event_start']));<br />

?><br />

}<br />

try<br />

{<br />

$events[$day][] = new Event($event);<br />

}<br />

catch ( Exception $e )<br />

{<br />

die ( $e->getMessage() );<br />

}<br />

}<br />

return $events;<br />

CHAPTER 4 ■ BUILD AN EVENTS CALENDAR<br />

Now the events can be loaded <strong>and</strong> organized in such a way that the method to output the actual<br />

calendar, HTML can easily put dates in the proper place.<br />

Outputting HTML to Display the Calendar <strong>and</strong> Events<br />

At this point, you have the database set up, test events stored, <strong>and</strong> methods in place to load <strong>and</strong> organize<br />

the event data into an easy-to-use array. You’re ready to put the pieces together <strong>and</strong> build a calendar!<br />

The calendar will be built <strong>by</strong> a public method called buildCalendar(). This will generate a calendar<br />

with the following attributes:<br />

• A heading that will show the month <strong>and</strong> year being displayed<br />

• Weekday abbreviations to make the calendar look like a calendar<br />

• Numbered boxes that contain events if they exist for the given date<br />

To start, declare the buildCalendar() method in the Calendar class, <strong>and</strong> create the heading in an H2<br />

element. Also, create an array of weekday abbreviations <strong>and</strong> loop through them to generate an<br />

unordered list. Add the following bold code to do so:<br />

143

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!