note 84278 added to function.array
| From: | jonberglund at gmail dot com | Date: | Mon, 07 Jul 2008 16:25:51 +0000 |
| Subject: | note 84278 added to function.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-141243@lists.php.net to get a copy of this message | ||
The following function (similar to one above) will render an array as a series of HTML select
options (i.e. "<option>...</option>"). The problem with the one before is that
there was no way to handle <optgroup>, so this function solves that issue.
function arrayToSelect($option, $selected = '', $optgroup = NULL)
{
$returnStatement = '';
if ($selected == '') {
$returnStatement .= '<option value="" selected="selected">Select
one...</option>';
}
if (isset($optgroup)) {
foreach ($optgroup as $optgroupKey => $optgroupValue) {
$returnStatement .= '<optgroup label="' . $optgroupValue .
'">';
foreach ($option[$optgroupKey] as $optionKey => $optionValue) {
if ($optionKey == $selected) {
$returnStatement .= '<option selected="selected" value="' .
$optionKey . '">' . $optionValue . '</option>';
} else {
$returnStatement .= '<option value="' . $optionKey . '">' .
$optionValue . '</option>';
}
}
$returnStatement .= '</optgroup>';
}
} else {
foreach ($option as $key => $value) {
if ($key == $selected) {
$returnStatement .= '<option selected="selected" value="' . $key .
'">' . $value . '</option>';
} else {
$returnStatement .= '<option value="' . $key . '">' . $value
. '</option>';
}
}
}
return $returnStatement;
}
So, for example, I needed to render a list of states/provinces for various countries in a select
field, and I wanted to use each country name as an <optgroup> label. So, with this function,
if only a single array is passed to the function (i.e. "arrayToSelect($stateList)") then
it will simply spit out a bunch of "<option>" elements. On the other hand, if two
arrays are passed to it, the second array becomes a "key" for translating the first array.
Here's a further example:
$countryList = array(
'CA' => 'Canada',
'US' => 'United States');
$stateList['CA'] = array(
'AB' => 'Alberta',
'BC' => 'British Columbia',
'AB' => 'Alberta',
'BC' => 'British Columbia',
'MB' => 'Manitoba',
'NB' => 'New Brunswick',
'NL' => 'Newfoundland/Labrador',
'NS' => 'Nova Scotia',
'NT' => 'Northwest Territories',
'NU' => 'Nunavut',
'ON' => 'Ontario',
'PE' => 'Prince Edward Island',
'QC' => 'Quebec',
'SK' => 'Saskatchewan',
'YT' => 'Yukon');
$stateList['US'] = array(
'AL' => 'Alabama',
'AK' => 'Alaska',
'AZ' => 'Arizona',
'AR' => 'Arkansas',
'CA' => 'California',
'CO' => 'Colorado',
'CT' => 'Connecticut',
'DE' => 'Delaware',
'DC' => 'District of Columbia',
'FL' => 'Florida',
'GA' => 'Georgia',
'HI' => 'Hawaii',
'ID' => 'Idaho',
'IL' => 'Illinois',
'IN' => 'Indiana',
'IA' => 'Iowa',
'KS' => 'Kansas',
'KY' => 'Kentucky',
'LA' => 'Louisiana',
'ME' => 'Maine',
'MD' => 'Maryland',
'MA' => 'Massachusetts',
'MI' => 'Michigan',
'MN' => 'Minnesota',
'MS' => 'Mississippi',
'MO' => 'Missouri',
'MT' => 'Montana',
'NE' => 'Nebraska',
'NV' => 'Nevada',
'NH' => 'New Hampshire',
'NJ' => 'New Jersey',
'NM' => 'New Mexico',
'NY' => 'New York',
'NC' => 'North Carolina',
'ND' => 'North Dakota',
'OH' => 'Ohio',
'OK' => 'Oklahoma',
'OR' => 'Oregon',
'PA' => 'Pennsylvania',
'RI' => 'Rhode Island',
'SC' => 'South Carolina',
'SD' => 'South Dakota',
'TN' => 'Tennessee',
'TX' => 'Texas',
'UT' => 'Utah',
'VT' => 'Vermont',
'VA' => 'Virginia',
'WA' => 'Washington',
'WV' => 'West Virginia',
'WI' => 'Wisconsin',
'WY' => 'Wyoming');
...
<select name="state" id="state"><?php echo
arrayToSelect($stateList,'',$countryList) ?></select>
<select name="country" id="country"><?php echo
arrayToSelect($countryList,'US') ?></select>
----
Server IP: 64.71.164.2
Probable Submitter: 140.239.17.68
----
Manual Page -- http://www.php.net/manual/en/function.array.php
Edit -- https://master.php.net/note/edit/84278
Del: integrated -- https://master.php.net/note/delete/84278/integrated
Del: useless -- https://master.php.net/note/delete/84278/useless
Del: bad code -- https://master.php.net/note/delete/84278/bad+code
Del: spam -- https://master.php.net/note/delete/84278/spam
Del: non-english -- https://master.php.net/note/delete/84278/non-english
Del: in docs -- https://master.php.net/note/delete/84278/in+docs
Del: other reasons-- https://master.php.net/note/delete/84278
Reject -- https://master.php.net/note/reject/84278
Search -- https://master.php.net/manage/user-notes.php