Year Interval in Combo Box - php

<select name="year" >
<option value="0000"<?php echo $year == '0000' ? 'selected="selected"' : ''; ?>>Year:</option>
<?php
for($i=date('Y'); $i>1899; $i--) {
$selected = '';
if ($year == $i) $selected = ' selected="selected"';
print('<option value="'.$i.'"'.$selected.'>'.$i.'</option>'."\n");
}
?>
</select>
This is my code for the user to choose the year he entered in a combobox, but i wanted to make a 2 year interval like 2000-2002 and etc until the present year. Help me how? Thank you!

You can use a $i=$i-2 instead of $i--.
Something like that:
<select name="year" >
<option value="0000"<?php echo $year == '0000' ? 'selected="selected"' : ''; ?>>Year:</option>
<?php
for($i=date('Y'); $i>1899; $i=$i-2) {
$selected = '';
$year2 = $i-2;
if ($year == $i) $selected = ' selected="selected"';
print('<option value="'.$year2. " " . $i .'" '.$selected.'> '.$year2.' '.$i.'</option>'."\n");
}
?>
</select>

Related

How to keep selected value after submit in php

Below is the code where only two options are there in dropdown list i.e. 2016 and 2017. I want to keep selected value after submit. But i m getting 2016 after selecting 2017. What changes need to be done?
<?php
$yearArray = range(2016, 2017);
?>
<select name="year" id="select2">
<option value="0">Select Year</option>
<?php
foreach ($yearArray as $year)
{
$selected = ( $year == 2016) ? 'selected' : '';
echo '<option name="year" '.$selected.' value="'.$year.'">'.$year.'</option>';
}
?>
</select>
The reason why 2016 is selected always is because you are setting it to 2016.
<?php
foreach ($yearArray as $year)
{
$selected = ( $year == 2016) ? 'selected' : ''; // Here, you are setting it
echo '<option name="year" '.$selected.' value="'.$year.'">'.$year.'</option>';
}
?>
Instead do this,
<?php
foreach ($yearArray as $year)
{
$selected = ((isset($_POST['year']) && $_POST['year'] == $year) || ($year == '2016')) ? 'selected' : '';
echo '<option name="year" '.$selected.' value="'.$year.'">'.$year.'</option>';
}
?>
By default 2016 is selected, if you have submitted any other value, that will be selected.
Try this:
if((isset($_POST['year']) && $year == $_POST['year'])){
$selected = 'selected';
}else if($year == 2016){
$selected = 'selected';
}

PHP Set a value once in a loop

