On Thursday, June 20, 2002, at 09:16 PM, Justin French wrote:
> When users have to enter dates on my forms, I always provide them with
> three
> drop-down menus, for day (1-31), Month (1-12) and year (usually current
> year, the next and the next, depending on the application).
>
> Then I have three values on the next page ($_POST['day'],
> $_POST['month'],
> $_POST['year']) which I can combine into a date format I like, or even
> into
> a Unix timestamp.
Maybe this will help you -- it's a function that does exactly this. Of
course, the mail client will probably mess up the line endings, but if
you clean it up, this function will do the following things:
1. create a dropdown listbox for months, days, and years
2. allow you to specify which years should appear (by using a "start
year" and a "total year"
3. you provide the name of each form input, so you can use this function
multiple times on a page (using unique names each time)
4. you provide a default value for when the page loads so that you can
make it "default" to a certain date
5. you decide whether or not to have a dummy "Please choose a date" or
"any" choice on the listboxes (whose value will be an empty string)
6. you decide whether or not this dummy choice will persist if the
default values are not empty.
It is a big, ugly subroutine, but I use it in so many places in my
application that it has allowed me to remove hundreds of lines of
"custom" date listboxes.
Here is the usage, since it may be somewhat confusing (copy this into a
PHP script and see what effect it has):
$current_year = date('Y', mktime()); // gets the current year
$listbox_str = date_listbox_generator( 'month',
'day',
'year',
$_POST['month'],
$_POST['day'],
$_POST['year'],
$current_year,
$current_year + 5,
'any',
true);
print $listbox_str;
This will generate three date listboxes which each start with "any" as a
choice which has no value when the form is submitted (you can test for
an empty string to assume that is the choice). If this form has called
itself and for some reason these listboxes reappear in the new instance
of the page (which might be the case in, say a search engine interface),
then when the page loads, the values chosen on the previous instance of
the page will be the "default" values. The "any" choice will reappear
in this case (so the user can still choose "any") because the last
argument is "true", if the last argument was "false" then this option
would disappear (this is useful for when you want to force the user to
make a choice).
Here is the function, hope it helps:
/**
* date_listbox_generator
*
* generates 3 HTML dropdown lists for selecting a date
*
* @param string $monthInputName name attribute of HTML select tag for
displaying month (e.g., 'month')
* @param string $dayInputName name attribute of HTML select tag for
displaying days (e.g., 'day')
* @param string $yearInputName name attribute of HTML select tag for
displaying years (e.g., 'year')
* @param string $defaultMonth one- or two-digit number representing a
default month (if empty, defaults to current month + day multiplier)
* @param string $defaultDay one- or two-digit number representing a
default day of the month (if empty, defaults to current day + day
multiplier)
* @param string $defaultYear four-digit number representing a default
year (if empty, defaults to current year + day multiplier)
* @param string $startYear four-digit number representing a start year
for the years array
* @param string $totalYears total number of years to contain in the
years array
* @param $pleaseChooseMsg string used if there is to be a "Please
Choose" option, the value of
* this string is the text of the "Please Choose" option
* @param $forcePleaseChoose bool forces the "Please Choose" option to
persist even if there
* is a default value.
* @return string code to display 3 HTML select tag listboxes to accept
user input for a date
*/
function date_listbox_generator($monthInputName, $dayInputName,
$yearInputName, $defaultMonth,
$defaultDay,
$defaultYear, $startYear, $totalYears,
$pleaseChooseMsg = '',
$forcePleaseChoose = false)
{
// set the name of the month input from the function parameter
$month_form = "\n<select name=\"{$monthInputName}\">\n";
// create an array of months
$months = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
// set the counter to 1 b/c we don't want Jan = 0
$i = 1;
$monthSelectedAlready = false;
$monthPleaseChooseMade = false;
// if the client code wants a pleaseChoose, and the default
evaluates to empty,
// then generate a "Please Choose" option tag.
if (!empty($pleaseChooseMsg) && empty($defaultMonth)) {
$month_form .= "\t<option value=\"\" selected=\"yes\">" .
$pleaseChooseMsg . "</option>\n";
$monthSelectedAlready = true;
$monthPleaseChooseMade = true;
}
if ($monthPleaseChooseMade == false && !empty($pleaseChooseMsg)) {
$month_form .= "\t<option value=\"\">" . $pleaseChooseMsg .
"</option>\n";
}
foreach ($months as $monthname) {
// if there's a default month and it's equal to the current
iteration AND
// we haven't already selected a "Please Choose" message,
highlight that one
if (!empty($defaultMonth) && $defaultMonth == $i &&
$monthSelectedAlready == false) {
$month_form .= "\t<option value=\"{$i}\"
selected=\"yes\">{$monthname}</option>\n";
// otherwise it's just a normal option, no highlight
} else {
$month_form .= "\t<option
value=\"{$i}\">{$monthname}</option>\n";
}
$i++;
}
$month_form .= "</select>\n";
// set the name of the day input from the function parameter
$day_form = "\n<select name=\"{$dayInputName}\">\n";
// create an array of days
$days = array();
for ($i = 1; $i < 32; $i++) {
$days[] = $i;
}
$daySelectedAlready = false;
$dayPleaseChooseMade = false;
// if the client code wants a pleaseChoose, and the default
evaluates to empty,
// then generate a "Please Choose" option tag.
if (!empty($pleaseChooseMsg) && empty($defaultDay)) {
$day_form .= "\t<option value=\"\" selected=\"yes\">" .
$pleaseChooseMsg . "</option>\n";
$daySelectedAlready = true;
$dayPleaseChooseMade = true;
}
if ($dayPleaseChooseMade == false && !empty($pleaseChooseMsg)) {
$day_form .= "\t<option value=\"\">" . $pleaseChooseMsg .
"</option>\n";
}
// loop through the array of days, set a default if appropriate
foreach ($days as $dayname) {
if (!empty($defaultDay) && $defaultDay == $dayname &&
$daySelectedAlready == false) { // do I need a way to exclude
'any' from this? ie, add && $defaultDay != 'any' ?
$day_form .= "\t<option value=\"{$dayname}\"
selected=\"yes\">{$dayname}</option>\n";
} else {
$day_form .= "\t<option
value=\"{$dayname}\">{$dayname}</option>\n";
}
}
$day_form .= "</select>\n";
// set the name of the year input from the function parameter
$year_form = "\n<select name=\"{$yearInputName}\">\n";
// create an array of years -- from the function parameter
$years = array();
for ($i = $startYear; $i <= ($startYear + $totalYears); $i++) {
$years[] = $i;
}
$yearSelectedAlready = false;
$yearPleaseChooseMade = false;
// if the client code wants a pleaseChoose, and the default
evaluates to empty,
// then generate a "Please Choose" option tag.
if (!empty($pleaseChooseMsg) && empty($defaultYear)) {
$year_form .= "\t<option value=\"\" selected=\"yes\">" .
$pleaseChooseMsg . "</option>\n";
$yearSelectedAlready = true;
$yearPleaseChooseMade = true;
}
if ($yearPleaseChooseMade == false && !empty($pleaseChooseMsg)) {
$year_form .= "\t<option value=\"\">" . $pleaseChooseMsg .
"</option>\n";
}
// loop through the array of years, set a default if appropriate
foreach ($years as $yearname) {
if (!empty($defaultYear) && $defaultYear == $yearname) {
$year_form .= "\t<option value=\"{$yearname}\"
selected=\"yes\">{$yearname}</option>\n";
} else {
$year_form .= "\t<option
value=\"{$yearname}\">{$yearname}</option>\n";
}
}
$year_form .= "</select>\n";
return $month_form . $day_form . $year_form;
}
----
Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php