Welcome Guest, Not a member yet? Register   Sign In
Form Helper - Select Lists/Menu's
#1

[eluser]Phil Sturgeon[/eluser]
Well we have form_dropdown but nothing for making lists? *unhappyface*

Try:

Code:
function form_list($name = '', $options = array(), $selected = array(), $extra = '')
{

    if ($extra != '') $extra = ' '.$extra;
    
    // converts to an array if it is not
    if(!is_array($selected)) $selected = array($selected);
    
    // If not set, will default
    if(!strpos('size=', '#'.$extra)) $extra .= ' size="5"';
    
    $form = '<select name="'.$name.'"'.$extra.">\n";
    
    foreach ($options as $key => $val)
    {
        $sel = (!in_array($key, $selected)) ? '' : ' selected="selected"';
        
        $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
    }

    $form .= '</select>';
    
    return $form;
}


Useage:

Code:
form_list('supervisorID[]', array(1 => 'Foo', 2 => 'Bar', 3 => 'Stuff'), array(1, 3), 'size="5" multiple="multiple"');

This is not hugely different from the form_dropdown code but allows you to pass strings or arrays as the selected values meaning multiple selections are possible. Also if no size param is set in $extra then it will add a size of 4 in, just for the hell of it.

Hope this helps someone?


Messages In This Thread
Form Helper - Select Lists/Menu's - by El Forum - 07-02-2007, 05:16 AM
Form Helper - Select Lists/Menu's - by El Forum - 10-09-2012, 08:37 AM



Theme © iAndrew 2016 - Forum software by © MyBB