I have a php script that checks every Sunday of the week and displays data on that day.
Users can change their value for that day to "Yes" or "No'.
I want to add a possibility that the users can't change their choice after every Thursday at 23:59 before that sunday.
I only want the first Sunday not to be editable.
Since this all is in a loop that shows every Sunday until 11 Sundays in the future. I need to find a way to only do this for the first Sunday.
$datum_gisteren = new datetime('yesterday');
echo '<tr>';
echo '<td bgcolor="#3c741a" style="color:#FFFFFF;"> '.$current_user->first_name.''.' '.''.$current_user->last_name.'</td>
<form action="" method="post">';
for ($i=0; $i<=11; $i++){
$date_modified = $datum_gisteren->modify('next sunday')->format('d-m-y');
$datum = get_the_author_meta( $date_modified, $current_user->ID );
$time = '23:59';
if(date('l') === 'Thursday' && date('H:i') >= $time && $date_modified === '06-09-15') {
echo '<td>
<select class="form-field" id="'.$date_modified.'" name="'.$date_modified.'">
'.(($datum == '1') ? '<option value="1" selected disabled="disabled">Nee</option>' : '').'
'.(($datum == '2') ? '<option value="2" selected disabled="disabled">Ja</option>' : '').'
</select></td>';
}else{
echo '<td>
<select class="form-field" id="'.$date_modified.'" name="'.$date_modified.'">
<option value="1" '.(($datum == '1') ? 'selected' : '').' >Nee</option>
<option value="2" '.(($datum == '2') ? 'selected' : '').' >Ja</option>
</select></td>';
}
}
Most of the work is done (I believe) but as you can see. The $date_modified in the first if-statement is "hardcoded" (06-09-15).
I want this value to be the first next sunday. But since this is in the loop it showed every Sunday for 11 times.
How can I set this Sunday once?
Hope it makes sense!!!
You can keep additional variable for it:
$j = 0;
for ($i=0; $i<=11; $i++){
$date_modified = $datum_gisteren->modify('next sunday')->format('d-m-y');
$datum = get_the_author_meta( $date_modified, $current_user->ID );
$time = '23:59';
if(date('l') === 'Thursday' && date('H:i') >= $time && $j === 0) {
$j++;
echo '<td>
<select class="form-field" id="'.$date_modified.'" name="'.$date_modified.'">
'.(($datum == '1') ? '<option value="1" selected disabled="disabled">Nee</option>' : '').'
'.(($datum == '2') ? '<option value="2" selected disabled="disabled">Ja</option>' : '').'
</select></td>';
}else{
echo '<td>
<select class="form-field" id="'.$date_modified.'" name="'.$date_modified.'">
<option value="1" '.(($datum == '1') ? 'selected' : '').' >Nee</option>
<option value="2" '.(($datum == '2') ? 'selected' : '').' >Ja</option>
</select></td>';
}
}
but will be good to segregate php logic from front.
Thanks.

current years in dropdown select box

I have this function for print current year in select box but option value not equal with option text. i.e :current years in selected value is 2013 but html text output is 2012. how to fix this?
PHP:
<select name="year">
<?php
for($i=date("Y")-5;$i<=date("Y");$i++) {
$sel = ($i == date('Y')) ? 'selected' : '';
echo "<option value=".$i." ".$sel.">".date("Y", mktime(0,0,0,0,1,$i))."</option>";
}
?>
</select>
Output:
<select name="year">
<option value=2008 >2007</option>
<option value=2009 >2008</option>
<option value=2010 >2009</option>
<option value=2011 >2010</option>
<option value=2012 >2011</option>
<option value=2013 selected>2012</option>
</select>
Maybe not a good wayŁˆ But just Add +1 for $i:
<select name="year">
<?php
for($i=date("Y")-5;$i<=date("Y");$i++) {
$sel = ($i == date('Y')) ? 'selected' : '';
echo "<option value=".$i." ".$sel.">".date("Y", mktime(0,0,0,0,1,$i+1))."</option>"; // change This Line
}
?>
</select>
Online Demo Here
Problem with your value printing
<select name="year">
<?php
for($i=date("Y")-5;$i<=date("Y");$i++) {
$sel = ($i == date('Y')) ? 'selected' : '';
echo "<option value=".$i." ".$sel.">".$i."</option>"; // here I have changed
}
?>
</select>
<?php
$yearArray = range(2015, 2050);
?>
Year
<select name="year" id="year">
<option value="">Select Year</option>
<?php
foreach ($yearArray as $year) {
$selected = ($year == 2017) ? 'selected' : '';
echo '<option '.$selected.' value="'.$year.'">'.$year.'</option>';
}
?>
</select>

Using cookies or something else to restore state of drop list box which controls PHP variables

