|
|
|
|
|
|
| |
In a lot of dynamic forms or content management sytems 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 based on an associative array. Just define an array and call the function, that's all.
| <?php
// build here the array with values for the select,
// notice that the array key is used option value and the array value as the label.
$test_array = array("var_1"=>"first label", "some_var"=>"it's hot", "last_constant"=>"last element");
// the properties of this function the array above, the name of the select menu and the initial value.
// If the initial value is empty (by default the an empty option is added otherwise.
// Use only a valid key from the above array as initial value and the selected state is used for this value.
function dynamic_select($the_array, $element_name, $init_value = "") {
$menu = "<select name=\"".$element_name."\">\n";
if (!isset($_REQUEST[$element_name])) {
if ($init_value == "") {
$menu .= " <option value=\"\">...\n";
} else {
$curr_val = $init_value;
}
} else {
$curr_val = $_REQUEST[$element_name];
}
foreach ($the_array as $key => $value) {
$menu .= " <option value=\"".$key."\"";
$menu .= ($key == $curr_val) ? " selected" : "";
$menu .= ">".$value."\n";
}
$menu .= "</select>\n";
return $menu;
}
/* Example:
echo create_select($test_array, "test_menu", "some_var");
will output this:
<select name="test_menu">
<option value="var_1">first label
<option value="some_var" selected>it's hot
<option value="last_constant">last element
</select>
*/
?> | | |
|
| Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | How to pass an array from one PHP Script to another via an HTML form Categories : PHP, HTML and PHP, Arrays | | | dynamic table columns Categories : PHP, HTML and PHP, Arrays, Databases, MySQL | | | Print out array key => value in colored HTML Categories : PHP, Arrays, HTML and PHP | | | Form Submission Using Array's Categories : PHP, HTML and PHP, Beginner Guides, Arrays | | | Parsing html tags with php. Get an array from this function Categories : PHP, HTML and PHP, Arrays, Tag Extractors | | | CSS style switcher Categories : PHP, CSS, HTML and PHP, Arrays, Sessions | | | navbar.php3 - Dynamic hyperlinked navigation bars Categories : HTML and PHP, Arrays, PHP, Complete Programs | | | PHP3: Formmail. Just a cgi formmail, but than in PHP. It is easy to use! Categories : HTML and PHP, Email, PHP, Perl, HTML and PHP | | | Simple script to passing persistent and growing array between recalls of one page (manipulate little stack). Categories : Arrays, Global Variables, PHP, HTML and PHP, Variables | | | Select with current month Categories : PHP, HTML and PHP, Date Time, Arrays | | | PHP Script to find url links in a page Categories : PHP, URLs, Regexps, Arrays | | | a function that builds an HTML select list from any mysql table. Categories : PHP, MySQL, HTML and PHP | | | Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | | | Check parameters validity. Paranoia was designed to check the validity of the parameters that a php page will receive after a form submission. It can be used to check the variables sent by POST or GET Categories : Algorithms, HTML and PHP, PHP, Variables | |
|
|
|