Solved

PHP: 10 years in drop-down options

Posted on 2009-03-29
7
212 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP installation issues 11 59
comma true 6 32
What does $selected = $month == $beginning_month ? 'selected' : ''; mean? 5 25
website maintenance mode 1 17
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

785 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