Skip to content

Instantly share code, notes, and snippets.

@isotrope
Created January 5, 2015 16:30
Show Gist options
  • Save isotrope/56f48a49657b01525514 to your computer and use it in GitHub Desktop.
Save isotrope/56f48a49657b01525514 to your computer and use it in GitHub Desktop.
For Matthew, a simple way to build options / dropdown menus
<?php
/**
*
* Easily handle a bunch of <option>s
*
*/
$my_options = array(
// name/label => value
'item 1' => 850,
'item 2' => 950,
'item 3' => 750,
'item 4' => 650,
'item 5' => 850, // Gonna run into a problem because this is the second 850
'item 6' => 450,
);
// Need more options? Simply add a pair to the array above. :)
// Start up our <select> HTML
$html_out = '<select name="price" id="price">';
foreach ( $my_options as $option_label => $option_value ) {
//Clean up the POST value (based on Andrew's code)
$post_value = filter_input(INPUT_POST, 'price', FILTER_SANITIZE_NUMBER_INT);
//Add/build the option
$html_out .= '<option value="' . $option_value .'" ' . selected($post_value, $option_value) .'>' . $option_label .'</option>';
}
// Close up our <select> HTML
$html_out .= '</select>';
echo $html_out; // or return $html_out or...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment