1.单选框控件
/** * @param $arr 数组 * @param string $name 控件name * @param string $checked 默认选中的值 * @param string $ext 其它信息如 id class style 等 * @return string */ function makeHtmlRadio($arr, $name = '', $checked = '', $ext = '') { $html = ''; $i = 0; foreach ($arr as $key => $val) { $id = $name . '_' . $i; if ($checked == $val) { $check = ' checked '; } else { $check = ''; } $html .= "<input {$ext} {$check} type='radio' name=\"$name\" id='{$id}' value='{$key}' /><label for='{$id}'>$val</label>\r\n"; ++$i; } return $html; }
2.复选框控件
/** * @param $arr 数组 * @param string $name 控件name * @param array $checked 默认选中的值 * @param string $ext 其它信息如 id class style 等 * @return string */ function makeHtmlCheckbox($arr, $name = '', $checked = [], $ext = '') { $html = ''; $i = 0; foreach ($arr as $key => $value) { $id = $name . '_' . $i; //默认选中 if (in_array($value, $checked)) { $check = ' checked '; } else { $check = ' '; } $html .= "<input type='checkbox' {$ext} {$check} name='{$name}[]' id='{$id}' value='{$key}' /><label for='{$id}'>{$value}</label>\r\n"; ++$i; } return $html; }
3.下拉选择控件
/** * @param $arr 数组 * @param string $name 控件name * @param string $selected 默认选中的值 * @param string $ext 其它信息如 id class style 等 * @return string */ function makeHtmlSelect($arr, $name = '', $selected = '', $ext = '') { $html = "<select name={$name} {$ext}>"; foreach ($arr as $key => $value) { if ($selected == $value) { $select = ' selected '; } else { $select = ''; } $html .= "<option {$select} value='{$key}'>{$value}</option>\r\n"; } $html .= '</select>'; return $html; }