Tương tác giữa PHP và jQuery - part 15 ppt

10 274 0
Tương tác giữa PHP và jQuery - part 15 ppt

Đang tải... (xem toàn văn)

Thông tin tài liệu

CHAPTER 4 ■ BUILD AN EVENTS CALENDAR 141 * 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 CHAPTER 4 ■ BUILD AN EVENTS CALENDAR 142 { 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() { /* * Load the events array */ $arr = $this->_loadEventData(); /* CHAPTER 4 ■ BUILD AN EVENTS CALENDAR 143 * 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: CHAPTER 4 ■ BUILD AN EVENTS CALENDAR 144 <?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>"; 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>"; CHAPTER 4 ■ BUILD AN EVENTS CALENDAR 145 /* * 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). CHAPTER 4 ■ BUILD AN EVENTS CALENDAR 146 Figure 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. 7. Check if the current calendar box is a Saturday, and close the list and open a new one if so. 8. Assemble the pieces of the list item and append them to the markup. CHAPTER 4 ■ BUILD AN EVENTS CALENDAR 147 9. 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 */ CHAPTER 4 ■ BUILD AN EVENTS CALENDAR 148 $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 } /* * Return the markup for output */ return $html; } CHAPTER 4 ■ BUILD AN EVENTS CALENDAR 149 To 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); CHAPTER 4 ■ BUILD AN EVENTS CALENDAR 150 $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="&nbsp;"; } /* * 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\">&nbsp;</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). . is_array($event) ) { $this->id = $event['event_id']; $this->title = $event['event_title']; $this->description = $event['event_desc']; $this->start = $event['event_start'];. "201 0-0 1-0 1 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 ) 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& apos;;

Ngày đăng: 04/07/2014, 17:20

Tài liệu cùng người dùng

Tài liệu liên quan