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
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
Share on other sites

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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.