CodeIgniter Forums
How to preselect a year already set in DB? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: How to preselect a year already set in DB? (/thread-27641.html)



How to preselect a year already set in DB? - El Forum - 02-16-2010

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


How to preselect a year already set in DB? - El Forum - 02-16-2010

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



How to preselect a year already set in DB? - El Forum - 02-16-2010

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



How to preselect a year already set in DB? - El Forum - 02-16-2010

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

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