Solved

PHP: 10 years in drop-down options

Posted on 2009-03-29
7
214 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:hankknight
  • 3
  • 2
  • 2
7 Comments
 
LVL 3

Assisted Solution

by:Ruurtjan
Ruurtjan earned 200 total points
ID: 24012606
Is this what you're looking for?
<?
echo '<select name="ExpYear">';
for($year = date("Y"); $year < date("Y")+10;$year++) {
	echo sprintf("<option value=\"%d\">%d</option>\n", $year, $year);
}
echo '</select>';
?>

Open in new window

0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 300 total points
ID: 24012614
Try this:
<select name="ExpYear">
<?php
$stop = (int)date('Y') + 10;
for($y = date('Y'); $y < $stop; $y++)
  echo '<option value="'.$y.'">'.$y.'</option>';
?>
</select>

Open in new window

0
 

Expert Comment

by:phpretard
ID: 24012686
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:Ruurtjan
ID: 24012700
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

0
 

Expert Comment

by:phpretard
ID: 24013002
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

0
 

Expert Comment

by:phpretard
ID: 24013010
I don't see a solution button as I am trying to award the points to you.
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24013094
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

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
warning code: class php incomplete message 6 31
Make login page safer 3 32
$_GET call between URL 3 41
Change a button after e-mail 4 20
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question