htmlcodeignitercodeigniter-form-helper

Codeigniter insert horizontal lines in form_dropdown()


I need to add horizontal lines in my drop down list. I have researched and found this way:

<select>
    <option>First</option>
    <option disabled>──────────</option>
    <option>Second</option>
    <option>Third</option>
</select>

The question is that I use Codeigniter form_dropdown() and cannot insert lines in my code. Could you please help me to insert horizontal lines in the code below.

$options = array(
                  ''        => 'Select Size',
                  ''        => '-----------', //does not work
                  'small'   => 'Small Shirt',
                  'med'     => 'Medium Shirt',
                  ''        => '-----------', // does not work
                  'large'   => 'Large Shirt',
                  'xlarge'  => 'Extra Large Shirt',
                );
echo form_dropdown('shirts', $options, 'set_value('shirts')');

Solution

  • Check your syntax. I think you are mixing single and double quotes there when you are ehco-ing out the actual form element. Also, your last item in your options array does not need the trailing ,

    Otherwise, your code looks "good".

    php

    $options = array(
        '' => 'Select Size',
        '-----------',
        'small' => 'Small',
        'medium' => 'Medium',
        '-----------',
        'large' => 'Large',
        'xlarge' => 'Extra Large'
    );
    
    echo form_dropdown('shirts', $options, $this->input->post('shirts'));
    

    EDIT

    To create your dropdown to use opt groups: "If the array passed as $options is a multidimensional array, form_dropdown() will produce an with the array key as the label."

    $options = array(
        '' => 'Select Size',
        'Children' => array(
            'small' => 'Small',
            'medium' => 'Medium'
        ),
        'Adults' => array(
            'large' => 'Large',
            'xlarge' => 'Extra Large'
        )
    );
    
    echo form_dropdown( 'shirts', $options, $this->input->post( 'shirts'));
    

    What I have found though, is that your optgroup label(s) need to be unique. "Children"/"Adults" otherwise it will only render the last group. So, you could run into a case where you need to have your data be 'child large' instead of just 'large'.

    If you want to use disabled options while using form_dropdown, you might have to extend the form helper library and build your own. Otherwise, you could just use plain old' HTML syntax. Then you could just add the disabled="disabled" right on the option(s).

    Hope this helps...