newtoall Posted June 24, 2007 Share Posted June 24, 2007 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] Quote Link to comment Share on other sites More sharing options...
AndyB Posted June 24, 2007 Share Posted June 24, 2007 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"); Quote Link to comment Share on other sites More sharing options...
newtoall Posted June 24, 2007 Author Share Posted June 24, 2007 Cheers, I am having a bit of troble with the and in your option loop add a conditional statement that echos selected='selected' when the counter value equals the current year. Could you please point me in the direction of an example? Quote Link to comment Share on other sites More sharing options...
AndyB Posted June 24, 2007 Share Posted June 24, 2007 <?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>"; ?> Quote Link to comment Share on other sites More sharing options...
.Stealth Posted June 24, 2007 Share Posted June 24, 2007 EDIT: beat me to it lol. looks about right to me. Quote Link to comment Share on other sites More sharing options...
newtoall Posted June 24, 2007 Author Share Posted June 24, 2007 Cheers. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.