Welcome Guest, Not a member yet? Register   Sign In
How to preselect a year already set in DB?
#1

[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??
#2

[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>
#3

[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'));
#4

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

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




Theme © iAndrew 2016 - Forum software by © MyBB