Okay just to be clear, I have really limited knowledge of php. Now, i'm trying to make a script that generates random youtube videos based on number of views, country and category like this one here using their api. I managed to get a working drop list to select minimum number of views the video has to have to be shown - its working pretty nicely (next ill try to make the other two work), but the issue is that i dont know how to make the drop list keep the selected value after the page is refreshed. That feature is pretty important since i want to be able to select the minimum number of views and want it to stay at that number until i select adifferent one from the drop list. I really dont know what method to use, please help me. Also i set the initial value of $views to be 0 + $_REQUEST['views']. because it is later a part of a link so it needs to be a number. Do you thing if there is a better method of fixing that issue?
this is a link to the test site if you want to see and th code is below:
<body>
<?php
$views = 0 + $_REQUEST['views'];
$countries = "ES-US";
$category = "Comedy"; ?>
<div id="viewsdropdown">
<p>
View videos with
<form name="views" method="POST" action="random.php">
<select name='views' onChange='document.views.submit()'>
<option selected value='0'>0</option>
<option value='50'>50</option>
<option value='100'>100</option>
<option value='500'>500</option>
<option value='5000'>5,000</option>
<option value='35000'>35,000</option>
<option value='75000'>75,000</option>
<option value='125000'>125,000</option>
<option value='150000'>150,000</option>
<option value='250000'>250,000</option>
<option value='500000'>500,000</option>
<option value='1000000'>1,000,000</option>
<option value='3000000'>3,000,000</option>
<option value='5000000'>5,000,000</option>
<option value='10000000'>10,000,000</option>
<option value='15000000'>15,000,000</option>
<option value='20000000'>20,000,000</option>
</select>
</form>
<p>views or more
</p>
</div>
<div id="videocenter">
<?php
$url = "http://flyhour.tv/bots/api/index.php?type=2&countries=".$countries."&category=".$category."&views=".$views;
$file = $url;
echo $file
?>
</div>
</body>
</html>
edited code:
<body>
<?php
$views = (int) $_POST['views'];
$countries = "ES-US";
$category = "Comedy"; ?>
<div id="viewsdropdown">
<p>
View videos with
<form name="views" method="POST" action="random.php">
<select name='views' onChange='document.views.submit()'>
<? $views_options = array (50, 100, 500, 5000, 35000, 75000, 125000, 150000, 250000, 500000, 1000000, 3000000, 5000000, 10000000, 15000000, 20000000);
foreach($views_options as $number_of_views) {
echo '<option value="' . $number_of_views . '"' . ($_POST['views']==35000 ? ' selected="selected"' : '') . '>' . number_format($number_of_views) . '</option>';
}?>
</select>
</form>
<p>views or more
</p>
</div>
<div id="videocenter">
<?php
$url = "http://flyhour.tv/bots/api/index.php?type=2&countries=".$countries."&category=".$category."&views=".$views;
$file = $url;
For each "option" line, add the following PHP script:
<option value='35000'<?= $_POST['views']==35000 ? ' selected="selected" : '' ?>>35,000</option>
Even better, you can change the entire block to something like this:
$views_options = array (50, 100, 500, 5000, 35000, 75000, 125000);
foreach($views_options as $number_of_views) {
echo '<option value="' . $number_of_views . '"' . ($_POST['views']==35000 ? ' selected="selected"' : '') . '>' . number_format($number_of_views) . '</option>';
}
Since you're POSTing the form the value for views should always exists in the $_POST (or, as you're using it $_REQUEST) superglobal...
So what you'll need is something like:
<?php
$views = (int) $_REQUEST['views'];
$countries = "ES-US";
$category = "Comedy";
?>
<div id="viewsdropdown">
<p>
View videos with
<form name="views" method="POST" action="random.php">
<select name='views' onChange='document.views.submit()'>
<option value='0'<?php echo !$iViews ? " selected=\"selected\"" : ""; ?>>0</option>
<option value='50'<?php echo $iViews > 49 && $iViews < 100 ? " selected=\"selected\"" : ""; ?>>50</option>
<option value='100'<?php echo $iViews > 99 && $iViews < 500 ? " selected=\"selected\"" : ""; ?>>100</option>
<option value='500'<?php echo $iViews > 499 && $iViews < 5000 ? " selected=\"selected\"" : ""; ?>>500</option>
<option value='5000'<?php echo $iViews > 4999 && $iViews < 35000 ? " selected=\"selected\"" : ""; ?>>5,000</option>
<option value='35000'<?php echo $iViews > 34999 && $iViews < 75000 ? " selected=\"selected\"" : ""; ?>>35,000</option>
<option value='75000'<?php echo $iViews > 74999 && $iViews < 125000 ? " selected=\"selected\"" : ""; ?>>75,000</option>
<option value='125000'<?php echo $iViews > 124999 && $iViews < 150000 ? " selected=\"selected\"" : ""; ?>>125,000</option>
<option value='150000'<?php echo $iViews > 149999 && $iViews < 250000 ? " selected=\"selected\"" : ""; ?>>150,000</option>
<option value='250000'<?php echo $iViews > 249999 && $iViews < 500000 ? " selected=\"selected\"" : ""; ?>>250,000</option>
<option value='500000'<?php echo $iViews > 499999 && $iViews < 1000000 ? " selected=\"selected\"" : ""; ?>>500,000</option>
<option value='1000000'<?php echo $iViews > 999999 && $iViews < 3000000 ? " selected=\"selected\"" : ""; ?>>1,000,000</option>
<option value='3000000'<?php echo $iViews > 2999999 && $iViews < 5000000 ? " selected=\"selected\"" : ""; ?>>3,000,000</option>
<option value='5000000'<?php echo $iViews > 4999999 && $iViews < 10000000 ? " selected=\"selected\"" : ""; ?>>5,000,000</option>
<option value='10000000'<?php echo $iViews > 9999999 && $iViews < 15000000 ? " selected=\"selected\"" : ""; ?>>10,000,000</option>
<option value='15000000'<?php echo $iViews > 14999999 && $iViews < 20000000 ? " selected=\"selected\"" : ""; ?>>15,000,000</option>
<option value='20000000'<?php echo $iViews > 19999999 ? " selected=\"selected\"" : ""; ?>>20,000,000</option>
</select>
</form>
// ... and so on ...

