Skip to content

Instantly share code, notes, and snippets.

@anselmobattisti
Created May 21, 2009 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anselmobattisti/115792 to your computer and use it in GitHub Desktop.
Save anselmobattisti/115792 to your computer and use it in GitHub Desktop.
<?
/*
* selectArray
* @auth Anselmo Battisti
* @mail anselmobattisti@gmail.com
*
* @version 1.0
*
* @example
require_once "selectArray.php";
$fruit[0] = 'Lemon';
$fruit[1] = 'Banana';
$fruit[2] = 'Orange';
echo selectArray::getHtmlSelect($fuit,'fruit',$_POST['fuit']);
*/
class selectArray
{
/**
* getHtmlSelect
*
* @abstract Gera um SELECT html em função de um vetor, a chave do vetor é o value e o option
*/
static function getHtmlSelect($array, $name, $selected, $null = false)
{
if(!is_array($array)){
$html = "Erro ao gerar o select";
} else {
$html = '<select name="'.$name.'" id="'.$name.'" size="1">';
if($null) {
$html .= "<option value=''>-----</value>";
}
foreach($array as $k=>$v){
unset($s);
if($k == $selected) $s = "selected = 'true'";
$html .= '<option value="'.$k.'" '.$s.'>'.$v.'</option>';
}
$html .= '</select>';
}
return $html;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment