Mar
22
Posted by john in php, programing
Just another quick way to automate the simple process of creating HTML selects with a function. This small function with also remember which value was selected by checking it against the $_POST value and adding a selected="SELECTED" to that option.
2 dimentional array:
$people_array = array( '1' => 'Bob', '2' => 'James', '3' => 'Jessi', '4' => 'Sam');
Just call this function either through a request(…) or just simply adding it to the file’s code:
function array2select($array, $post_field){
foreach($array as $key => $value){
if($post_field == $key){$selected = ' selected="SELECTED"';}
echo ''.$value.'';
$selected = false;
}
}
Then just add it where you want the select to do:
< select name="people" > echo array2select($people_array,$people); < /select >
This is pretty basic, but it should give you an idea on how arrays and functions can be used together to help speed up projects and automate these basic tasks.






