Solved

PHP: 10 years in drop-down options

Posted on 2009-03-29
7
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

739 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