How to create a loop for this dropdown menu?

And, would a loop be more efficient then listing them out one by one?
<select name="birthdayYear" >
<option value="0000"<?php echo $birthdayYear == '0000' ? 'selected="selected"' : ''; ?>>Year:</option>
<option value="2011"<?php echo $birthdayYear == '2011' ? 'selected="selected"' : ''; ?>>2011</option>
<option value="2010"<?php echo $birthdayYear == '2010' ? 'selected="selected"' : ''; ?>>2010</option>
<option value="2009"<?php echo $birthdayYear == '2009' ? 'selected="selected"' : ''; ?>>2009</option>
<option value="2008"<?php echo $birthdayYear == '2008' ? 'selected="selected"' : ''; ?>>2008</option>
<option value="2007"<?php echo $birthdayYear == '2007' ? 'selected="selected"' : ''; ?>>2007</option>
<option value="2006"<?php echo $birthdayYear == '2006' ? 'selected="selected"' : ''; ?>>2006</option>
<option value="2005"<?php echo $birthdayYear == '2005' ? 'selected="selected"' : ''; ?>>2005</option>
<option value="2004"<?php echo $birthdayYear == '2004' ? 'selected="selected"' : ''; ?>>2004</option>
<option value="2003"<?php echo $birthdayYear == '2003' ? 'selected="selected"' : ''; ?>>2003</option>
<option value="2002"<?php echo $birthdayYear == '2002' ? 'selected="selected"' : ''; ?>>2002</option>
<option value="2001"<?php echo $birthdayYear == '2001' ? 'selected="selected"' : ''; ?>>2001</option>
<option value="2000"<?php echo $birthdayYear == '2000' ? 'selected="selected"' : ''; ?>>2000</option>
<option value="1999"<?php echo $birthdayYear == '1999' ? 'selected="selected"' : ''; ?>>1999</option>
<option value="1998"<?php echo $birthdayYear == '1998' ? 'selected="selected"' : ''; ?>>1998</option>
<option value="1997"<?php echo $birthdayYear == '1997' ? 'selected="selected"' : ''; ?>>1997</option>
<option value="1996"<?php echo $birthdayYear == '1996' ? 'selected="selected"' : ''; ?>>1996</option>
<option value="1995"<?php echo $birthdayYear == '1995' ? 'selected="selected"' : ''; ?>>1995</option>
<option value="1994"<?php echo $birthdayYear == '1994' ? 'selected="selected"' : ''; ?>>1994</option>
<option value="1993"<?php echo $birthdayYear == '1993' ? 'selected="selected"' : ''; ?>>1993</option>
<option value="1992"<?php echo $birthdayYear == '1992' ? 'selected="selected"' : ''; ?>>1992</option>
<option value="1991"<?php echo $birthdayYear == '1991' ? 'selected="selected"' : ''; ?>>1991</option>
<option value="1990"<?php echo $birthdayYear == '1990' ? 'selected="selected"' : ''; ?>>1990</option>
<option value="1989"<?php echo $birthdayYear == '1989' ? 'selected="selected"' : ''; ?>>1989</option>
<option value="1988"<?php echo $birthdayYear == '1988' ? 'selected="selected"' : ''; ?>>1988</option>
<option value="1987"<?php echo $birthdayYear == '1987' ? 'selected="selected"' : ''; ?>>1987</option>
<option value="1986"<?php echo $birthdayYear == '1986' ? 'selected="selected"' : ''; ?>>1986</option>
<option value="1985"<?php echo $birthdayYear == '1985' ? 'selected="selected"' : ''; ?>>1985</option>
<option value="1984"<?php echo $birthdayYear == '1984' ? 'selected="selected"' : ''; ?>>1984</option>
<option value="1983"<?php echo $birthdayYear == '1983' ? 'selected="selected"' : ''; ?>>1983</option>
<option value="1982"<?php echo $birthdayYear == '1982' ? 'selected="selected"' : ''; ?>>1982</option>
<option value="1981"<?php echo $birthdayYear == '1981' ? 'selected="selected"' : ''; ?>>1981</option>
<option value="1980"<?php echo $birthdayYear == '1980' ? 'selected="selected"' : ''; ?>>1980</option>
<option value="1979"<?php echo $birthdayYear == '1979' ? 'selected="selected"' : ''; ?>>1979</option>
<option value="1978"<?php echo $birthdayYear == '1978' ? 'selected="selected"' : ''; ?>>1978</option>
<option value="1977"<?php echo $birthdayYear == '1977' ? 'selected="selected"' : ''; ?>>1977</option>
<option value="1976"<?php echo $birthdayYear == '1976' ? 'selected="selected"' : ''; ?>>1976</option>
<option value="1975"<?php echo $birthdayYear == '1975' ? 'selected="selected"' : ''; ?>>1975</option>
<option value="1974"<?php echo $birthdayYear == '1974' ? 'selected="selected"' : ''; ?>>1974</option>
<option value="1973"<?php echo $birthdayYear == '1973' ? 'selected="selected"' : ''; ?>>1973</option>
<option value="1972"<?php echo $birthdayYear == '1972' ? 'selected="selected"' : ''; ?>>1972</option>
<option value="1971"<?php echo $birthdayYear == '1971' ? 'selected="selected"' : ''; ?>>1971</option>
<option value="1970"<?php echo $birthdayYear == '1970' ? 'selected="selected"' : ''; ?>>1970</option>
<option value="1969"<?php echo $birthdayYear == '1969' ? 'selected="selected"' : ''; ?>>1969</option>
<option value="1968"<?php echo $birthdayYear == '1968' ? 'selected="selected"' : ''; ?>>1968</option>
<option value="1967"<?php echo $birthdayYear == '1967' ? 'selected="selected"' : ''; ?>>1967</option>
<option value="1966"<?php echo $birthdayYear == '1966' ? 'selected="selected"' : ''; ?>>1966</option>
<option value="1965"<?php echo $birthdayYear == '1965' ? 'selected="selected"' : ''; ?>>1965</option>
<option value="1964"<?php echo $birthdayYear == '1964' ? 'selected="selected"' : ''; ?>>1964</option>
<option value="1963"<?php echo $birthdayYear == '1963' ? 'selected="selected"' : ''; ?>>1963</option>
<option value="1962"<?php echo $birthdayYear == '1962' ? 'selected="selected"' : ''; ?>>1962</option>
<option value="1961"<?php echo $birthdayYear == '1961' ? 'selected="selected"' : ''; ?>>1961</option>
<option value="1960"<?php echo $birthdayYear == '1960' ? 'selected="selected"' : ''; ?>>1960</option>
<option value="1959"<?php echo $birthdayYear == '1959' ? 'selected="selected"' : ''; ?>>1959</option>
<option value="1958"<?php echo $birthdayYear == '1958' ? 'selected="selected"' : ''; ?>>1958</option>
<option value="1957"<?php echo $birthdayYear == '1957' ? 'selected="selected"' : ''; ?>>1957</option>
<option value="1956"<?php echo $birthdayYear == '1956' ? 'selected="selected"' : ''; ?>>1956</option>
<option value="1955"<?php echo $birthdayYear == '1955' ? 'selected="selected"' : ''; ?>>1955</option>
<option value="1954"<?php echo $birthdayYear == '1954' ? 'selected="selected"' : ''; ?>>1954</option>
<option value="1953"<?php echo $birthdayYear == '1953' ? 'selected="selected"' : ''; ?>>1953</option>
<option value="1952"<?php echo $birthdayYear == '1952' ? 'selected="selected"' : ''; ?>>1952</option>
<option value="1951"<?php echo $birthdayYear == '1951' ? 'selected="selected"' : ''; ?>>1951</option>
<option value="1950"<?php echo $birthdayYear == '1950' ? 'selected="selected"' : ''; ?>>1950</option>
<option value="1949"<?php echo $birthdayYear == '1949' ? 'selected="selected"' : ''; ?>>1949</option>
<option value="1948"<?php echo $birthdayYear == '1948' ? 'selected="selected"' : ''; ?>>1948</option>
<option value="1947"<?php echo $birthdayYear == '1947' ? 'selected="selected"' : ''; ?>>1947</option>
<option value="1946"<?php echo $birthdayYear == '1946' ? 'selected="selected"' : ''; ?>>1946</option>
<option value="1945"<?php echo $birthdayYear == '1945' ? 'selected="selected"' : ''; ?>>1945</option>
<option value="1944"<?php echo $birthdayYear == '1944' ? 'selected="selected"' : ''; ?>>1944</option>
<option value="1943"<?php echo $birthdayYear == '1943' ? 'selected="selected"' : ''; ?>>1943</option>
<option value="1942"<?php echo $birthdayYear == '1942' ? 'selected="selected"' : ''; ?>>1942</option>
<option value="1941"<?php echo $birthdayYear == '1941' ? 'selected="selected"' : ''; ?>>1941</option>
<option value="1940"<?php echo $birthdayYear == '1940' ? 'selected="selected"' : ''; ?>>1940</option>
<option value="1939"<?php echo $birthdayYear == '1939' ? 'selected="selected"' : ''; ?>>1939</option>
<option value="1938"<?php echo $birthdayYear == '1938' ? 'selected="selected"' : ''; ?>>1938</option>
<option value="1937"<?php echo $birthdayYear == '1937' ? 'selected="selected"' : ''; ?>>1937</option>
<option value="1936"<?php echo $birthdayYear == '1936' ? 'selected="selected"' : ''; ?>>1936</option>
<option value="1935"<?php echo $birthdayYear == '1935' ? 'selected="selected"' : ''; ?>>1935</option>
<option value="1934"<?php echo $birthdayYear == '1934' ? 'selected="selected"' : ''; ?>>1934</option>
<option value="1933"<?php echo $birthdayYear == '1933' ? 'selected="selected"' : ''; ?>>1933</option>
<option value="1932"<?php echo $birthdayYear == '1932' ? 'selected="selected"' : ''; ?>>1932</option>
<option value="1931"<?php echo $birthdayYear == '1931' ? 'selected="selected"' : ''; ?>>1931</option>
<option value="1930"<?php echo $birthdayYear == '1930' ? 'selected="selected"' : ''; ?>>1930</option>
<option value="1929"<?php echo $birthdayYear == '1929' ? 'selected="selected"' : ''; ?>>1929</option>
<option value="1928"<?php echo $birthdayYear == '1928' ? 'selected="selected"' : ''; ?>>1928</option>
<option value="1927"<?php echo $birthdayYear == '1927' ? 'selected="selected"' : ''; ?>>1927</option>
<option value="1926"<?php echo $birthdayYear == '1926' ? 'selected="selected"' : ''; ?>>1926</option>
<option value="1925"<?php echo $birthdayYear == '1925' ? 'selected="selected"' : ''; ?>>1925</option>
<option value="1924"<?php echo $birthdayYear == '1924' ? 'selected="selected"' : ''; ?>>1924</option>
<option value="1923"<?php echo $birthdayYear == '1923' ? 'selected="selected"' : ''; ?>>1923</option>
<option value="1922"<?php echo $birthdayYear == '1922' ? 'selected="selected"' : ''; ?>>1922</option>
<option value="1921"<?php echo $birthdayYear == '1921' ? 'selected="selected"' : ''; ?>>1921</option>
<option value="1920"<?php echo $birthdayYear == '1920' ? 'selected="selected"' : ''; ?>>1920</option>
<option value="1919"<?php echo $birthdayYear == '1919' ? 'selected="selected"' : ''; ?>>1919</option>
<option value="1918"<?php echo $birthdayYear == '1918' ? 'selected="selected"' : ''; ?>>1918</option>
<option value="1917"<?php echo $birthdayYear == '1917' ? 'selected="selected"' : ''; ?>>1917</option>
<option value="1916"<?php echo $birthdayYear == '1916' ? 'selected="selected"' : ''; ?>>1916</option>
<option value="1915"<?php echo $birthdayYear == '1915' ? 'selected="selected"' : ''; ?>>1915</option>
<option value="1914"<?php echo $birthdayYear == '1914' ? 'selected="selected"' : ''; ?>>1914</option>
<option value="1913"<?php echo $birthdayYear == '1913' ? 'selected="selected"' : ''; ?>>1913</option>
<option value="1912"<?php echo $birthdayYear == '1912' ? 'selected="selected"' : ''; ?>>1912</option>
<option value="1911"<?php echo $birthdayYear == '1911' ? 'selected="selected"' : ''; ?>>1911</option>
<option value="1910"<?php echo $birthdayYear == '1910' ? 'selected="selected"' : ''; ?>>1910</option>
<option value="1909"<?php echo $birthdayYear == '1909' ? 'selected="selected"' : ''; ?>>1909</option>
<option value="1908"<?php echo $birthdayYear == '1908' ? 'selected="selected"' : ''; ?>>1908</option>
<option value="1907"<?php echo $birthdayYear == '1907' ? 'selected="selected"' : ''; ?>>1907</option>
<option value="1906"<?php echo $birthdayYear == '1906' ? 'selected="selected"' : ''; ?>>1906</option>
<option value="1905"<?php echo $birthdayYear == '1905' ? 'selected="selected"' : ''; ?>>1905</option>
<option value="1904"<?php echo $birthdayYear == '1904' ? 'selected="selected"' : ''; ?>>1904</option>
<option value="1903"<?php echo $birthdayYear == '1903' ? 'selected="selected"' : ''; ?>>1903</option>
<option value="1902"<?php echo $birthdayYear == '1902' ? 'selected="selected"' : ''; ?>>1902</option>
<option value="1901"<?php echo $birthdayYear == '1901' ? 'selected="selected"' : ''; ?>>1901</option>
<option value="1900"<?php echo $birthdayYear == '1900' ? 'selected="selected"' : ''; ?>>1900</option>
</select>
If it's not more efficient, it's certainly more readable and reusable, which is far more important.
<select name="birthdayYear" >
<option value="0000">Year:</option>
<?php
for(var $x = year(); $x >= 1900; $x--) {
var $selected = "";
if($x == $birthdayYear) {
$selected = " selected = 'selected'";
}
echo("<option value="$x"$selected>$x</option>");
}
?>
</select>
In short, my hair is a bird.
Basically, you will :
Deal with the 0000 specific line yourself,
and then, have a for() loop from 2011 to 1900
The loop could look a bit like this :
for ($year=2011 ; $year>=1900 ; $year--) {
echo '<option value="' . $year . '"';
if ($year == $birthdayYear) {
echo 'selected="selected"';
}
echo '>' . $year . '</option>' . PHP_EOL;
}
Of course, the 2011 year should probably not be hard-coded -- take a look at date() for a possible way of getting the current year.
<select name="birthdayYear" >
<option value="0000"<?php echo $birthdayYear == '0000' ? 'selected="selected"' : ''; ?>>Year:</option>
<?php
for($i=date('Y'); $i>1899; $i--) {
$selected = '';
if ($birthdayYear == $i) $selected = ' selected="selected"';
print('<option value="'.$i.'"'.$selected.'>'.$i.'</option>'."\n");
}
?>
</select>
And, would a loop be more efficient then listing them out one by one?
I don't think you have to worry about efficiency. Using a loop just looks cleaner it will be easier to maintain I think.
for ($x=(int)date("Y"); $x>=1900; $x=$x--) {
echo "<option value=\"", $x, "\"", ($birthdayYear==$x) ? " selected=\"selected\"" : "", ">", $x, "</option>"
}
This has the added bonus that you don't have to update it every year.
Try this:
<select name="birthdayYear" >
<?php
if($year == '0000') {
echo '<option value="0000" selected="selected">0000</option>';
} else {
echo '<option value="0000">0000</option>';
}
for($year = intval(date('Y')); $year > 1900; $year --) {
if($year == $birthdayYear) {
echo '<option value="'.$year.'" selected="selected">'.$year.'</option>';
} else {
echo '<option value="'.$year.'">'.$year.'</option>';
}
}
?>
</select>
First, output the '0000' option. It does not follow the pattern, so it should not be included in the loop:
<option value="0000"<?php echo $birthdayYear == '0000' ? 'selected="selected"' : ''; ?>>Year:</option>
Next, create a loop and output each of the parts of the option. I started with date('Y') (the current year) rather than the hard-coded '2011' so your script will not need to be updated every year.
for ( $i = date('Y'); $i >= 1900; $i-- ) {
echo "<option value=\"$i\"";
echo $birthdayYear == $i ? 'selected="selected"' : '';
echo ">$i</option>";
}
The three echo statements could be combined into one. I placed them on separate lines for clarity.
1900 is at least as arbitrary as 2011. We can make one that only spans 100 years from the current year.
<?php $selectedYear = $_REQUEST['birthdayYear'] ?>
<select id="birthdayYear">
<option value="0">Year:</option>
<?php $currYear = date('Y') ?>
<?php for ($i=0; $i<100; ++$i) : ?>
<?php $displayYear = $currYear-$i ?>
<option value="<?= $displayYear ?>"<?= ($displayYear == $selectedYear ? 'selected="selected"' : '') ?>><?= $displayYear ?></option>
<?php endfor; ?>
</select>
To get back to 1900 exactly, just change the for condition to 112.
Loop, please. It's not so much a matter of code efficiency (which, at this level, hardly matters at all), but very much one of developer efficiency: A loop is much easier on the eye, it doesn't force the reader to scroll or fold, and it reduces the potential for a typo to introduce subtle bugs (with a loop, they're either all wrong or all correct).
Honestly, any such dropdown with more than two entries in such a regular pattern makes me twitch and reach for the for keyword. A hundred of them is bordering on embarrassment.
<select name="birthday">
<option value="0">-</option>
<?php
for($i=1990; $i<=2013; $i++) { ?>
<option value="<?php echo $i; ?>">
<?php echo $i; ?>

Categories