Create a dynamic select menu with PHP

In many HTML forms or content management systems the form element “SELECT” is used frequently. If you use your PHP code inside the standard HTML element the code will be nearly unreadable. Adding extra options is not really fun. This small handy function will produce a select menu based on an associative array. Just define an array and call the function, that’s all you need to do.

function dynamic_select($the_array, $element_name, $label = '', $init_value = '') {
    $menu = '';
    if ($label != '') $menu .= '
    	<label for="'.$element_name.'">'.$label.'</label>';
    $menu .= '
    	<select name="'.$element_name.'" id="'.$element_name.'">';
    if (empty($_REQUEST[$element_name])) {
        $curr_val = $init_value;
    } else {
        $curr_val = $_REQUEST[$element_name];
    }
    foreach ($the_array as $key => $value) {
        $menu .= '
			<option value="'.$key.'"';
        if ($key == $curr_val) $menu .= ' selected="selected"';
        $menu .= '>'.$value.'</option>';
    }
    $menu .= '
    	</select>';
    return $menu;
}

How-to use the function

Create and array with all values for the select element. The array key is used as the option value and the array value as the label. Add on the first position an empty value if you need such an element as a default (first) value.

$test_array = array(''=>'...', 'var_1'=>'first label', 'some_var'=>'it\'s hot', 'last_constant'=>'last element');

The properties for the function:

The array we created above, the name of the select menu, some text for the label element (keep this one empty = disable) and the initial (key) value (this might be empty).

echo create_select($test_array, 'test_menu', 'my label', 'some_var');

Calling the function will create this HTML code:

<label for="test_menu">my label</label>
<select name="test_menu">
 <option value="">...</option>
 <option value="var_1">first label</option>
 <option value="some_var" selected="selected">it's hot</option>
 <option value="last_constant">last element</option>
</select>

Select menu from month names

This is an example with month notations. You can translate the names for local using. Based on the current date the actual month is selected (by default).

<?php
$curr_month = date("m");
$month = array (1=>"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
echo dynamic_select($month, 'month', 'Select a month', $curr_month);

The output (HTML) looks like:

<label for="month">Select a month</label>
<select name="month">
 <option value="1">January</option>
 <option value="2">February</option>
 <option value="3">March</option>
 <option value="4">April</option>
 <option value="5">May</option>
 <option value="7">July</option>
 <option value="8">August</option>
 <option value="9">September</option>
 <option value="10">October</option>
 <option value="11">November</option>
 <option value="12" selected="selected">December</option>
</select>

Leave a Reply

Your email address will not be published. Required fields are marked *