| [ Team LiB ] |
|
26.4 Generating HTML with PHPAn HTML select tag allows you to list several options that appear as a pull-down menu. I am often in the situation of creating the contents of the list on the fly. Sometimes the contents are pulled from a database, such as for choosing from among users in a Web application. Other times the contents are generated, such as choosing month, day, and year. There are two aspects to this problem. First, there is the fairly simple problem of creating all the values for the option tags. This is best accomplished in a loop. The second issue deals with preselecting one of the options. Regardless of the source of the contents, database or otherwise, the technique is similar. To illustrate, I'll develop a function for generating three select fields for getting a date from the user: month, day, and year. To generate a list of the months, it is best to draw from an array to display their names. Days and years are numbers, so their values and displayed names are the same. Listing 26.11 demonstrates. Listing 26.11 Date selector
<?php
/*
** Get three selectors for month, day, year
*/
function getDateSelectors($name, $date=NULL)
{
static $monthName = array(1=>"January",
"February", "March", "April", "May",
"June", "July", "August", "September",
"October", "November", "December");
if($date === NULL)
{
$date = time();
}
//make Month selector
$givenMonth = date("m", $date);
$fields = "<select name=\"{$name}[month]\">\n";
for($m = 1; $m <= 12; $m++)
{
$fields .= "<option value=\"$m\"";
if($m == $givenMonth)
{
$fields .= " selected";
}
$fields .= ">" . $monthName[$m] . "</option>\n";
}
$fields .= "</select>\n";
$fields .= "<select name=\"{$name}[day]\">\n";
$givenDay = date("d", $date);
for($d=1; $d <= 31; $d++)
{
$fields .= "<option value=\"$d\"";
if($d == $givenDay)
{
$fields .= " selected";
}
$fields .= ">$d</option>\n";
}
$fields .= "</select>\n";
$fields .= "<select name=\"{$name}[year]\">\n";
$givenYear = date("Y", $date);
$lastYear = date('Y')+5;
for($y = date('Y')-5; $y <= $lastYear; $y++)
{
$fields .= "<option value=\"$y\"";
if($y == $givenYear)
{
$fields .= " selected";
}
$fields .= ">$y</option>\n";
}
$fields .= "</select>\n";
return($fields);
}
//start document
print("<html>\n" .
"<head>\n" .
"<title>Listing 26-11</title>\n" .
"</head>\n");
//start body
print("<body>\n");
//choose default date
if(isset($_REQUEST['sample']))
{
//construct time
$UseDate = mktime(0, 0, 0,
$_REQUEST['sample']['month'],
$_REQUEST['sample']['day'],
$_REQUEST['sample']['year']);
}
else
{
//use default
$UseDate = NULL;
}
//make simple form
print("<form action=\"{$_SERVER['PHP_SELF']}\">\n");
print(getDateSelectors("sample", $UseDate));
print("<input type=\"submit\">\n");
print("</form>\n");
//close HTML document
print("</body>\n" .
"</html>\n");
?>
The options for each selector are generated in a for loop. Months range from 1 to 12, days from 1 to 31. For years, I've chosen to present an 11-year range around the current year. Notice that if you submit a date, it refreshes the page and sets the form with the date you chose. The key is the addition of the if statement. Each time through the loop, the current value is tested against the one to be selected. Note how the three selectors pass their values as part of an array. PHP understands to create array elements from form fields named with square brackets. If you duplicate this technique, do not include quotes around the associative key. That is, use {$name}[month] instead of {$name}['month']. When parsing form fields, PHP does not expect string delimiters around the key. |
| [ Team LiB ] |
|