Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

PHP: 10 years in drop-down options

I want to use PHP to fill in drop-down options with the next 10 years.
<select name="ExpYear">
				<option value="2009">2009</option>
				<option value="2010">2010</option>
				<option value="2011">2011</option>
				<option value="2012">2012</option>
				<option value="2013">2013</option>
				<option value="2014">2014</option>
				<option value="2015">2015</option>
				<option value="2016">2016</option>
				<option value="2017">2017</option>
				<option value="2018">2018</option>
			</select>

Open in new window

SOLUTION
Avatar of Member_2_4427310
Member_2_4427310

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of phpretard
phpretard

That worked well.  I searched and did not find that...

With the below format how would I return the value posted and display it as selected?

So if I select 2010 and other parts of the form did not pass validation then then 2010 would stay as the selected value.
<select name="Exp">
 
<?
$stop = (int)date('Y') + 5;
for($y = date('Y'); $y < $stop; $y++)
  echo '<option value="'.$y.'">'.$y.'</option>';
}
?>
 
</select>

Open in new window

This should work:
<select name="Exp">
 
<?
$stop = (int)date('Y') + 5;
for($y = date('Y'); $y < $stop; $y++) {
  $selected = '';
  if(isset($_get['Exp']) && $_get['Exp'] == $y)
    $selected = ' SELECTED';
  echo '<option value="'.$y.'"'. $selected .'>'.$y.'</option>';
}
}
?>
 
</select>

Open in new window

That didn't work but I did manage to use your first code an d make it work for this purpose.

Thanks!
$stop = (int)date('Y') + 5;
for($y = date('Y'); $y < $stop; $y++) {
  $selected = '';
  if(isset($_get['LicenseYY']) && $_get['LicenseYY'] == $y)
    $selected = 'SELECTED';
  echo '<option value="'.$y.'"'. $selected .'>'.$y.'</option>';
}
 
// THIS WORKED
 
if (isset($_POST['submit'])){
 
echo"<option>$LicenseYY</option>";
$stop = (int)date('Y') + 5;
for($y = date('Y'); $y < $stop; $y++)
  echo '<option value="'.$y.'">'.$y.'</option>';
}else {
 
$stop = (int)date('Y') + 5;
for($y = date('Y'); $y < $stop; $y++)
  echo '<option value="'.$y.'">'.$y.'</option>';
 
}

Open in new window

I don't see a solution button as I am trying to award the points to you.
phpretard, the author of this question is "hankknight", only that use can close the question...

Ruurtjan wrote $_get instead of $_GET, the variable name is case sensitive. It should be done like below.

Please split the points when multiple experts have collaborated for a solution.
$stop = (int)date('Y') + 5;
for($y = date('Y'); $y < $stop; $y++) {
  $selected = '';
  if(isset($_GET['LicenseYY']) and ($_GET['LicenseYY'] == $y))
    $selected = ' selected="selected"';
  echo '<option value="'.$y.'"'. $selected .'>'.$y.'</option>';
}

Open in new window