1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

Hour 12. Creating a Simple Calendar

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (7.37 MB, 561 trang )


This document is created with the unregistered version of CHM2PDF Pilot



[ Team LiB ]



This document is created with the unregistered version of CHM2PDF Pilot



Building a Simple Display Calendar

Let's bring together the date and time functions you learned in the previous hour, and use them to build a calendar

that can display the dates for any month between 1980 and 2010. The user will be able to select both month and

year with pull-down menus, and the dates for the selected month will be organized according to the days of the

week. We will be working with two variables—one for month and one for year—which the user will supply.



These pieces of information will be used to build a timestamp based on the first day of the month defined. If the user

input is invalid or absent, we will default to the first day of the current month.



Checking User Input

When the user opens our calendar application for the first time, he or she will not be submitting any information. We

must therefore make sure that our script can handle the fact that the variables for month and year may not be defined.

We could use the isset() function for this. (isset() returns false if the variable passed to it has not been defined.)

However, let's use checkdate() instead. Listing 12.1 shows the fragment of code that checks for month and year

variables coming from a form, and builds a timestamp based on them.

Listing 12.1 Checking User Input for the Calendar Script

1:

2:

3:

4:

5:

6:

7:

8:

9:

10:

11:

12:




if (!checkdate($_POST[month], 1, $_POST[year])) {

$nowArray = getdate();

$month = $nowArray['mon'];

$year = $nowArray['year'];

} else {

$month = $_POST[month];

$year = $_POST[year];

}

$start = mktime (12, 0, 0, $month, 1, $year);

$firstDayArray = getdate($start);

?>



Listing 12.1 is a fragment of a larger script, so it does not produce any output itself. In the if statement on line 2, we

use checkdate() to test whether the month and year have been provided by a form. If they have not been defined,

checkdate() returns false because the script cannot make a valid date from undefined month and year arguments. This

approach has the added bonus of ensuring that the data submitted by the user constitutes a valid date.



If the date is not valid, we use getdate() on line 3 to create an associative array based on the current time. We then

set values for $month and $year ourselves, using the array's mon and year elements (lines 4 and 5). If the variables

have been set from the form, we put the data into $month and $year variables so as not to touch the values in the

original $_POST superglobal.



Now that we are sure that we have valid data in $month and $year, we can use mktime() to create a timestamp for

the first day of the month (line 10). We will need information about this timestamp later on, so on line 11 we create a

variable called $firstDayArray that will store an associative array returned by getdate() and based on this timestamp.



Building the HTML Form



This document is created with the unregistered version of CHM2PDF Pilot



[ Team LiB ]



This document is created with the unregistered version of CHM2PDF Pilot



[ Team LiB ]



This document is created with the unregistered version of CHM2PDF Pilot



Creating a Calendar Library

Because dates are ubiquitous in Web interfaces, and because working with dates is often comparatively complicated,

let's look at creating a class library to automate some of the work that dates can present. Along the way we will

revisit some of the techniques we have already covered.



If the concept of classes is completely foreign to you, you can supplement your knowledge

by reading through Chapter 14 of the PHP Manual. It is titled "Classes and Objects," and

you can find it at http://www.php.net/manual/en/language.oop.php.



The simple date_pulldown library we will look at was born during the creation of a freelance job listing site. The

project necessarily involved the presentation of multiple date pull-downs allowing employers to select both the start

and end of contract periods, and for candidates to indicate periods of availability. A date pull-down, in this instance,

consists of three separate select elements, one for day of the month, one for month, and one for year.



When a user submits a page, the script will check his input. If there is a problem, we will need to represent the page

with the user's input still in place. This is very easy to accomplish with text boxes but is more of a chore with

pull-down menus. Pages that display information pulled from a database present a similar problem. Data can be

entered straight into the value attributes of text type input elements. Dates will need to be split into month, day, and

year values, and then the correct option elements selected.



The date_pulldown class aims to make date pull-downs sticky (so they will remember settings from page to page)

and easy to set. In order to create our class, we first need to declare it and create a constructor.



A constructor is a function that exists within a class, and which is automatically called when

a new instance of the class is created.



We can also declare some class properties. The following snippet shows the beginning of the class:

1: class date_pulldown

2:

var $name;

3:

var $timestamp = -1;

4:

var $months = array("Jan", "Feb", "Mar", "Apr", "May", "Jun",

5:

"Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

6:

var $yearstart = -1;

7:

var $yearend = -1;

8:

9: function date_pulldown($name) {

10:

$this->name = $name;

11:

}



We first declare the $name property. This will be used to name the HTML select elements. The $timestamp property

will hold a Unix timestamp. The $months array property contains the strings we will display in our month pull-down.

The $yearstart and $yearend properties are both set to –1 pending initialization. They will eventually hold the first and

last years of the range that will be presented in the year pull-down.



This document is created with the unregistered version of CHM2PDF Pilot



[ Team LiB ]



This document is created with the unregistered version of CHM2PDF Pilot



[ Team LiB ]



Summary

In this hour, you pulled together the PHP date-related functions you learned about in the previous hour to work

within a calendar application. You learned how to test the validity of an input date using checkdate(). You worked

through an example script, which applies some of the tools you have looked at, and built a class library that

automates some of the more tedious aspects of working with dates in forms.

[ Team LiB ]



This document is created with the unregistered version of CHM2PDF Pilot



[ Team LiB ]



Q&A



Q1:

Are there any functions for converting between

different calendars?

A1:

Yes. PHP provides an entire suite of functions that

cover alternative calendars. You can read about these

in the official PHP Manual at

http://www.php.net/manual/en/ref.calendar.php.



[ Team LiB ]



This document is created with the unregistered version of CHM2PDF Pilot



[ Team LiB ]



Workshop

The workshop is designed to help you anticipate possible questions, review what you've learned, and begin learning

how to put your knowledge into practice.



Quiz



1:

What PHP function did we use to check the validity of

a date?

A1:

checkdate()

2:

What PHP function did we use to create a timestamp?

A2:

mktime()

3:

What PHP function did we use to create an

associative array of date-related information?

A3:

getdate()



Activity

Use your fancy new date pull-down class in the context of your own form. Create a back-end script that takes the

selected dates and displays their input.

[ Team LiB ]



This document is created with the unregistered version of CHM2PDF Pilot



[ Team LiB ]



This document is created with the unregistered version of CHM2PDF Pilot



Hour 13. Working with Strings

The World Wide Web is very much a plain text environment. No matter how rich Web content becomes, HTML lies

behind it all. It is no accident, then, that PHP provides many functions with which you can format, investigate, and

manipulate strings. As you might expect, MySQL also comes with its own set of string-related functions, which you

will also learn about in this hour.



In this hour, you will learn



How to format strings



How to determine the length of a string



How to find a substring within a string



How to break a string down into component parts



How to remove whitespace from the beginning or end of a string



How to replace substrings



How to change the case of a string



How to use MySQL to put strings together or extract pieces of strings



How to use MySQL to create variations of original strings



How to use MySQL to find alternate representations of strings, in different bases

[ Team LiB ]



Xem Thêm
Tải bản đầy đủ (.pdf) (561 trang)

×