Home  >  Article  >  Backend Development  >  PHP function to automatically output the select form

PHP function to automatically output the select form

WBOY
WBOYOriginal
2016-07-25 08:58:031407browse
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.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn