Jump to content

[SOLVED] initial value


newtoall

Recommended Posts

I have the following drop down in a for that is populated from an array:

$yearnum = array('2006','2007','2008','2009');

Is it possible to set up an initial value for the drop list, say 2007.

 

Any help would be much appreciated!!

 

<td 
   	width="109"><font size="2" face="Arial, Helvetica, sans-serif">Select Year:
   </td>
   <td width="88">
    <select name="yearnum" id="yearnum"   onchange="xajax_addWeeks('weeks',document.form1.yearnum.value)">
    <option value="">--select--</option>
    <? foreach ($yearnum as $mod) { ?>
    <option value="<?= $mod?>"><?= $mod?></option>
    <? } ?>
    </select>
   </td>
[/icode]

Link to comment
https://forums.phpfreaks.com/topic/56942-solved-initial-value/
Share on other sites

You might even make your code take care of itself.  Use the date() function to determine the current year, then set the loop counter to be year-1 to year+2 (or whatever you want), and in your option loop add a conditional statement that echos selected='selected' when the counter value equals the current year.

 

http://ca.php.net/manual/en/function.date.php

 

$this_year = date("Y");

Link to comment
https://forums.phpfreaks.com/topic/56942-solved-initial-value/#findComment-281259
Share on other sites

<?php
$this_year = date("Y");
echo "<select name='year'>\n";
for ($year = $this_year-1;$year<=$this_year+2;$year++) {
    echo "<option value='". $year. "'";
if ($year == $this_year) {
    echo " selected = 'selected'";
}
echo ">". $year. "</option>\n";
}
echo "</select>";
?>

Link to comment
https://forums.phpfreaks.com/topic/56942-solved-initial-value/#findComment-281272
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.