Welcome Guest, Not a member yet? Register   Sign In
form_dropdown replacement helper
#1

[eluser]GSV Sleeper Service[/eluser]
it annoyed me greatly that the form_dropdown helper would not accept an array of attributes, like form_input does, so I modified it.
stick this is application/helpers/MY_form_helper.php
Code:
function form_dropdown($data = '', $options = array(), $selected = array(), $extra = ''){

    if ( ! is_array($selected)){
        $selected = array($selected);
    }

    if ($extra != '') $extra = ' '.$extra;

    $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';


    $form = "<select ".parse_form_attributes($data,array()).$extra.$multiple.">\n";

    foreach ($options as $key => $val){
        $key = (string) $key;
        $val = (string) $val;

        $sel = (in_array($key, $selected))?' selected="selected"':'';

        $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
    }

    $form .= '</select>';

    return $form;
}
#2

[eluser]jleequeen[/eluser]
It is also a bit of an annoyance that you have to have a key/value pair array. If I just have a simple hardcoded array that I want to use for a list, you are forced to make an array where the key and value are the same. It just seems inefficient to show the value when it is not needed for a simple value only array that doesn't need a key.

Code:
<option value="something">something</option>

with a hardcoded array as:

Code:
$somethings = array('something' => 'something', 'somethingelse' => 'somethingelse');

Just looks weird when all you want to do is take a simple list with only a val that doesn't need a key and spit it out in a dropdown. Has anyone modified the form_dropdown helper to do such a thing?




Theme © iAndrew 2016 - Forum software by © MyBB