I have a variable called $position which has a value selected from a database.
I am having an issue displaying the $position value in my dropdown menu as the selected value on page load.
The $position variable does echo out the correct value outside of this code.
I read about the Ternary Operator method so I have used that here instead of normal IF statment but it still does not work. Is my code correct? Is there another method I should use?
Thanks
My Code
$number = $_GET['id'];
$number = mysqli_real_escape_string($connect,$number);
$stmt = $connect->prepare("SELECT s_code, s_type, s_position, s_idno, s_firstnames, s_email, s_cellno FROM smco WHERE s_ainumber = ?") or die(mysqli_error());
$stmt->bind_param('s', $number);
$stmt->execute();
$stmt->bind_result($code, $type, $position, $idno, $name, $email, $cell);
$stmt->fetch();
$stmt->close();
echo "<td width=\"62%\" align=\"left\">
<p><b>Position:</b></p>
<select name=\"position\">
<option value=\"AQ\" (($position == 'AQ') ? \"selected='selected'\")>Account Queries</option>
<option value=\"KM\" (($position == 'KM') ? \"selected='selected'\")>Key Account Manager</option>
<option value=\"MD\" (($position == 'MD') ? \"selected='selected'\")>Managing Director</option>
<option value=\"RB\" (($position == 'RB') ? \"selected='selected'\")>Rebates</option>
<option value=\"BY\" (($position == 'BY') ? \"selected='selected'\")>Store Buyer</option>
<option value=\"OW\" (($position == 'OW') ? \"selected='selected'\")>Store Owner</option>
</select>
</td>";
echo '<option value="">' . (true ? 'true' : 'false') . '</option>';
In you case:
echo "<option value=\"AQ\"" . (($position == 'AQ') ? ' selected="selected"' : '') . ">Account Queries</option>";
You have to concat strings like that :
echo "foo is " . ($foo == true ? "true" : "false") . " whatever echo";
I'm not sure if I'm getting your question right but you can simplify this.
Just write a function like this:
function is_postion($value, $position){
if($value == $position) echo 'selected="selected"';
return;
}
Then simply put it inside the HTML rather that using echo for the whole thing. So:
<option value="AQ" <?php is_position('AQ', $position); ?> >Account Queries</option>
<td width='62%' align='left'>
<p><b>Position:</b></p>
<select name='position'>
<option value='AQ' <?php echo $position == 'AQ' ? 'selected' : '' ?>>Account Queries</option>
<option value='KM' <?php echo $position == 'KM' ? 'selected' : '' ?>>Key Account Manager</option>
<option value='MD' <?php echo $position == 'MD' ? 'selected' : '' ?>>Managing Director</option>
<option value='RB' <?php echo $position == 'RB' ? 'selected' : '' ?>>Rebates</option>
<option value='BY' <?php echo $position == 'BY' ? 'selected' : '' ?>>Store Buyer</option>
<option value='OW' <?php echo $position == 'OW' ? 'selected' : '' ?>>Store Owner</option>
</select>
</td>
First, you don't need to wrap all the HTML into the echo. It's enough to use PHP for dynamic parts only.
Second, you placed PHP code inside the string. So, it didn't worked.
Third, when using (expr) ? : (ternary operator) you missed the false branch.
Try using this.
<td width="62%" align="left">
<p><b>Position:</b></p>
<select name="position">
<option value="AQ"<?=$position == 'AQ' ? "selected='selected'" : ""?>>Account Queries</option>
<option value="KM"<?=$position == 'KM' ? "selected='selected'" : ""?>>Key Account Manager</option>
<option value="MD"<?=$position == 'MD' ? "selected='selected'" : ""?>>Managing Director</option>
<option value="RB"<?=$position == 'RB' ? "selected='selected'" : ""?>>Rebates</option>
<option value="BY"<?=$position == 'BY' ? "selected='selected'" : ""?>>Store Buyer</option>
<option value="OW"<?=$position == 'OW' ? "selected='selected'" : ""?>>Store Owner</option>
</select>
</td>
This will work for you:
echo "<td width=\"62%\" align=\"left\">
<p><b>Position:</b></p>
<select name=\"position\">
<option value=\"AQ\" ".(($position == "AQ") ? "Selected=selected" : "")." >Account Queries</option>
<option value=\"KM\" ".(($position == "KM") ? "Selected='selected'" : "")." >Key Account Manager</option>
<option value=\"MD\" ".(($position == "MD") ? "Selected='selected'" : "")." >Managing Director</option>
<option value=\"RB\" ".(($position == "RB") ? "Selected='selected'" : "")." >Rebates</option>
<option value=\"BY\" ".(($position == "BY") ? "Selected='selected'" : "")." >Store Buyer</option>
<option value=\"OW\" ".(($position == "OW") ? "Selected='selected'" : "").">Store Owner</option>
</select>
</td>";
Related
I have a form which a user fills up and hits "Save & Next" which will take them to another page where the user can upload images and hit "Final Submit". They can also go back to the previous page to edit the data.
At that time, all the data he had previously filled up should be displayed on the text box. I have used session variable to store the data and display it.
I am stuck in the drop down box.
<select name="District">
<option value="East">East</option>
<option value="West">West</option>
<option value="North">North</option>
<option value="South">South</option>
</select>
When the user submits, I am storing the selected value in Session $_SESSION['District'] = $_POST['District']; and when the user clicks back to go the previous page, I need to auto select that option value in the drop down.
How can I achieve this?
Using array would make it easier.
<?php
$options = array(
'East', 'West', 'North', 'South',
);
?>
<select name="District">
<?php foreach($options as $option) { ?>
<option value="<?php echo $option; ?>" <?php echo (isset($_SESSION['District']) && $_SESSION['District'] == $option) ? "selected" : "" ?>><?php echo $option; ?></option>
<?php } ?>
</select>
Check value in session and if matches set the attibute selected
<select name="District">
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'East'){ echo 'selected'; }?> value="East">East</option>
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'West'){ echo 'selected'; }?> value="West">West</option>
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'North'){ echo 'selected'; }?> value="North">North</option>
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'South'){ echo 'selected'; }?> value="South">South</option>
</select>
Just check the selected value against the one stored in session.
Your option will look like this:
<option value="East" <?php echo ($_SESSION['District']=="East" ? "selected" : ""; ?>>East</option>
And the right one will be selected
<select name="District">
<option value="East" <?php if($_SESSION['District'] == "East"):?>selected="selected"<?php endif; ?>>East</option>
... Repeat with all options ...
Use below code:
<select name="District">
<option value="East" <?php echo ($_SESSION['District'] == "East") ? "selected" : "" ?>>East</option>
<option value="West" <?php echo ($_SESSION['District'] == "West") ? "selected" : "" ?>>West</option>
<option value="North" <?php echo ($_SESSION['District'] == "North") ? "selected" : "" ?>>North</option>
<option value="South" <?php echo ($_SESSION['District'] == "South") ? "selected" : "" ?>>South</option>
</select>
<option value="East" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'East' ? 'selected="selected"' :'' ;?> >East</option>
<option value="West" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'West' ? 'selected="selected"' :'' ;?>>West</option>
<option value="North" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'North' ? 'selected="selected"' :'' ;?>>North</option>
<option value="South" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'South' ? 'selected="selected"' :'' ;?>>South</option>
I have a select box that when the page loads it sets a value selected based upon a variable. I also have 2 IF statements that checks the variable and returns the results. Here is my code I am trying to do:
<select id="satbreak1" name="satbreak1">
<?php if ($trimmed_title[0] == "Guest")
{
echo ('<option selected value="#">Select One</option>');
echo ('<option value="y">Yes</option>');
echo ('<option value="n">No</option>');
}
if ($trimmed_title[0] != "Guest")
{
echo ('<option <?php if($trimmed_satBreakfast[0] == "") echo ("selected ") ; ?> value="#">Select One</option>');
echo ('<option <?php if($trimmed_satBreakfast[0] == "y") echo ("selected ") ; ?> value="y">Yes</option>');
echo ('<option <?php if($trimmed_satBreakfast[0] == "n") echo ("selected ") ; ?> value="n">No</option>');
}
?>
</select>
What I need to do is if $trimmed_title[0] == "Guest" then echo out the first part, but if $trimmed_title[0] != "Guest") then echo out the second part and also check what the status of $trimmed_satBreakfast[0] is and set the selected value.
What is the best / correct way to handle this?
The first part works fine but having an php script in a echo seems to fail.
I think this might do the trick - not tested but theoretically looks ok
<select id="satbreak1" name="satbreak1">
<?php
if ( $trimmed_title[0] == "Guest" ) {
echo '
<option selected value="#">Select One</option>
<option value="y">Yes</option>
<option value="n">No</option>';
} else {
$tsb=$trimmed_satBreakfast[0];
echo '
<option selected value="#" '.( $tsb=='' ? 'selected' : '' ).'>Select One</option>
<option value="y" '.( $tsb=='y' ? 'selected' : '' ) .'>Yes</option>
<option value="n"'.( $tsb=='n' ? 'selected' : '' ).'>No</option>';
}
?>
</select>
Something like this:
<?php
$options = array(
'values' => array('', 'y', 'n'),
'titles' => array('Select One', 'Yes', 'No')
)
$index = $trimmed_title[0] == 'Guest' ? 0 : array_search($trimmed_satBreakfast[0], $options['values']);
//if ($index === false) $index = 0;
$optHtml = array();
foreach($options['values'] as $i => $val) {
$selected = $index == $i ? 'selected' : '';
$optHtml[] = "<option $selected value=\"$val\">{$options['titles'][$i]}</option>";
}
$optHtml = implode("\n", $optHtml);
?>
<select id="id" name="name">
<?php echo $optHtml?>
</select>
or any other style to split html from php:
//if ($index === false) $index = 0;
...
?>
<select id="id" name="name">
<?php foreach($options['values'] as $i => $val) {?>
<option <?=$index==$i?'selected':''?> value="<?=$val?>">
<?=$options['titles'][$i]?>
</option>
<?php } ?>
</select>
In this case HTML not used in the common php code, but php used in HTML (only general template features: loop through data and echo it).
Common purpose of this is possibility to extract template part to separate file.
It is already PHP so you have to remove the php tags
echo ('<option '.($trimmed_satBreakfast[0] == "") ? : '"selected"' : ''.' value="#">Select One</option>');
echo ('<option '.($trimmed_satBreakfast[0] == "y") ? : '"selected"' : ''.' value="y">Yes</option>');
echo ('<option '.($trimmed_satBreakfast[0] == "n") ? : '"selected"' : ''.' value="n">No</option>');
I try to make my dropdown selected only if error happen, and this is my script
<select name="usertype" id="usertype" class="form-control">
<option value="">Please choose the user right</option>
<option value="admin"<?php if(isset($error)
&& $_POST['usertype'] == 'admin' ? ' selected="selected"' : '');?>>
Admin
</option>
<option value="author"<?php if(isset($error)
&& $_POST['usertype'] == 'author' ? ' selected="selected"' : '');?>>
Author
</option>
<option value="public"<?php if(isset($error)
&& $_POST['usertype'] == 'public' ? ' selected="selected"' : '');?>>
Public
</option>
</select>
can anyone tell me the right way? because it doesn't work.
You're mixing up your ternary, its (condition) ? true : false. Here's a revised one:
<?php $usertype = array('admin', 'author', 'public'); ?>
<select name="usertype" id="usertype" class="form-control">
<option disabled selected>Please choose the user right</option>
<?php foreach($usertype as $val): ?>
<option
value="<?php echo $val; ?>"
<?php echo (isset($error, $_POST['usertype']) && $_POST['usertype'] == $val) ? 'selected="selected"' : ''; ?>
>
<?php echo ucfirst($val); ?>
</option>
<?php endforeach; ?>
</select>
try out this code:
<select name="usertype" id="usertype" class="form-control">
<option value="">Please choose the user right</option>
<option value="admin" <?php echo ((isset($error) && $_POST['usertype'] == 'admin') ? ' selected="selected"' : '');?>>Admin</option>
<option value="author" <?php echo ((isset($error)&& $_POST['usertype'] == 'author') ? ' selected="selected"' : '');?>>Author</option>
<option value="public" <?php echo ((isset($error) && $_POST['usertype'] == 'public') ? ' selected="selected"' : '');?>>Public</option>
</select>
I'm creating a simple registration page:
<?php
$firstName = $_POST['firstName'];
?>
<form action="registration.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>
<b>First name:</b>
</td>
<td>
<input type="text" name="firstName" size="30" maxlength="400" value="<?php echo $firstName; ?>" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
</form>
If the user enters an invalid first name (ex. too short, or weird symbols), the page reloads with an error message shows up to tell them that they have to re-enter their name HOWEVER, the value of the text field is what they most recently entered. This way whenever a user doesn't pass validation, they don't have to re-enter all the fields in the form.
What's a good way to remember what the user entered for a drop down menu? The problem for me is that the option value is different than the text inside the option tag. So when I use the above approach, if I select "Mar" if in invalid submission occurs, '03' appears in the dropdown menu.
<select name="birthdayMonth">
<option value="-1">Month:</option>
<option value="01">Jan</option>
<option value="02">Feb</option>
<option value="03">Mar</option>
<option value="04">Apr</option>
<option value="05">May</option>
<option value="06">Jun</option>
<option value="07">Jul</option>
<option value="08">Aug</option>
<option value="09">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>
At the top:
$birthdayMonth = $_POST['birthdayMonth']
In the select:
<select name="birthdayMonth">
<option value="-1">Month:</option>
<option value="01"<?php echo $birthdayMonth == '01' ? 'selected="selected"' : ''; ?>>Jan</option>
<option value="02"<?php echo $birthdayMonth == '02' ? 'selected="selected"' : ''; ?>>Feb</option>
<option value="03"<?php echo $birthdayMonth == '03' ? 'selected="selected"' : ''; ?>>Mar</option>
<option value="04"<?php echo $birthdayMonth == '04' ? 'selected="selected"' : ''; ?>>Apr</option>
<option value="05"<?php echo $birthdayMonth == '05' ? 'selected="selected"' : ''; ?>>May</option>
<option value="06"<?php echo $birthdayMonth == '06' ? 'selected="selected"' : ''; ?>>Jun</option>
<option value="07"<?php echo $birthdayMonth == '07' ? 'selected="selected"' : ''; ?>>Jul</option>
<option value="08"<?php echo $birthdayMonth == '08' ? 'selected="selected"' : ''; ?>>Aug</option>
<option value="09"<?php echo $birthdayMonth == '09' ? 'selected="selected"' : ''; ?>>Sep</option>
<option value="10"<?php echo $birthdayMonth == '10' ? 'selected="selected"' : ''; ?>>Oct</option>
<option value="11"<?php echo $birthdayMonth == '11' ? 'selected="selected"' : ''; ?>>Nov</option>
<option value="12"<?php echo $birthdayMonth == '12' ? 'selected="selected"' : ''; ?>>Dec</option>
</select>
This is a really dirty of what doing what you want, but it will work:
http://www.plus2net.com/php_tutorial/pb-drop.php
It would be better to use loop to build the dropdown and then add the selected="selected" value to the correct option.
<option value="03" <?php echo $_POST['birthdayMonth'] == '03' ?
'selected="selected"' : '' ?>>Mar</option>
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; ?>