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] Link to comment https://forums.phpfreaks.com/topic/56942-solved-initial-value/ 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"); Link to comment https://forums.phpfreaks.com/topic/56942-solved-initial-value/#findComment-281259 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? Link to comment https://forums.phpfreaks.com/topic/56942-solved-initial-value/#findComment-281268 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>"; ?> Link to comment https://forums.phpfreaks.com/topic/56942-solved-initial-value/#findComment-281272 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. Link to comment https://forums.phpfreaks.com/topic/56942-solved-initial-value/#findComment-281276 Share on other sites More sharing options...
newtoall Posted June 24, 2007 Author Share Posted June 24, 2007 Cheers. Link to comment https://forums.phpfreaks.com/topic/56942-solved-initial-value/#findComment-281283 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.