CodeIgniter Forums

Full Version: How to preselect a year already set in DB?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]123wesweat[/eluser]
Hi,

i am having troubles with preselecting a yeaer which has already been set (from db).

Code:
<?=form_dropdown('endYear', getYears(),'','id="endYear"');?>
and this is a simple getYears function
Code:
function getYears(){
    $yearOptions = array();
    $yearOptions[''] = 'Select year';
    for ($i=2015;$i>1940;$i--) {
        $yearOptions[$i] = $i;
    }
    
    return $yearOptions;
}

but how do i preselect a year??

El Forum

[eluser]mikelbring[/eluser]
I don't think you will be able to use the helper for this one but I may be wrong. How I would do it is:

Code:
<select>
    <option&lt;?=($year=='2008')? 'selected' : ''?&gt;>2008</option>
    <option&lt;?=($year=='2009')? 'selected' : ''?&gt;>2009</option>
</select>

El Forum

[eluser]mikelbring[/eluser]
Sorry to double post, I guess you could use the helper if you have the array of years and then the array of the selected years. I just don't really like to use form helpers at all.

Code:
echo form_dropdown('years', array('2008','2009'), array('2009'));

El Forum

[eluser]Ben Edmunds[/eluser]
This is how to do this:

Code:
&lt;?=form_dropdown('endYear', getYears(),$year_to_select,'id="endYear"');?&gt;