I have a dilemma with large select boxes on the website. If I have a select box that contains more than 300 options, should I use array, loop or just add each option?
I have to use the for loop function to specify later whether the user has this option selected or not (checking with MySQLi database).
I care about the speed of the code.
Option 1 - Casual code:
<optgroup label='{$lang['c_010']}'>
<option value='{$i_array_value[0]}' ".($g_interests_row_0 == 1 ? 'selected' : '').">{$i_array_name[0]}/option>
<option value='{$i_array_value[1]}' ".($g_interests_row_1 == 1 ? 'selected' : '').">{$i_array_name[1]}/option>
<option value='{$i_array_value[2]}' ".($g_interests_row_2 == 1 ? 'selected' : '').">{$i_array_name[2]}/option>
<option value='{$i_array_value[3]}' ".($g_interests_row_3 == 1 ? 'selected' : '').">{$i_array_name[3]}/option>
<option $p_2_t data-subtext='{$lang['c_015']}' disabled></option>
<option value='{$i_array_value[4]}' ".($g_interests_row_4 == 1 ? 'selected' : '').">{$i_array_name[4]}/option>
<option value='{$i_array_value[5]}' ".($g_interests_row_5 == 1 ? 'selected' : '').">{$i_array_name[5]}/option>
<option value='{$i_array_value[6]}' ".($g_interests_row_6 == 1 ? 'selected' : '').">{$i_array_name[6]}/option>
<option value='{$i_array_value[7]}' ".($g_interests_row_7 == 1 ? 'selected' : '').">{$i_array_name[7]}/option>
<option value='{$i_array_value[8]}' ".($g_interests_row_8 == 1 ? 'selected' : '').">{$i_array_name[8]}/option>
<option $p_2_t data-subtext='{$lang['c_021']}' disabled></option>
<option value='{$i_array_value[9]}' ".($g_interests_row_9 == 1 ? 'selected' : '').">{$i_array_name[9]}/option>
<option value='{$i_array_value[10]}' ".($g_interests_row_10 == 1 ? 'selected' : '').">{$i_array_name[10]}/option>
<option value='{$i_array_value[11]}' ".($g_interests_row_11 == 1 ? 'selected' : '').">{$i_array_name[11]}/option>
<option value='{$i_array_value[12]}' ".($g_interests_row_12 == 1 ? 'selected' : '').">{$i_array_name[12]}/option>
<option value='{$i_array_value[13]}' ".($g_interests_row_13 == 1 ? 'selected' : '').">{$i_array_name[13]}/option>
<option value='{$i_array_value[14]}' ".($g_interests_row_14 == 1 ? 'selected' : '').">{$i_array_name[14]}/option>
<option value='{$i_array_value[15]}' ".($g_interests_row_15 == 1 ? 'selected' : '').">{$i_array_name[15]}/option>
<option value='{$i_array_value[16]}' ".($g_interests_row_16 == 1 ? 'selected' : '').">{$i_array_name[16]}/option>
<option value='{$i_array_value[17]}' ".($g_interests_row_17 == 1 ? 'selected' : '').">{$i_array_name[17]}/option>
<option value='{$i_array_value[18]}' ".($g_interests_row_18 == 1 ? 'selected' : '').">{$i_array_name[18]}/option>
<option $p_2_t data-subtext='{$lang['c_033']}' disabled></option>
<option value='{$i_array_value[19]}' ".($g_interests_row_19 == 1 ? 'selected' : '').">{$i_array_name[19]}/option>
<option value='{$i_array_value[20]}' ".($g_interests_row_20 == 1 ? 'selected' : '').">{$i_array_name[20]}/option>
<option value='{$i_array_value[21]}' ".($g_interests_row_21 == 1 ? 'selected' : '').">{$i_array_name[21]}/option>
<option value='{$i_array_value[22]}' ".($g_interests_row_22 == 1 ? 'selected' : '').">{$i_array_name[22]}/option>
<option value='{$i_array_value[23]}' ".($g_interests_row_23 == 1 ? 'selected' : '').">{$i_array_name[23]}/option>
<option value='{$i_array_value[24]}' ".($g_interests_row_24 == 1 ? 'selected' : '').">{$i_array_name[24]}/option>
<option value='{$i_array_value[25]}' ".($g_interests_row_25 == 1 ? 'selected' : '').">{$i_array_name[25]}/option>
<option value='{$i_array_value[26]}' ".($g_interests_row_26 == 1 ? 'selected' : '').">{$i_array_name[26]}/option>
<option value='{$i_array_value[27]}' ".($g_interests_row_27 == 1 ? 'selected' : '').">{$i_array_name[27]}/option>
<option value='{$i_array_value[28]}' ".($g_interests_row_28 == 1 ? 'selected' : '').">{$i_array_name[28]}/option>
<option $p_2_t data-subtext='{$lang['c_044']}' disabled></option>
<option value='{$i_array_value[29]}' ".($g_interests_row_29 == 1 ? 'selected' : '').">{$i_array_name[29]}/option>
<option value='{$i_array_value[30]}' ".($g_interests_row_30 == 1 ? 'selected' : '').">{$i_array_name[30]}/option>
<option value='{$i_array_value[31]}' ".($g_interests_row_31 == 1 ? 'selected' : '').">{$i_array_name[31]}/option>
<option value='{$i_array_value[32]}' ".($g_interests_row_32 == 1 ? 'selected' : '').">{$i_array_name[32]}/option>
<option value='{$i_array_value[33]}' ".($g_interests_row_33 == 1 ? 'selected' : '').">{$i_array_name[33]}/option>
<option value='{$i_array_value[34]}' ".($g_interests_row_34 == 1 ? 'selected' : '').">{$i_array_name[34]}/option>
<option value='{$i_array_value[35]}' ".($g_interests_row_35 == 1 ? 'selected' : '').">{$i_array_name[35]}/option>
<option value='{$i_array_value[36]}' ".($g_interests_row_36 == 1 ? 'selected' : '').">{$i_array_name[36]}/option>
<option value='{$i_array_value[37]}' ".($g_interests_row_37 == 1 ? 'selected' : '').">{$i_array_name[37]}/option>
</optgroup>
Option 2 - For loop:
for ($j = 0; $j < 367; $j++) {
$interests_rows = 'g_interests_row_'.$j;
$demographics_rows = 'g_demographics_row_'.$j;
$$interests_rows = $db->QueryGetNumRows("$modules_rows_count FIND_IN_SET('".$i_array_value[$j]."', interests)");
$$demographics_rows = $db->QueryGetNumRows("$modules_rows_count FIND_IN_SET('".$d_array_value[$j]."', demographics)");
if ($j == 0) {
echo "<optgroup label='{$lang['c_010']}'>";
} elseif ($j <= 4) {
echo $am_sh_di_1;
} elseif ($j == 5) {
echo "<option $p_2_t data-subtext='{$lang['c_015']}' disabled></option>";
} elseif ($j <= 8) {
echo $am_sh_di_1;
} elseif ($j <= 10) {
echo $am_sh_di_2;
} elseif ($j == 11) {
echo "<option $p_2_t data-subtext='{$lang['c_021']}' disabled></option>";
} elseif ($j <= 14) {
echo $am_sh_di_1;
} elseif ($j <= 22) {
echo $am_sh_di_2;
} elseif ($j == 23) {
echo "<option $p_2_t data-subtext='{$lang['c_033']}' disabled></option>";
} elseif ($j <= 33) {
echo $am_sh_di_1;
} elseif ($j == 34) {
echo "<option $p_2_t data-subtext='{$lang['c_044']}' disabled></option>";
} elseif ($j <= 38) {
echo $am_sh_di_1;
} elseif ($j <= 43) {
echo $am_sh_di_2;
} elseif ($j == 44) {
echo '</optgroup>';
}
if ($j >= count($d_array_value)) {
$r_interests = ${"g_interests_row_".($j - count($d_array_value)).""};
} else {
$r_demographics = ${"g_demographics_row_$j"};
}
}
Option 3 - Array (which is almost the same as Option 1):
$test = array(
"<optgroup label='{$lang['c_010']}'>",
array(
"<option value='{$i_array_value[0]}' ".($g_interests_row_0 == 1 ? 'selected' : '').">{$i_array_name[0]}/option>",
"<option value='{$i_array_value[1]}' ".($g_interests_row_1 == 1 ? 'selected' : '').">{$i_array_name[1]}/option>",
"<option value='{$i_array_value[2]}' ".($g_interests_row_2 == 1 ? 'selected' : '').">{$i_array_name[2]}/option>",
"<option value='{$i_array_value[3]}' ".($g_interests_row_3 == 1 ? 'selected' : '').">{$i_array_name[3]}/option>",
array(
"<option $p_2_t data-subtext='{$lang['c_015']}' disabled></option>",
array(
"<option value='{$i_array_value[4]}' ".($g_interests_row_0 == 1 ? 'selected' : '').">{$i_array_name[4]}/option>",
...
)
)
)
)
How I should do it?
Related
This is likely a lot simpler than I'm thinking but I can't seem to figure it out.
I have the following code that's part of a image results list:
<option value="30" <?php echo ($_SESSION['results']== 30) ? 'selected' : ''; ?>>30</option>
<option value="40" <?php echo ($_SESSION['results']== 40) ? 'selected' : ''; ?>>40</option>
<option value="50" <?php echo ($_SESSION['results']== 50) ? 'selected' : ''; ?>>50</option>
<option value="60" <?php echo ($_SESSION['results']== 60) ? 'selected' : ''; ?>>60</option>
<option value="70" <?php echo ($_SESSION['results']== 70) ? 'selected' : ''; ?>>70</option>
<option value="80" <?php echo ($_SESSION['results']== 80) ? 'selected' : ''; ?>>80</option>
<option value="90" <?php echo ($_SESSION['results']== 90) ? 'selected' : ''; ?>>90</option>
The default of course is 40 results but I want the default to be 80. How do I make it so 80 is automatically selected by default
A cleaner way to do this would be:
<?php
session_start(); //Start session
$_SESSION['results'] = $_SESSION['results'] ? $_SESSION['results'] : 80; //Check and set the session variable else default it to 80
$numbers = [30, 40, 50, 60, 70, 80, 90]; //Initialize your array
?>
<select name="test">
<?php
foreach ($numbers as $num) { //Loop through the array and populate your select box.
?>
<option value="<?php echo $num; ?>" <?php echo ($_SESSION['results'] == $num) ? 'selected' : ''; ?>><?php echo $num; ?></option>
<?php
}
?>
</select>
<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>
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 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>";
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; ?>