Snippets

Add Blank Option to Dropdown

So, you have an array of data prepared for the form_dropdown() helper, but you want to add a blank option at the top.
Usage is $data = add_blank_option($array, ‘Choose’);

function add_blank_option($options, $blank_option = '') 
{
    if (is_array($options) && is_string($blank_option))
    {
       if (empty($blank_option))
       {
          $blank_option = array( NULL => '--');
       }
       else
       {
          $blank_option = array( NULL => $blank_option);
       }
       $options = $blank_option + $options;
       return $options;
    }
    else
    {
       show_error("Wrong options array passed");
    }
}

Source: http://ci-snippets.tumblr.com/post/2595681599/add-blank-opti...

Share this snippet

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Please respect the following rules: No advertising, no spam, no keyword in name field. Thank you!