Home >Backend Development >PHP Tutorial >PHP function to automatically output the select form
This article introduces a function that uses PHP to automatically output (construct) a select form. Friends in need can refer to it.
The following functions are implemented: Automatically generate select options. The code is as follows: <?php /** * 自动生成form中的select表单 * edit by bbs.it-home.org */ function get_select_html($msg_list,$msg_val=""){ $arr_list=$msg_list; if(!is_array($arr_list)) return ""; $str_return=""; if(is_assoc($arr_list)){ foreach($arr_list as $key=>$item){ $str_sel=""; if($key==$msg_val) $str_sel=" selected"; $str_return.="<option value="".$key."" ".$str_sel."="">".$item."</option>"; } }else{ foreach($arr_list as $item){ $str_sel=""; if($item==$msg_val) $str_sel=" selected"; $str_return.="<option value="".$item."" ".$str_sel."="">".$item."</option>"; } } return $str_return; } ?> Code description: Returns a select option item, $msg_list is an array ($key=>$value), $msg_val is the value that needs to be selected by default. The function can be written as a static method in a PHP static class, which is more convenient to use. |