I needed to translate bunch of things from my native language, Polish, and had no chance to test the updated version, so if something does not work, please let me know.
<?php
/**
* dropMenu Function
*
* This function generates an HTML dropdown menu with various customization options.
* It is designed for ease of use and flexibility in creating dropdown menus.
*
* @param string $f_name - The name attribute for the dropdown.
* @param mixed $f_value - The selected value for the dropdown.
* @param array $f_array - An associative array of key-value pairs for dropdown options.
* @param string $f_default - Default text for the initial option (optional).
* @param string $f_css - Additional CSS classes for styling (optional).
* @param string $f_tabindex - The tabindex attribute for keyboard navigation (optional).
* @param string $f_js - Additional JavaScript attributes (optional).
*
* @return void Outputs the HTML for the dropdown menu.
*
* You are free to use this function without limits. If you include the name or both
* the name and URL in your code, attribution to the original author is appreciated.
*
* Usage example:
* dropMenu('us_state', '', $states_arr, 'Select State', '', '', '');
*/
function dropMenu($f_name, $f_value, $f_array, $f_default='', $f_css='', $f_tabindex='', $f_js='')
{
echo '<select name="' . $f_name . '" id="' . $f_name . '"';
if ($f_css != '') echo ' class="' . $f_css . '"';
if ($f_js != '') echo ' ' . $f_js;
if ($f_tabindex != '') echo ' tabindex="' . $f_tabindex . '"';
echo ">";
if($f_default != '') echo "n<option value="">" . $f_default . "</option>n";
while(list($key, $val) = each($f_array))
{
$output = "<option value="" . $key . """;
if ($key == $f_value)
{
$output .= " selected";
}
$output .= ">" . $val . "</option>n";
echo $output;
}
echo"</select>";
/* *** dropMenu function ends *** */
}
So, to the point. With this function, you will need an array defined somewhere. Maybe in an included file or somewhere within the page itself. Let's use few of US States' as an example.
<?php
/* *** create an array with the States *** */
$states_arr = array (
'CA' => 'California',
'NV' => 'Nevada',
'TX' => 'Texas'
);
/* *** initialize drop-down function *** */
dropMenu('us_state', '', $states_arr, 'Select State', '', '', '');
/* *** or, since you don't need those extra parameters for now, you can shorten the init code: *** */
dropMenu('us_state', '', $states_arr, 'Select State');
How does it look? Here we go:
HTML output:
<select name="us_state" id="us_state">
<option value="">Select State</option>
<option value="CA">California</option>
<option value="NV">Nevada</option>
<option value="TX">Texas</option>
</select>
So this the base, something you call when an User fills the form for the first time.
Now, most likely you will also need the same User to update his/her information. While at it: sooner or later this will come in handy to already print out the initial selection with the variable taken out from the array.
The second option in the function will help you out here.
All you need to do is to define the initial state.
<?php
dropMenu('us_state', 'CA', $states_arr, 'Select State', '', '', '');
HTML output:
<select name="us_state" id="us_state">
<option value="">Select State</option>
<option value="CA" selected>California</option>
<option value="NV">Nevada</option>
<option value="TX">Texas</option>
</select>
Or Nevada:
<?php
dropMenu('us_state', 'NV', $states_arr, 'Select Nevada State', '', '', '');
HTML output:
<select name="us_state" id="us_state">
<option value="">Select Nevada State</option>
<option value="CA">California</option>
<option value="NV" selected>Nevada</option>
<option value="TX">Texas</option>
</select>
The second value in the funtion should take its value dynamically, meaning a simple IF condition will do the job just fine:
IF
value is defined
$my_value = defined value
ELSE
$my_value = '';
<?php
dropMenu('us_state', $my_value, $states_arr, 'Select State', '', '', '');
...and you're all set.
By default the additional parameters are empty, so all you need to do is to fill the proper variable with your own input.
<?php
dropMenu('us_state', 'CA', $states_arr, 'Select State', 'free_db_class', '', '');
If you want your form's elements to be focused in particular order, you can add a value to the next-to-last parameter of the dropdown function.
<?php
dropMenu('us_state', 'TX', $states_arr, '-Select State-', '', '3', '');
<?php
dropMenu('us_states', '', $states_arr_up, ' - Drop with Jump option - ', '', '', 'onchange="document.location.href=this.value"');
<?php
dropMenu('us_states', '', $states_arr_up, 'Drop with all parameters enabled', 'free_db_class', '2', 'onchange="document.location.href=this.value"');
Summary: Font-end and Back-end developer, also designer with 25+ years of experience. Currently based in Poland. Eligible to work in the US (US Social Security Number holder) for any employer on W2 / 1099 basis. Utilizes both technical skills and designing aptitude. Lived and worked in Europe, Australia and North America.
PHP CMS HTML5 CSS3 RWD OOP MySQL PDO JS jQuery JSON GIT Bitbucket GitHub Gulp
I've implemented language version feature, ready for additional languages, based on URL modification. Doesn't rely on cookies or sessions and is available via a /{lang} modifier. More about languages
Zaimplementowałem wersję językową, gotową na dodanie kolejnych języków, opartą na modyfikacji URL, która nie korzysta z plików cookie ani sesji. Wersja językowa dostępna jest przez modyfikator /{język}. Więcej o językach