Skip to content

Instantly share code, notes, and snippets.

@calexandrepcjr
Created July 26, 2017 18:23
Show Gist options
  • Save calexandrepcjr/fd7ab544df4950af78ccac604c0f3a58 to your computer and use it in GitHub Desktop.
Save calexandrepcjr/fd7ab544df4950af78ccac604c0f3a58 to your computer and use it in GitHub Desktop.
A helper to handle with some HTML tags
<?php
/**
* Builds HTML dropdowns in a simple way
* @param array $array
* @param bool $hasAll
*/
function mountDropdownHtml($array, $hasAll = false)
{
if (!is_array($array)) {
throw new Exception('Invalid argument: First argument must be an array');
}
$indexes = array_keys($array);
$dropdown = "<select name='{$indexes[0]}'>";
$all = ' selected ';
$selected = '';
foreach ($array[$indexes[0]] as $index => $value) {
if ($value == ${$indexes[0]}) {
$selected = ' selected ';
$all = '';
}
$dropdown .= "<option value='{$index}' {$selected}>{$value}</option>";
}
if ($hasAll) {
$dropdown .= "<option value='0' $all >All</option>";
}
$dropdown .= "</select>";
return $dropdown;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment