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.
Similar Posts:
- jQuery Image Gallery with Transitions and Navigation
- Fade In and Out Images from a Single Directory Using jQuery (with plugin)
- Display Last.fm’s Recent Tracks on Web Site with PHP Function
- UPDATED: Strip Off Characters from String Using PHP (substr)
- MySQL Return Single Data or Single Array with PHP
No related posts.

No comments yet.