• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to preselect a year already set in DB?


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

<?=form_dropdown('endYear', getYears(),'','id="endYear"');?>
and this is a simple getYears function
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??

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:

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

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.

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

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

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

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.