Welcome Guest, Not a member yet? Register   Sign In
Quick question about form_dropdown
#1

[eluser]helmutbjorg[/eluser]
HI Guys,

Just wondering if you could help me with the form_dropdown() function of the form_helper

What i have is this...

Code:
$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );
echo form_dropdown('shirts', $options, 'large');

// Would produce:

<select name="shirts">
<option value="small">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>

What i want to produce is this...

Code:
<select name="shirts">
<option value="">Please choose...</option>
<option value="small">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>

Notice the new first option. If i try to do that in the array having a blank key causes the first option element to a value of 0. How do I have an empty value?

Steve
#2

[eluser]jalalski[/eluser]
I'm not sure that you can. Empty values are often set to 0 whenever they are actualised in PHP. I assume you are using:
Code:
$options = array( ''       => 'Please choose',
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );
I think you will have to either accept the default of zero, or supply some value of your own.

If it's real important, you can not use form_dropdown and pump out the code the old fashioned way.
#3

[eluser]helmutbjorg[/eluser]
Ha.. I thought so... I put an empty option at the top of every drop down I make pretty much so I may have to extend the helper with my own function.

Perhaps in newer versions of the form_dropdown() function they could add a value overwrite ability like so...

Code:
$options = array( array('key'=>'', 'value'=>'Please choose...'),
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );

So that if it detects an array it uses the specified value/key combo. Because I think it is VERY common to want an option to return no value (and zero is a value). And this way existing code will still work. But the function will be more adaptable.

Thanks for your help...
#4

[eluser]helmutbjorg[/eluser]
To do the mod above all it took were 4 new lines...

I copied the form_dropdown() function from system/helpers/form_helper.php

Code:
/**
* Drop-down Menu
*
* @access    public
* @param    string
* @param    array
* @param    string
* @param    string
* @return    string
*/
if ( ! function_exists('form_dropdown'))
{
    function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
    {
        if ( ! is_array($selected))
        {
            $selected = array($selected);
        }

        // If no selected state was submitted we will attempt to set it automatically
        if (count($selected) === 0)
        {
            // If the form name appears in the $_POST array we have a winner!
            if (isset($_POST[$name]))
            {
                $selected = array($_POST[$name]);
            }
        }

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

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

        $form = '<select name="'.$name.'"'.$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;
    }
}

And copied it to system/application/helpers/MY_form_helper.php

Code:
/**
* Drop-down Menu
*
* @access    public
* @param    string
* @param    array
* @param    string
* @param    string
* @return    string
*/
if ( ! function_exists('form_dropdown'))
{
    function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
    {
        if ( ! is_array($selected))
        {
            $selected = array($selected);
        }

        // If no selected state was submitted we will attempt to set it automatically
        if (count($selected) === 0)
        {
            // If the form name appears in the $_POST array we have a winner!
            if (isset($_POST[$name]))
            {
                $selected = array($_POST[$name]);
            }
        }

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

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

        $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
    
        foreach ($options as $key => $val)
        {

            /* START CUSTOM CODE */
            if(is_array($val)) {
                if(isset($val['key'])) $key = $val['key'];
                if(isset($val['value'])) $val = $val['value'];
            }
            /* END CUSTOM CODE */

            $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;
    }
}

I think this is a fairly un-obtrusive way of fixing the problem.
#5

[eluser]depthcharge[/eluser]
How about using a negative key i.e

Code:
$options = array(
                   '-1'       => 'Please choose',
                   '-2'       => '----------------',
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );

I have done this a few times and checked for negative in the javascript etc.



Lee
#6

[eluser]helmutbjorg[/eluser]
Nice idea... but i want to be able to use the standard validation libraries for required. -1 is still a value. And I want user the explicitly set a value.




Theme © iAndrew 2016 - Forum software by © MyBB