Solved

PHP: 10 years in drop-down options

Posted on 2009-03-29
7
217 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

696 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