Solved

PHP: 10 years in drop-down options

Posted on 2009-03-29
7
211 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 3

Expert Comment

by:Ruurtjan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now