Skip to content

Instantly share code, notes, and snippets.

@niczak
Created February 24, 2011 17:56
Show Gist options
  • Save niczak/842553 to your computer and use it in GitHub Desktop.
Save niczak/842553 to your computer and use it in GitHub Desktop.
A function that will generate and return an HTML select element.
<?php
/*
Here is an example function call, array abbreviated for the example.
$afields = array("Jan"=>"January", "Feb"=>"February", "Mar"=>"March);
echo fnDisplay_DropDown($afields, "Months", "Mar", "required");
Results in:
<select name="Months" class="required">
<option value="Jan">January</option>
<option value="Feb">February</option>
<option value="Mar" selected="selected">March</option>
</select>
*/
function fnDisplay_DropDown($aFields, $sName, $sSel, $sClass=NULL, $sID=NULL)
{
if(empty($aFields) || empty($sName) || empty($sSel))
fnErr("Parameter missing in call to fnDisplay_DropDown()", sSCRIPT_NAME);
if(!(is_array($aFields)))
fnErr("Array passed to fnDisplay_DropDown() is in invalid format.", sSCRIPT_NAME);
$sHTML = sprintf(" <select name=\"%s\"%s%s>\n", $sName,
!empty($sClass) ? " class=\"$sClass\"" : $sClass,
!empty($sID) ? " id=\"$sID\"" : $sID);
foreach($aFields as $sVar=>$sVal)
{
$sSelect = NULL;
if($sVar === $sSel)
$sSelect = " selected=\"selected\"";
$sHTML .= sprintf(" <option value=\"%s\"%s>%s</option>\n",
$sVar, $sSelect, $sVal);
}
$sHTML .= sprintf(" </select>\n");
return $sHTML;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment