NYCPHP Meetup

NYPHP.org

[nycphp-talk] Remembering which option in a SELECT the user picked.

Webapprentice webapprentice at onemain.com
Wed Jan 29 20:12:22 EST 2003


Based on recent feedback, I created this snippet of code to generate the 
SELECT dropdown menu.

// Dates clubhouse is available
$clubhouseDates = array(
    'Friday August 22, 04:00 PM - 06:00 PM' => 'Friday August 22, 04:00 
PM - 06:00 PM',
    'Friday August 22, 06:00 PM - 08:00 PM' => 'Friday August 22, 06:00 
PM - 08:00 PM',
    'Friday August 22, 08:00 PM - 10:00 PM' => 'Friday August 22, 08:00 
PM - 10:00 PM',
    'Friday August 22, 10:00 PM - 12:00 AM' => 'Friday August 22, 10:00 
PM - 12:00 AM',
    'Saturday August 23, 12:00 AM - 02:00 AM' => 'Saturday August 23, 
12:00 AM - 02:00 AM',
    'Saturday August 23, 12:00 PM - 02:00 PM' => 'Saturday August 23, 
12:00 PM - 02:00 PM',
    'Saturday August 23, 02:00 PM - 04:00 PM' => 'Saturday August 23, 
02:00 PM - 04:00 PM',
    'Saturday August 23, 04:00 PM - 06:00 PM' => 'Saturday August 23, 
04:00 PM - 06:00 PM',
    'Saturday August 23, 06:00 PM - 08:00 PM' => 'Saturday August 23, 
06:00 PM - 08:00 PM',
    'Saturday August 23, 08:00 PM - 10:00 PM' => 'Saturday August 23, 
08:00 PM - 10:00 PM',
    'Saturday August 23, 10:00 PM - 12:00 AM' => 'Saturday August 23, 
10:00 PM - 12:00 AM',
    'Sunday August 24, 12:00 AM - 02:00 AM' => 'Sunday August 24, 12:00 
AM - 02:00 AM',
    'Sunday August 24, 12:00 PM - 02:00 PM' => 'Sunday August 24, 12:00 
PM - 02:00 PM',
    'Sunday August 24, 02:00 PM - 04:00 PM' => 'Sunday August 24, 02:00 
PM - 04:00 PM'
);

/* Generate the SELECT tag with options.
 * $selectName: Name of the SELECT tag
 * $optionValues: Array containing all the values
 * $selectdValue: Which option gets the SELECTED attribute (pass this 
function the value 
 *                of the SELECT tag
 */
function generateHtmlSelectTag($selectName, $optionValues, $selectedValue) {
    $str = "<select name=\\"$selectName\\">\
";
    foreach ($optionValues as $key => $value) {
        if ($selectedValue == $value) {
            $str .="<option value=\\"$value\\" selected>$key</option>\
";
        } else {
            $str .= "<option value=\\"$value\\">$key</option>\
";
        }    // End if-else
    }    // End foreach
    $str .= "</select>\
";
    return $str;
}    // End function


// This is called in the PHP file elsewhere to generate the SELECT tag 
with options

<?php $displaySelectTag = generateHtmlSelectTag("requestTime", 
$clubhouseDates, 0);
echo $displaySelectTag; ?>


But every OPTION tag has the selected attribute...
It appears this condition turns out true.
if ($selectedValue == $value) {

Printing out the value gives me 0 (integer) and Friday August 22, 04:00 
PM - 06:00 PM (string), but how can the equality be true?

What do I not understand?

Thanks.

--Stephen




More information about the talk mailing list