Created
July 26, 2017 18:23
-
-
Save calexandrepcjr/fd7ab544df4950af78ccac604c0f3a58 to your computer and use it in GitHub Desktop.
A helper to handle with some HTML tags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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